Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String sessionid = null;
- Cookie[] cookies = request.getCookies();
- // Check to see if you need to clear session data
- if (request.getParameter("clear") != null){
- Cookie cookie = new Cookie("sessionid", "");
- cookie.setMaxAge(0);
- response.addCookie(cookie);
- response.sendRedirect("");
- }
- if (cookies != null) {
- for (int i = 0; i < cookies.length; i++) {
- if (cookies[i].getName().equals("sessionid")) {
- sessionid = cookies[i].getValue();
- break;
- }
- }
- }
- private static String generateSessionId() throws UnsupportedEncodingException {
- String uid = new java.rmi.server.UID().toString(); // guaranteed unique
- return URLEncoder.encode(uid,"UTF-8"); // encode any special chars
- }
- // If the session ID wasn't sent, generate one.
- // Then be sure to send it to the client with the response.
- if (sessionid == null) {
- sessionid = generateSessionId();
- Cookie c = new Cookie("sessionid", sessionid);
- response.addCookie(c);
- out.println("hello new person");
- } else {
- out.println("welcome back" + sessionid);
- }
Add Comment
Please, Sign In to add comment