Use cookie to save session data


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import java.io.IOException;
  2. import java.io.PrintWriter;
  3. import java.io.UnsupportedEncodingException;
  4. import java.net.URLEncoder;
  5.  
  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.Cookie;
  8. import javax.servlet.http.HttpServlet;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11.  
  12. public class ShoppingCartViewerCookie extends HttpServlet {
  13.  
  14. public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
  15. res.setContentType("text/html");
  16. PrintWriter out = res.getWriter();
  17.  
  18. String sessionid = null;
  19. Cookie[] cookies = req.getCookies();
  20. if (cookies != null) {
  21. for (int i = 0; i < cookies.length; i++) {
  22. if (cookies[i].getName().equals("sessionid")) {
  23. sessionid = cookies[i].getValue();
  24. break;
  25. }
  26. }
  27. }
  28.  
  29. // If the session ID wasn't sent, generate one.
  30. // Then be sure to send it to the client with the response.
  31. if (sessionid == null) {
  32. sessionid = generateSessionId();
  33. Cookie c = new Cookie("sessionid", sessionid);
  34. res.addCookie(c);
  35. }
  36.  
  37. out.println("<HEAD><TITLE>Current Shopping Cart Items</TITLE></HEAD>");
  38. out.println("<BODY>");
  39.  
  40. // Cart items are associated with the session ID
  41. String[] items = getItemsFromCart(sessionid);
  42.  
  43. // Print the current cart items.
  44. out.println("You currently have the following items in your cart:<BR>");
  45. if (items == null) {
  46. out.println("<B>None</B>");
  47. } else {
  48. out.println("<UL>");
  49. for (int i = 0; i < items.length; i++) {
  50. out.println("<LI>" + items[i]);
  51. }
  52. out.println("</UL>");
  53. }
  54.  
  55. // Ask if they want to add more items or check out.
  56. out.println("<FORM ACTION=\"/servlet/ShoppingCart\" METHOD=POST>");
  57. out.println("Would you like to<BR>");
  58. out.println("<INPUT TYPE=SUBMIT VALUE=\" Add More Items \">");
  59. out.println("<INPUT TYPE=SUBMIT VALUE=\" Check Out \">");
  60. out.println("</FORM>");
  61.  
  62. // Offer a help page.
  63. out.println("For help, click <A HREF=\"/servlet/Help"
  64. + "?topic=ShoppingCartViewerCookie\">here</A>");
  65.  
  66. out.println("</BODY></HTML>");
  67. }
  68.  
  69. private static String generateSessionId() throws UnsupportedEncodingException {
  70. String uid = new java.rmi.server.UID().toString(); // guaranteed unique
  71. return URLEncoder.encode(uid,"UTF-8"); // encode any special chars
  72. }
  73.  
  74. private static String[] getItemsFromCart(String sessionid) {
  75. return new String[]{"a","b"};
  76. }
  77. }

URL: http://www.java2s.com/Code/Java/Servlets/Usecookietosavesessiondata.htm

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.