Cookies are the client holds the data
Session is the server-side save data
Session is actually based on cookies for the first access server, the server background will record an ID number, the information into a
The cookie named Jessionid is passed back to the browser and will be accessed by the server the next time it is accessed
Users Buy Books:
Session session = Request.getsession ();
This way, even if you close the browser and reopen it, the original purchase data will be displayed.
Cookie cookie = new Cookie ("Jsessionid", Session.getid ());
Cookie.setmaxage (30*60);//30 minutes
Cookie.setpath ("/test");///when accessing the entire Web application with cookies
Response.addcookie (cookie);
List List = (list) session.getattribute ("case");
if (list = = null) {
List = new ArrayList ();
Session.setattribute ("Case", list);
}
List.add (book);
Respinse.sendredirect (...); /redirect
==============================================================================
Show User Shopping Cart:
Here if you disable cookies. Then Request.getsession is a whole new session when you call GetAttribute
Method will report an exception because the data in the Jessionid is not available at this time. This should be overridden with a URL.
URL rewrite will be accompanied by the Jessionid pass.
list<book> list = Request.getsession (). getattribute ("case");
for (book book:list) {
Response.getWriter.out (Book.getname);
}
URL rewrite need to note: 1: To the site involved in all the URL address to rewrite, so you will not buy the same as lost.
2: When rewriting the URL to see if the session was created, only the above request.getsession (), create session, will be in the URL rewrite when the Jessionid.
Request.getsession ();
String url = "/test/servlet/buyservlet?id=" + Book.getid ();
url = response.encodeurl (URL);//Only above request.getsession (); Create session to be shipped with Jessionid when URL is rewritten.
Out.write (Book.getname () + "<a href= '" +url+ "' target= ' _blank ' > Buy </a><br/>");
3: After the cookie is disabled, close the browser, reopen it, and rewrite it with a URL to not see the original purchase of the product.
4: Details: response.encodeurl (URL); This method overrides the URL if the cookie is disabled, and the URL is not overwritten if the cookie is not disabled.