Java cookie operation details

Source: Internet
Author: User
Tags set cookie

1. Set Cookie

The Code is as follows:
Cookie cookie = new Cookie ("key", "value ");

Cookie. setMaxAge (60 );
 

Set the 60-second lifetime. If it is set to a negative value, it is the browser process Cookie (saved in memory). If it is disabled, the browser becomes invalid.

The Code is as follows:

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

Set the Cookie Path. If not set, it is the current path (for Servlet, It is the url-pattern path part of the Servlet configured in request. getContextPath () + web. xml ).

The Code is as follows:
Response. addCookie (cookie );
 

2. Read Cookie

This method can read all Cookie objects in the current path and "direct parent path". If no Cookie exists, null is returned.

 

The Code is as follows:

Cookie [] cookies = request. getCookies ();

3. delete a Cookie

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

Cookie. setMaxAge (0 );
 

Set 0 to delete the Cookie immediately;

The Code is as follows:
Cookie. setPath ("/test/test2 ");
 

Delete the Cookie in the specified path. If this path is not set, the Cookie in the current path is deleted by default;

  

The Code is as follows: response. addCookie (cookie );

The following is a complete example.

 

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.bkjia.com/">
<Html xmlns = "http://www.bkjia.com/">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> </title>
</Head>

<Body>
<%
String username = null;
Cookie [] cookies = request. 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 ");
} Else
{
%>

<Table width = "302" border = "1">
<Form id = "form1" name = "form1" method = "post" action = "clogin. jsp">
<Tr>

<Td width = "79"> <div align = "center"> </div> </td>
<Td width = "207"> <input type = "text" name = "user" id = "user"/> </td>
</Tr>
<Tr>
<Td> <div align = "center"> </div> </td>
<Td> <input type = "text" name = "textfield2" id = "textfield2"/> </td>
</Tr>
<Tr>
<Td> <div align = "center">
</Div> </td>
<Td> <select name = "select" id = "select">
<Option value = "31536000"> one year </option>
<Option value = "120"> 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>
</Html>
 


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: assume that 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 in different paths.

B. if no Cookie with the key "key" is found in the current path, all parent paths are queried, perform the delete operation upon retrieval (only one parent path Cookie closest to the user can be deleted at a time) FF. you must specify the same path used when setting the cookie to delete and modify the cookie, and specify the path for the cookie key name, whether in uppercase, lowercase, or both sizes. IE. when the key name is lower case, if the current path is/test/test2, if it cannot be found, then query/test,/test555,/test345, if not, query/(/test555/test666 ). When key names are both case-sensitive and uppercase, if no path is specified, the current path is deleted by default and the query is not performed upwards.

C. When reading a Cookie, you can only read the Cookie in the direct parent path. If the current path is/test/test2, the key to be read is "key ". After reading the current path, read/test,/test, and read /.

D. for Java web projects, because the general Web servers (such as Tomcat or Jetty) Use Context to manage different Web applications, different paths are available for each Context, when there are multiple Web applications in a Server, be careful not to set the Cookie with Path as/, which is prone to misoperations (of course, the premise is that the domain name is the same ).

Related Article

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.