servlet Cookie Manipulation

Source: Internet
Author: User

Java's operation of cookies is simple, mainly introduces the establishment of cookies and read cookies, and how to set the life cycle of cookies and the path of cookies.

1, create an inanimate cookie that disappears as the browser closes, and the code is as follows

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

2, create a life cycle cookie that can set his life cycle

cookie =  new  Cookie( "cookiename" , "cookievalue" ); cookie.setMaxAge( 3600 ); //设置路径,这个路径即该工程下都可以访问该cookie 如果不设置路径,那么只有设置该cookie路径及其子路径可以访问 cookie.setPath( "/" ); response.addCookie(cookie); 

3, how to read the cookie, read the cookie code as follows

Cookie[] cookies = request.getCookies(); //这样便可以获取一个cookie数组 for (Cookie cookie : cookies) {     cookie.getName(); // get the cookie name     cookie.getValue();  // get the cookie value } 

4, encapsulation, add a cookie, we are concerned about the name,value of the cookie, the life cycle, so to encapsulate a function, of course, to pass in a response object, Addcookie () code as follows

/**   * 设置cookie   * @param response   * @param name  cookie名字   * @param value cookie值   * @param maxAge cookie生命周期  以秒为单位   */ public  static  void  addCookie(HttpServletResponse response,String name,String value, int  maxAge) {      Cookie cookie =  new  Cookie(name,value);      cookie.setPath( "/" );      if (maxAge> 0 )  cookie.setMaxAge(maxAge);      response.addCookie(cookie); } 

5, when reading a cookie, in order to facilitate our operation, we want to encapsulate a function, as long as we provide the name of the cookie, we can obtain the value of the cookie, with this idea, it is easy to think of the cookie encapsulated in the map, so the following package

/**   * 根据名字获取cookie   * @param request   * @param name cookie名字   * @return   */ public  static  Cookie getCookieByName(HttpServletRequest request,String name) {      Map<String,Cookie> cookieMap = ReadCookieMap(request);      if (cookieMap.containsKey(name))     {          Cookie cookie = (Cookie)cookieMap.get(name);          return  cookie;      }     else     {          return  null ;      }   }  /**   * 将cookie封装到Map里面   * @param request   * @return   */ private  static  Map<String,Cookie> ReadCookieMap(HttpServletRequest request)      Map<String,Cookie> cookieMap =  new  HashMap<String,Cookie>();      Cookie[] cookies = request.getCookies();      if ( null !=cookies)     {          for (Cookie cookie : cookies)         {              cookieMap.put(cookie.getName(), cookie);          }      }      return  cookieMap; }

servlet Cookie Manipulation

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.