"Javaweb" Cookies and Session__java

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.