Cookie Knowledge Point Summary

Source: Internet
Author: User

What's the problem? What are the technologies? How to solve?

1. Cookies

1) A mechanism to complete a reply-to-trace: The scenario in which HTTP state information is saved at the client

2) A cookie is a small text file that is passed to the browser by the Web server in the HTTP response message header when the browser accesses a resource of the Web server.

3) Once a cookie is saved by the Web browser, it will be passed back to the Web server in the HTTP request header each time it accesses the Web server.

4) Bottom-up principle: The Web server sends the cookie information to the browser by adding the Set-cookie response Header field to the HTTP response message, and the browser passes the cookie back to the Web server by adding the cookie Request header field in the HTTP request message total.

2.Cookie related APIs:

1) Write COOKIE//1 to the client browser. Create a cookie object cookie cookie = new Cookie (name, "" "Shindo");//2.setmaxage: Sets the maximum age of the cookie, in seconds, if 0 , which means that deleting the cookie,//immediately if it is negative indicates that the cookie is not stored and, if positive, indicates the time it was stored. Cookie.setmaxage (30);//3. Sets the scope of the cookie: You can work on the current directory and subdirectories of the current directory, but not on the top-level directory of the current directory. Cookie.setpath (Request.getcontextpath ());//4. Call a method of response to pass the cookie to the client Response.addcookie (cookie); 2) Read COOKIE//1 from the browser. Get cookiecookie[] cookies = request.getcookies (); if (cookies! = null && cookies.lenth > 0) { C0/>for () {      //2. Get Cookiede name and value      out.print (cookie.getname () + ":" + cookie.getvalue ());      Out.print ("<br>");   }    }             

3. Session Cookies and persistent cookies

1) If you do not set an expiration time, the cookie disappears when the browser window is closed, as long as the cookie is in the lifetime of the browser session. This cookie, which is the lifetime of the browser session, is referred to as a session cookie. Session cookies are generally not saved on the hard disk but in memory.

2) If the expiration time is set, the browser will save the cookie to the hard disk, turn it off and open the browser again, these cookies remain valid until the set expiration time expires.

4. The principle of using cookies is simple: Automatically log in and display information about the products you have browsed.

Cookie Knowledge Point Summary

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.