Cookie, Session, Localstorage

Source: Internet
Author: User
Tags string to json

The earliest cookie problem was mostly too small, probably 4KB, and IE6 only supported 20 cookies per domain, too few. The advantage is that we all support, but also very good support. The contents of the cookie mainly include: name, value, expiration time, path and domain. The path together with the domain constitutes the scope of the cookie.  The difference between the cookie mechanism and the session mechanism specifically, the cookie mechanism uses a scheme that maintains state on the client side, while the session mechanism uses a scenario that maintains state on the server. At the same time, we also see that the session mechanism may need to use the cookie mechanism to achieve the purpose of preserving the identity, because the scenario in which the server-side holds the state needs to be stored on the client side, but there are actually other options. Ii. the difference between a session cookie and a persistent cookie if the expiration time is not set, the cookie disappears when the browser window is closed as long as the cookie's lifetime is the browser session. This cookie, which has a lifetime of browsing session, is referred to as a session cookie.  Session cookies are generally not saved on the hard disk but in memory.  If the expiration time is set, the browser will save the cookie to the hard disk, turn it off and open the browser again, and these cookies remain valid until the set expiration time expires. Cookies stored on the hard disk can be shared between different browser processes, such as two IE windows. For cookies stored in memory, different browsers have different ways of handling them. A cookie is a mechanism provided by the browser that provides the cookie properties of the document object to JavaScript. It can be controlled by JavaScript, not by the nature of JavaScript itself. A cookie is a file that is stored on the user's hard drive, which usually corresponds to a domain name, which is made available when the browser accesses the domain name again. Therefore, cookies can span multiple pages under a domain name, but cannot be used across multiple domain names. third, set the cookie1, each cookie is a name/value pair, you can assign the following string to Document.cookie:document.cookie= "userid=828"; 2, if you want to store more than one name/value pair at a time, you can use a semicolon plus a space (; ) Separated: document.cookie= "userid=828; Username=hulk "; 3, host name refers to a different host under the same domain, for example: www.google.com and gmail.google.com are two different host names. By default, a cookie created in one host cannot be accessed under another host, but it can be controlled by the domain parameter: document.cookie= "name=value;domain=.google.com"; This cookie is accessible to all hosts under Google.com. 4. set end date for cookies, the end date is not set, all cookies are single session cookies, which are lost after the browser is closed, in fact these cookies are stored in memory only, and no corresponding hard disk files are created. Document.cookie= "userid=828; Expiress=gmt_string "; Session life cycle session stored on the server side, generally in order to store in the server's memory (for high-speed access), Sessinon when the user access the first access to the server, it is necessary to note that only access to the JSP, servlet and other programs will be created session, Accessing only static resources such as HTML, image, and not creating a session can be called Request.getsession (true) to force a session to be generated.

  When does the session expire?

1. The server clears a session that has not been active for a long time from the server memory, and the session expires. The default expiration time for a session in Tomcat is 20 minutes.

2. Call the Invalidate method of the session.

  Session requirements for the browser:

Although the session is stored on the server and is transparent to the client, it still requires the support of the client browser for its normal operation. This is because the session needs to use a cookie as the identification mark. The HTTP protocol is stateless, and the session cannot determine whether it is the same client based on an HTTP connection, so the server sends a cookie named Jsessionid to the client browser. Its value is the ID of the session (that is, the return value of Httpsession.getid ()). The session is based on the cookie to identify whether it is the same user.

Localstorage supported scenarios such as in HTML5, the local storage is a window property, including Localstorage and Sessionstorage, from the name should be able to clearly identify the difference between the two, the former is always present in the local, The latter is only accompanied by the session, and once the window is closed it is gone. The use of the two is exactly the same, for example, Localstorage.  if (window.localstorage) { alert (' this browser supports localstorage ');} Else{ alert (' This browser does not support localstorage ');}   The way to store data is to add a property directly to Window.localstorage, such as:window.localstorage.a  or  window.localstorage["a"]. Its read, write, delete operation method is very simple, is in the way of key-value pairs exist, as follows:  localstorage.a = 3;//set A is "3" localstorage["a"] =  " SFSF ";//Set A to" SFSF ", overriding the above value Localstorage.setitem (" B "," Isaac ");//set B to" Isaac "var a1 = localstorage[" A "];//gets the value of a var a2 = localstorage.a;//gets the value of a var b = localstorage.getitem (" B ");// Get the value of B localstorage.removeitem ("C");//Clear the value of C   Here is the most recommended natural is getitem () and SetItem (), clear key value pairs using RemoveItem (). If you want to clear all key-value pairs at once, you can use clear (). In addition, HTML5 also provides a key () method that can be used when you do not know what key values are available, as follows:  var storage = window.localstorage;functIon showstorage () { for (var i=0;i<storage.length;i++) {  //key (i) get the corresponding key, then use GetItem () method to obtain the corresponding value   document.write (Storage.key (i) +  " : "  + storage.getitem ( Storage.key (i))  +  "<br>")   it is important to note that HTML5 local storage can only store local storage of string &NBSP;HTML5, and also provides a storage event, You can listen for changes to key-value pairs, using the following methods:  if (Window.addeventlistener) { window.addeventlistener ("storage", Handle_storage, FALSE);} Else if (window.attachevent) { window.attachevent ("Onstorage", Handle_storage);} Function handle_storage (e) { if (!e) {e=window.event;}  //showstorage ();}   for event variable E, is a Storageevent object, provides some useful properties, can be very good to observe the changes in key value pairs, such as the following table: The browser is not very good for this support, only ipad and Firefox support, and Firefox support is messy, E objects do not have those attributes at all. ipad support is very good, with E.uri (not e.url), Safari on the desktop is not, weird.   Currently JavaScript uses very many JSON formats, and if you want to store it locally, you can call Json.stringify () to string it directly. After reading it, call Json.parse () to convert the string to JSON format  

Cookie, session, Localstorage

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.