Dayun Liu

October 14, 2008

Java for the operation of Cookie Detailed

Filed under: Uncategorized — ld624 @ 6:15 am
Tags: , , , , ,

Java for the operation of Cookie Detailed:

1. Set Cookie

Cookie cookie = new Cookie ( “key”, “value”);

Cookie.setMaxAge (60);

60 seconds set survival, if set to negative, compared with the process of the browser Cookie (to preserve memory), closed the browser expire.

Cookie.setPath ( “/ test/test2″);

Cookie set the path, if not set up for the current path (for example Servlet for request.getContextPath () web.xml in the configuration of the Servlet the url-pattern part of the path)

Response.addCookie (cookie);

2. Read Cookie

This method can read the current path, as well as the “Father of the direct path of” all the objects Cookie, Cookie is no, then return null

Cookie [] cookies = request.getCookies ();

3. Delete Cookie

Cookie cookie = new Cookie ( “key”, null);

Cookie.setMaxAge (0);

Is set to 0 to immediately remove the Cookie

Cookie.setPath ( “/ test/test2″);

Remove the designated path on the Cookie, do not set the path for the default to delete the current path of the Cookie

Response.addCookie (cookie);

4. Note: the assumption that the path is structured as follows

Test/test2/test345/test555/test666

a. name of the same key Cookie (value can be the same or different) can exist in different paths.

b. delete, if not under the current path of the keys for the “key” the Cookie, is the father of all inquiries path to retrieve deleted on the implementation of the operation (each can only delete a father and his recent path Cookie)

FF. To be designated with cookie settings using the same path to delete cookie, the cookie and key members regardless of capitalization, or small size of the hybrid should be designated trails.

IE. Keys were lower case, if the current path / test/test2, If you can not find re-up inquiries / test, / test555, / test345, if it can not find on the query /. (/ test555/test666 not query)

Key case of mixed or upper case, you do not specify the default path to delete the current path, and not up inquiries.

c. read Cookie can only be read when the direct path of the father of Cookie.

If the current path / test/test2, to read the keys for the “key”. After reading the current path, but also read / test, / test reading, but also read /

d. do in Java’s web project, due to the general Web server (such as Tomcat or Jetty) are used to manage the Context of different Web Application, so the Context of each have different Path,

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

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.