Cliché Java use of cookies _java

Source: Internet
Author: User
Tags cas set cookie tomcat

1 What is a cookie

The browser communicates with the Web server using the HTTP protocol, and when a user makes a page request, the Web server simply responds, and then closes the connection to the user. So when a request is sent to a Web server, whether it is the first visit or not, the server treats it as if it was the first time, so the bad things can be imagined. To make up for this flaw, Netscape has developed cookies as an effective tool for saving information about a user's identity, so people are nicknamed "cookies". Cookies are a way for a Web server to store information on a visitor's hard disk through a browser: Netscape Navigator uses a local file named Cookies.txt to save cookie information that is received from all sites, while IE browsers store cookie information in a directory similar to C:\windows\cookies. When a user accesses a site again, the server will require the browser to look up and return the previously sent cookie information to identify the user.

2 Cookies with four properties

MAX-AGE Specifies the lifetime of the CCookie, in seconds! By default, the value of the cookie exists only during the session of the browser, and the values disappear when the user exits the browser!

path Specifies the Web page that is associated with the cookie. By default, a cookie is associated with a page that creates it and a page that is in the same directory as the page and a subdirectory under that directory.

Domain Settings Access domains For example, the server located in Order.example.com is to read the cookies for the catalog.example.com settings. This is to introduce the domain attribute, Suppose that the cookie on the page created by catalog.example.com sets its Path property to "/" and sets the domain property to ". example.com", then all of the "catalog.example.com" Web page and all Web pages located on "orders.example.com" and all other servers located in the example.com domain can access this cookie. If you do not set the domain value of the cookie,   The default value for this property is the hostname of the server that contains the Web page where the cookie was created. Note: You cannot set a cookie's domain to a domain other than the server's domain.

SEURE specifies how the value of a cookie is transferred on the network

3 Java Cookie Operations

Creating cookies

New Cookie object, key value pair for parameter 
cookie cookie = new Cookie ("Key", "value of Cookie"); 
   If the value of the cookie contains Chinese, you need to encode the cookie, otherwise it will produce garbled, using
      urlencoder.encode ("The value of the cookie", "Utf-8");


Set the maximum cookie lifetime, in seconds, negative words for the browser process, close the browser cookie disappears 
cookie.setmaxage (*24*60*60);/ 
/day///Add cookies to response , making it effective 
response.addcookie (cookie);//addcookie, if a cookie with the same name already exists, the latest overwrite old cookie

Note: You can use Servletactioncontext.getresponse () to get Respone objects in struts

Read cookies

The read cookie can only get all cookies from the request, and then loop through the iteration.

You can use Servletactioncontext.getrequest () in struts to get the request object

Get a cookie from request and get a cookie array 
cookie[] cookies = request.getcookies (); 
Then iterate if 
(cookies!= null && cookies.length > 0) {//If no cookie is set, returns a null for 
  (cookie Cookie:coo Kies) {...}} 
}

Delete Cookies

To delete a cookie, simply set the lifetime of the cookie to 0

cookie[] cookies = request.getcookies (); 
if (cookies!= null && cookies.length > 0) {for 
  (Cookie cookie:cookies) { 
    String name = Cookie.getna Me (); 
    Find the cookie that needs to be deleted 
    (Name.compareto ("target-key") = = 0) { 
      //Set lifetime of 0 
      cookie.setmaxage (0); 
      Set back into effect 
      Response.addcookie (cookie) in response;}} 
 

The SetPath method of 4 cookies is used:

A normal cookie can only be shared in one application, that is, a cookie can only be obtained by the application that created it.

1. Can be shared within the same application server method: Set Cookie.setpath ("/");

This machine Tomcat/webapp has two applications below: Webapp_a and Webapp_b,

1 The cookies originally set under webapp_a are not available under Webapp_b, and path defaults to the way in which the cookie is generated.

2 Add a Cookie.setpath ("/") or Cookie.setpath ("/webapp_b/") if the cookie is set under webapp_a;

You can get a cookie for CAS settings under Webapp_b.

3 The parameters here are relative to the root directory where the application server holds the application folder (such as WebApp under Tomcat), so Cookie.setpath ("/"), and then you can share cookies in all applications under the WebApp folder. and Cookie.setpath ("/webapp_b/");

Refers to a CAS application settings cookie that 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/"), only cookies can be obtained under webapp_b/jsp, in Webapp_ B below but no cookies are available outside of the JSP folder.

5) Setting Cookie.setpath ("/webapp_b"), which means that cookies can be used under webapp_b, so that cookies cannot be obtained under webapp_a of the application that generated the cookie.

6 A number of Cookie.setpath ("XXX"), the statement of the time, the function of the last one to prevail.

5 Cookie.setdomain method design cross domain sharing

The domain of a machine: Home.langchao.com,a has application webapp_a

b machine in the domain: Jszx.com,b has applied webapp_b

1 Add Cookie.setdomain (". jszx.com") when setting cookies under Webapp_a, so that cookies can be taken under webapp_b.

2 Enter the URL to access webapp_b, you must enter a domain name to resolve. For example, in a machine input: http://lc-bsp.jszx.com:8080/webapp_b, you can get webapp_a on the client set cookies, and B machine access to the application of the machine, input: http://localhost:8080/ Webapp_b can not get cookies.

3 The Cookie.setdomain (". jszx.com") is set, and can also be shared under the default home.langchao.com

The above Cliché Java cookie is used in small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.