使用cookie保存会话数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用cookie保存会话数据相关的知识,希望对你有一定的参考价值。

  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. }

以上是关于使用cookie保存会话数据的主要内容,如果未能解决你的问题,请参考以下文章

JavaWeb会话管理:Cookie

CodeIgniter 如何知道 cookie 保存了有效的会话数据?

会话过程保存数据对象cookie和session

状态管理之cookie使用及其限制session会话

使用 Angular 4.3 保存会话 cookie

会话管理——cookie和session技术