Guest User

Untitled

a guest
Oct 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. String sessionid = null;
  2. Cookie[] cookies = request.getCookies();
  3.  
  4.  
  5.  
  6. // Check to see if you need to clear session data
  7. if (request.getParameter("clear") != null){
  8. Cookie cookie = new Cookie("sessionid", "");
  9. cookie.setMaxAge(0);
  10. response.addCookie(cookie);
  11. response.sendRedirect("");
  12. }
  13.  
  14. if (cookies != null) {
  15. for (int i = 0; i < cookies.length; i++) {
  16. if (cookies[i].getName().equals("sessionid")) {
  17. sessionid = cookies[i].getValue();
  18. break;
  19. }
  20. }
  21. }
  22.  
  23. private static String generateSessionId() throws UnsupportedEncodingException {
  24. String uid = new java.rmi.server.UID().toString(); // guaranteed unique
  25. return URLEncoder.encode(uid,"UTF-8"); // encode any special chars
  26. }
  27. // If the session ID wasn't sent, generate one.
  28. // Then be sure to send it to the client with the response.
  29. if (sessionid == null) {
  30. sessionid = generateSessionId();
  31. Cookie c = new Cookie("sessionid", sessionid);
  32. response.addCookie(c);
  33. out.println("hello new person");
  34. } else {
  35. out.println("welcome back" + sessionid);
  36. }
Add Comment
Please, Sign In to add comment