The use of cookie in java is a cliche

  • 2020-05-19 04:57:11
  • OfStack

1 what is cookie

The browser communicates with the WEB server using the HTTP protocol. When a user makes a page request, the WEB server simply responds and then closes the connection with the user. Therefore, when a request is sent to the WEB server, the server will treat it as if it is the first visit, whether it is the first visit or not. To make up for this, Netscape developed cookie, an effective tool for saving a user's identifying information, hence the nickname "the cookie." cookies is a means by which an WEB server stores information on a visitor's hard drive via the browser: Netscape Navigator USES a local file called cookies.txt to store Cookie information received from all sites; The IE browser saves the Cookie information in a directory similar to C:\windows\cookies. When a user visits a site again, the server asks the browser to look up and return the Cookie message it sent earlier to identify the user.

2 four properties of cookie

max-age specifies the lifetime (in seconds) of Ccookie! By default,Cookie values exist only for the duration of the browser's session, and disappear when the user exits the browser!

path specifies a web page that is associated with Cookie for 1 day. By default,cookie associates the web page that created it with a web page in the same directory and a subdirectory in the same directory.

For example, domain sets the access domain: The server located at order.example.com will read cookie set by catalog.example.com. The domain property will be introduced here, assuming that it is created by the page located at catalog.example.com If cookie sets its path property to "/" and domain property to ".example.com ", then all web pages located in "catalog.example.com" and all web pages located in "orders.example.com" and all other servers located in the example.com domain can access cookie. If the domain value of cookie is not set, the default value of this property is to create cookie's web The hostname of the server on which the page is located. Note: you cannot set an cookie domain to a domain outside of the server's domain.

seure specifies how to transfer the value of cookie over the network

3 java Cookie operations

Create Cookie


// new1 a Cookie object , The key-value pairs are parameters  
Cookie cookie = new Cookie("key", "cookie the value value "); 
    if cookie When the value contains Chinese, it needs to be correct cookie Carry on the code, otherwise can produce the garbled code, use 
      URLEncoder.encode("cookie the value value ","utf-8");


//  Set up the Cookie Maximum survival time , In seconds , Negative Numbers are the browser process , Close the browser Cookie disappear  
cookie.setMaxAge(*24*60*60); // 1 day  
//  will Cookie Added to the Response In the , Into law  
response.addCookie(cookie); //addCookie After that, if the same name already exists cookie , then the latest covers the old cookie

Note: ServletActionContext.getResponse () can be used in Struts to get respone objects

Read cookie

Reading Cookie can only get all Cookie from request, and then iterate.

ServletActionContext.getRequest () can be used in Struts to obtain request objects


//  from request To derive Cookie, Get is 1 a Cookie An array of  
Cookie[] cookies = request.getCookies(); 
//  And then iterate through it  
if (cookies != null && cookies.length > 0) { // If it hasn't been set Cookie Returns the null 
  for (Cookie cookie : cookies) {...} 
}

Delete cookie

To delete Cookie, simply set the lifetime of Cookie to 0


Cookie[] cookies = request.getCookies(); 
if (cookies != null && cookies.length > 0) { 
  for (Cookie cookie : cookies) { 
    String name = cookie.getName(); 
    //  Find what needs to be deleted Cookie 
    if (name.compareTo("target-key") == 0) { 
      //  Set the lifetime to 0 
      cookie.setMaxAge(0); 
      //  Set back Response In effect  
      response.addCookie(cookie); 
    } 
  } 
} 

4 the setPath method of cookie is used:

Normal cookie can only be Shared in one application, that is, one cookie can only be obtained by the application that created it.

1. Methods can be Shared within the same application server: set cookie.setPath ("/");

The native tomcat/webapp has two applications below: webapp_a and webapp_b,

1) cookie, which was originally set under webapp_a, cannot be obtained under webapp_b. path is the default path to generate the application of cookie.

2) if cookie is set under webapp_a, add 1 cookie.setPath ("/"); Or cookie. setPath ("/webapp_b/");

You can get cookie set by cas under webapp_b.

3) the parameter here is relative to the root directory of the folder where the application server holds the application (e.g. webapp below tomcat), so cookie.setPath ("/"); After that, all applications in the webapp folder can share cookie, while cookie.setPath ("/webapp_b/");

The cookie set by the cas application can only be obtained under the webapp_b application, even if the webapp_a application that produces the cookie is not available.

4) when setting cookie.setPath ("/webapp_b/jsp") or cookie.setPath ("/webapp_b/jsp/"), you can get cookie only under webapp_b/ jsp, below webapp_b but not outside the jsp folder.

5) set cookie.setPath ("/webapp_b"); , means that cookie can be used under webapp_b, so that cookie cannot be obtained under webapp_a, which produces cookie

6) there are several cookie.setPath("XXX"); When the statement, the effect of the last 1 shall prevail.

5. cookie.setDomain method designs cross-domain sharing

A machine's domain: home. langchao. com, webapp_a A have application

B: jszx.com, B, webapp_b

1) when setting cookie under webapp_a, add cookie.setDomain (".jszx.com "); , so you can get cookie under webapp_b.

2) enter url to access webapp_b, you must enter the domain name to resolve. For example in A machine input: http: / / lc - bsp jszx. com: 8080 / webapp_b, can obtain webapp_a cookie in client Settings, and access to the application of the machine, machine B input: http: / / localhost: 8080 / webapp_b is not cookie can be obtained.

3) cookie.setDomain (".jszx.com "); , can also be Shared under the default home.langchao.com


Related articles: