Java operation cookie !!!

Source: Internet
Author: User
Tags set cookie

 

Java's operations on cookies are relatively simple. This section describes how to create and read cookies, and how to set the cookie lifecycle and Cookie Path.

 

Create a non-life cycle cookie, that is, the cookie disappears when the browser is closed. The Code is as follows:

 
1 HttpServletRequest request  
2 HttpServletResponse response
3 Cookie cookie = new
Cookie("cookiename","cookievalue");
4 response.addCookie(cookie);

 

Create a cookie with a lifecycle. You can set its lifecycle.

 
1 cookie = new
Cookie("cookiename","cookievalue");
2   
3 cookie.setMaxAge(3600);

Unit: seconds !!!!!!!

4   
5 // Set the path, that is, the cookie can be accessed under the project. If no path is set, only the Cookie Path and its sub-path can be accessed.
6   
7 cookie.setPath("/");
8 response.addCookie(cookie);

 

The following describes how to read a cookie and read the cookie Code as follows:

 
1 Cookie[] cookies = request.getCookies();// Obtain a cookie array.
2 for(Cookie cookie : cookies){
3     cookie.getName();// get the cookie name
4     cookie.getValue();
// get the cookie value
5 }

 

The above is the basic operation for reading and writing cookies. In practice, we 'd better encapsulate it. For example, to add a cookie, we are concerned with the name, value, and lifecycle of the cookie. Therefore, we need to encapsulate a function and pass in a response object, the addcookie () code is as follows:

01 /**
02  * Set cookie
03  * @param response
04  * @ Param name Cookie name
05  * @ Param value cookie value
06  * @ Param maxage cookie lifecycle in seconds
07  */
08 public
static
void addCookie(HttpServletResponse response,String name,String value,int
maxAge){
09     Cookie cookie =
new Cookie(name,value);
10     cookie.setPath("/");
11     if(maxAge>0)  cookie.setMaxAge(maxAge);
12     response.addCookie(cookie);
13 }

 

When reading cookies, we want to encapsulate a function to facilitate our operations. As long as we provide the cookie name, we can get the cookie value. With this idea, it is easy to encapsulate the cookie into the map, so the following encapsulation is performed.

 
01 /**
02  * Get cookie by name
03  * @param request
04  * @ Param name Cookie name
05  * @return
06  */
07 public
static
Cookie getCookieByName(HttpServletRequest request,String name){
08     Map<String,Cookie> cookieMap = ReadCookieMap(request);
09     if(cookieMap.containsKey(name)){
10         Cookie cookie = (Cookie)cookieMap.get(name);
11         return
cookie;
12     }else{
13         return
null;
14     }   
15 }
16   
17   
18   
19 /**
20  * Encapsulate cookies in Map
21  * @param request
22  * @return
23  */
24 private
static
Map<String,Cookie> ReadCookieMap(HttpServletRequest request){  
25     Map<String,Cookie> cookieMap =
new HashMap<String,Cookie>();
26     Cookie[] cookies = request.getCookies();
27     if(null!=cookies){
28         for(Cookie cookie : cookies){
29             cookieMap.put(cookie.getName(), cookie);
30         }
31     }
32     return
cookieMap;
33 }

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.