Java Cookie Actions in detail

Source: Internet
Author: User

1. Set Cookies

The code is as follows
Cookie cookie = new Cookie ("Key", "value");

Cookie.setmaxage (60);

Sets a 60-second lifetime, and if set to a negative value, the browser process cookie (saved in memory) is invalidated when the browser is closed.

The code is as follows

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

Sets the cookie path, not set as the current path (for the servlet, the Url-pattern path portion of the servlet configured in Request.getcontextpath () + web.xml).

The code is as follows
Response.addcookie (cookie);

2. Read cookies

The method can read the current path and all the cookie objects of the direct parent path, or null if there are no cookies.

 

The code is as follows

cookie[] cookies = request.getcookies ();

3. Delete Cookies

The code is as follows
Cookie cookie = new Cookie ("key", null);

Cookie.setmaxage (0);

Set to 0 to delete the cookie immediately;

The code is as follows
Cookie.setpath ("/test/test2");

Deletes the cookie on the specified path, and does not set the path by default to delete the current path cookie;

  

The code is as follows Response.addcookie (cookie);

The following is a complete example to illustrate

The code is as follows
<%@ page contenttype= "text/html; Charset=utf-8 "language=" java "import=" java.sql.* "errorpage=" "%>"
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.3lian.com/" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>

<body>
<%
    String username = null;
    cookie[] cookies = reques T.getcookies ();
    if (cookies!=null)
    {
        for (int i=0;i<cookies.length;i++)
        {
             if ("Cookies_user". Equals (Cookies[i].getname ()))
             {
                 username = Cookies[i].getvalue ();//cookies_user}
        }
       
         if ("Onepc". Equals (username))
        {
             out.println ("Hello");
&NBSP;&NBsp;     }else
        {
        %>
       
<table width= "302" border= "1"
  <form id= "Form1" Name= "Form1" method= "Post" action= "clogin.jsp"
  <tr>

    <td width= "><div" align= "center" ></DIV></TD>
     <TD width= "207" ><input type= "text" name= "user" id= "user"/></TD>
  </tr>
& nbsp <tr>
    <td><div align= "center" ></DIV></TD>
     <td><input type= "text" name= "textfield2" id= "Textfield2"/></TD>
  </tr>
& nbsp <tr>
    <td><div align= "center" >     
     </div></td>
    <td><select name= "Select" id= "select"
      <option value= "31536000" >one year</option>
       <option value= ">two" MIN</OPTION>
      </select></td
  </TR>
  <TR>
    <TD Colspan= "2" ><label>
      <input type= "Submit" name= "button" id= "button" Value= ""/>
    </label></td>

</tr>
</form>
</table>

<%
}

}

%>


</body>


login.jsp

The code is as follows

<%
String user = Request.getparameter ("user");
Cookie cookie = new Cookie ("Cookies_user", user);
Cookie.setmaxage (120);
Response.addcookie (cookie);
Response.sendredirect ("cindex.jsp");

%>


4. Note: Suppose the path structure is as follows

  

The code is as follows
test/test2/test345/test555/test666

A. Cookies with the same key name (values can be the same or different) can exist under different paths.

B. At the time of deletion, if there is no cookie with the key "key" under the current path, query all the parent paths and retrieve the delete operation (only one with its closest parent path cookie at a time) FF. You must specify the same path to use when setting cookies to remove the cookie, and the cookie's key name, regardless of uppercase, lowercase, or mixed size, specifies the path. IE. When the key name is lowercase, if the current path is/TEST/TEST2, if you can not find the/test,/test555,/test345, if you can not find the query/(/test555/test666 do not query). When a key name is mixed or uppercase, the current path is deleted by default without specifying a path and is not queried up.

C. Cookies can only be read from a direct parent path when reading cookies. If the current path is/test/test2, the key to read is "key". After the current path is read, also read the/test,/test read/.

D. When doing Java Web projects, because a typical Web server (such as Tomcat or jetty) uses the context to manage different Web application, there is a different path for each context, and there are multiple web Application should be particularly cautious, do not set path to/cookies, easy to operate (if the premise is the same domain name).

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.