Java Cookie Detailed

Source: Internet
Author: User

cookies are generated by the server and sent to User-agent (typically a browser), and the browser saves the key/value of the cookie to a text file in a directory, The next time you request the same Web site, the cookie is sent to the server (provided the browser is set to enable cookies). The cookie name and value can be defined by the server side and can be written directly to the JSP for Jsessionid to mark a session, so the server can know if the user is a legitimate user and if it needs to log in again. The server can set or read the information contained in the cookie to maintain the status of the user and the server session.

Cookies are client technology, and HttpSession is server-side technology.

Cookies in Java are detailed:

1. What is a cookie?

A small message that is written by the server to the browser. Saved by the browser.

The cookie information stored by the client can be brought to the server again.

Cookie class: Javax.servlet.http.Cookie

2. Properties of Cookies:

  name: Required

  value: required

  comment: Optional. Comments

  path: Optional, if the path is not set, only the URI that sets the cookie and its sub-path can be accessed

The access path to the program that wrote the cookie is: Http://localhost:8080/JavaWeb/servlet/CookieDemo

Where: localhost is the domain name;/javaweb/servlet is the path of the current cookie

if the URI of the visited address contains the path to the cookie, that is, Uri.startwith (the path to the cookie), true, the client brings the cookie to the server.

For example, the path of cookies stored by the browser is/javaweb
The address now visited is: Http://localhost:8080/JavaWeb/servlet/CookieDemo with the cookie
The address now visited is: Http://localhost:8080/JavaWeb/CookieDemo with the cookie

if the path to the cookie stored by the browser is/javaweb/servlet/
The address of the visit is: Http://localhost:8080/JavaWeb/servlet/CookieDemo with the cookie
The address of the visit is: Http://localhost:8080/JavaWeb/CookieDemo without the cookie

if the path of a cookie is set to/javaweb, it meansBrowser Accessall resources under the current application will be brought to the server with that cookie.


  domain: Optional. The domain name of the website to which the cookie belongs. (apache.org) Default value.

  Maximum Age: Optional. Not set is the session process (in memory of the browser). Unit is seconds
If it is 0, the description is to be deleted.

  version: Optional.

3. How to write cookies to the client:

HttpServletResponse object. Addcookie (Javax.servlet.http.Cookie object) (that is, write a response message header: Set-cookie:cookie information)

The cookie API in the servlet specification provides setmaxage SetPath SetDomain and other methods, can control the state of the cookie

Features: A browser can save up to 20 cookies for a Web site, and a maximum of 300 cookies, each cookie cannot exceed 4KB (scarce) in length. It's just a rule, but different browser implementations are different.

public void doget (HttpServletRequest request, httpservletresponse response)            throws Servletexception, IOException {        Cookie Cookie=new Cookie ("name", "Tom");        Set maximum age        cookie.setmaxage (+);        Sets the cookie path to the current project path        Cookie.setpath (Request.getcontextpath ());        Add a cookie        response.addcookie (cookie);    }



4. How the server obtains the cookie from the client:

using Serlvet or JSP scriptlet in Java, you can write cookies to the browser side, Similarly, cookie information can be read using Servet or JSP scriptlet

The cookie API in the servlet specification also has methods such as Getmaxage GetPath GetDomain, which can obtain a corresponding state.

However, there is a problem here:

When reading a cookie, it is found that except for the key and value of the cookie, the other value gets null.

The reason is simple:when the cookie is sent from the server to the client, the information is complete andthe cookie is sent from the client side to the service side, and the information is left with key and value. (due to domain's incorrect cookie, path not cookie, expired cookie, the client will not send it)

So why is there a corresponding get method in Java? The method is actually used when the cookie is generated and not yet sent to the client.

The server side passes the HttpServletRequest object. GetCookies () to get an array of cookies.

public void doget (HttpServletRequest request, httpservletresponse response)            throws Servletexception, IOException {        PrintWriter out=response.getwriter ();        Cookie[] Cookies=request.getcookies ();        if (cookies!=null) {for            (Cookie cookie:cookies) {                String name=cookie.getname ();                String Value=cookie.getvalue ();                Out.write (name+ "=" +value);}}    


5. How to differentiate cookies:

not by name, should be through Domain+path+name to differentiate.


Blog Park Blog: Small basket under the flat

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Cookie Detailed

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.