Session and Cookies 2
编程技术  /  houtizong 发布于 3年前   83
转载自:http://www.java-programming.info/tutorial/pdf/csajsp2/08-Session-Tracking.pdf
http://www.java2s.com/Code/Java/Servlets/Usecookietosavesessiondata.htm
HttpSession session = request.getSession();synchronized(session) {SomeClass value =(SomeClass)session.getAttribute("someID");if (value == null) { value = new SomeClass(...);}doSomethingWith(value);session.setAttribute("someID", value);}
HttpSession Methods:
Extracts a previously stored value from a session object. Returns null if no value is associated with given name.
implement HttpSessionBindingListener.
getId
Returns the unique identifier.
isNew
Determines if session is new to client (not to page)
Gets or sets the amount of time session should go without access before being invalidated
Use cookie to save session data:
import java.io.IOException;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ShoppingCartViewerCookie extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); String sessionid = null; Cookie[] cookies = req.getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (cookies[i].getName().equals("sessionid")) { sessionid = cookies[i].getValue(); break; } } } // 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); res.addCookie(c); } out.println("<HEAD><TITLE>Current Shopping Cart Items</TITLE></HEAD>"); out.println("<BODY>"); // Cart items are associated with the session ID String[] items = getItemsFromCart(sessionid); // Print the current cart items. out.println("You currently have the following items in your cart:<BR>"); if (items == null) { out.println("<B>None</B>"); } else { out.println("<UL>"); for (int i = 0; i < items.length; i++) { out.println("<LI>" + items[i]); } out.println("</UL>"); } // Ask if they want to add more items or check out. out.println("<FORM ACTION=\"/servlet/ShoppingCart\" METHOD=POST>"); out.println("Would you like to<BR>"); out.println("<INPUT TYPE=SUBMIT VALUE=\" Add More Items \">"); out.println("<INPUT TYPE=SUBMIT VALUE=\" Check Out \">"); out.println("</FORM>"); // Offer a help page. out.println("For help, click <A HREF=\"/servlet/Help" + "?topic=ShoppingCartViewerCookie\">here</A>"); out.println("</BODY></HTML>"); } 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 } private static String[] getItemsFromCart(String sessionid) { return new String[]{"a","b"}; }}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接