Java application skills: explain the operation of the Cookie

Author:Anonymous    Updated:2008-3-20 12:21:40
1. Settings Cookie

Cookie cookie = new Cookie (the "key", "value");

Cookie.setMaxAge (60);

60 seconds set survival, if set to a negative value, for the browser process Cookie (memory preservation), the failure to close your browser.

Cookie.setPath ( "/ test/test2");

Cookie set path, it is not set up for the current path (for Servlet, for request.getContextPath () + web.xml configuration of the Servlet in the url-pattern path)

Response.addCookie (cookie);

2. Read Cookie

The method can read the current path and the "Father of the direct path" Cookie all objects, in the absence of any Cookie, then returned to the null

Cookie [] = cookies request.getCookies ();

3. Delete Cookie

Cookie cookie = new Cookie (the "key", null);

Cookie.setMaxAge (0);

Immediately set to 0 for the deletion of the Cookie

Cookie.setPath ( "/ test/test2");

Delete Cookie on designated trails, set up the path, delete the default path Cookie

Response.addCookie (cookie);

4. NOTE: Path of the following assumptions

Test/test2/test345/test555/test666


A. the same keys to the Cookie (Value can be the same or different) can exist in different paths.

B. Delete, if the current path under no bond is the "key" Cookie, the father of all enquiries path to the retrieval operation on the implementation of the deletion (each with its own can only delete a recent father path Cookie)

FF. Cookie settings must be specified and when to use the same path to delete cookie and cookie keys to whether capital, lowercase or mixed sizes to designated trails.

IE. Keys to lowercase, if the current path to / test/test2, if not further upward enquiries / test, and / test555, / test345, if not also on the enquiries /. (/ Test555/test666 no enquiries)

Keys or mixed capital case, do not specify the default path to delete the current path, and not upward enquiries.

C. read Cookie father can only read direct path Cookie.

If the current path to / test/test2 to read key "key." After reading the current path, but also read / test, and / reading test, but also read /

D. do in the Java web item, the general Web server (such as Jetty or Tomcat) are used to manage different Context Web Application, such Context is different for each of the Path,

In a Server in a number of Web Application should be especially careful not to set Path to / the Cookie, easy to misuse. (Of course, the premise is the same domain)



1. Settings Cookie

Cookie cookie = new Cookie (the "key", "value");

Cookie.setMaxAge (60);

60 seconds set survival, if set to a negative value, for the browser process Cookie (memory preservation), the failure to close your browser.

Cookie.setPath ( "/ test/test2");

Cookie set path, it is not set up for the current path (for Servlet, for request.getContextPath () + web.xml configuration of the Servlet in the url-pattern path)

Response.addCookie (cookie);

2. Read Cookie

The method can read the current path and the "Father of the direct path" Cookie all objects, in the absence of any Cookie, then returned to the null

Cookie [] = cookies request.getCookies ();

3. Delete Cookie

Cookie cookie = new Cookie (the "key", null);

Cookie.setMaxAge (0);

Immediately set to 0 for the deletion of the Cookie

Cookie.setPath ( "/ test/test2");

Delete Cookie on designated trails, set up the path, delete the default path Cookie

Response.addCookie (cookie);

4. NOTE: Path of the following assumptions

Test/test2/test345/test555/test666


A. the same keys to the Cookie (Value can be the same or different) can exist in different paths.

B. Delete, if the current path under no bond is the "key" Cookie, the father of all enquiries path to the retrieval operation on the implementation of the deletion (each with its own can only delete a recent father path Cookie)

FF. Cookie settings must be specified and when to use the same path to delete cookie and cookie keys to whether capital, lowercase or mixed sizes to designated trails.

IE. Keys to lowercase, if the current path to / test/test2, if not further upward enquiries / test, and / test555, / test345, if not also on the enquiries /. (/ Test555/test666 no enquiries)

Keys or mixed capital case, do not specify the default path to delete the current path, and not upward enquiries.

C. read Cookie father can only read direct path Cookie.

If the current path to / test/test2 to read key "key." After reading the current path, but also read / test, and / reading test, but also read /

D. do in the Java web item, the general Web server (such as Jetty or Tomcat) are used to manage different Context Web Application, such Context is different for each of the Path,

In a Server in a number of Web Application should be especially careful not to set Path to / the Cookie, easy to misuse. (Of course, the premise is the same domain)
Previous:Java Web site to the request sent POST
Next:Timing implementation of the mandate of three methods
User Reviews
Related Articles
Recommended article
AD