The commonplace use of cookies in Java

1 what is a cookie

The browser communicates with the web server using HTTP protocol. When a user sends 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, no matter whether it is the first visit or not, the server will treat it as the first time, which can be imagined. In order to make up for this defect, Netscape has developed cookie, an effective tool to save the identification information of a user, so people nickname it "cookie". Cookies are a means by which the web server stores information on the visitor's hard disk through the browser: Netscape Navigator uses a file called cookies Txt local files store cookie information received from all sites; IE browser saves the cookie information in a directory similar to C: \ windows \ cookies. When a user visits a site again, the server will ask the browser to find and return the previously sent cookie information to identify the user.

2 four properties of cookies

Max age specifies the lifetime of the ccookie (in seconds)! By default, cookie values only exist during the browser session. These values disappear when the user exits the browser!

Path specifies the web page associated with the cookie By default, a cookie is associated with the web page that created it, the web page in the same directory as the web page, and the subdirectories in that directory.

Domain sets the access domain. For example, it is located in order example. COM server to read catalog example. Cookies set by. Com Here, we will introduce the domain attribute, which is assumed to be located in catalog example. The cookie created by the page of COM sets its path attribute to "/" and the domain attribute to ". Example. Com, then all web pages located in" catalog. Example. Com "and all web pages located in" orders. Example. Com "and all web pages located in example Web pages on other servers in the COM domain can access this cookie If the domain value of the cookie is not set, the default value of this property is the host name of the server where the web page that created the cookie is located. Note: the domain of a cookie cannot be set to a domain other than the domain where the server is located

See specifies how cookies are transferred over the network

3. Java cookie operation

Create cookie

Note: servletactioncontext can be used in struts Getresponse() gets the response object

Read cookies

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

Servletactioncontext can be used in struts Getrequest() gets the request object

delete cookie

To delete a cookie, you only need to set the lifetime of the cookie to 0

4. The setpath method of cookie uses:

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

1. It can be shared in the same application server. Method: set cookies setPath("/");

There are two applications under native Tomcat / webapp: webapp_ A and webapp_ b,

1) Originally in webapp_ A the cookie set below is on webapp_ B not available below. Path is the path of the application that generates the cookie by default.

2) If in webapp_ A add a cookie when setting cookies below setPath("/"); Or cookies setPath("/webapp_b/");

You can use webapp_ B the cookie set by CAS is obtained below.

3) The parameters here are relative to the root directory of the folder where the application server stores the application (such as webapp under Tomcat), so the cookie setPath("/"); After that, you can share cookies in all applications under the webapp folder, and cookies setPath("/webapp_b/");

This means that cookies set by CAS applications can only be used in webapp_ B is obtained under the application, even the webapp that generates the cookie_ A application is not allowed.

4) Set cookies Setpath ("/ webapp_b / JSP") or cookie When setpath ("/ webapp_b / JSP /"), only webapp_ Cookies can be obtained under B / JSP, which can be found in webapp_ B below, but outside the JSP folder, can't get cookies.

5) Set cookies setPath("/webapp_b");, It means in webapp_ B cookies can only be used below, so you can't use webapp in the application that generates cookies_ A got the cookie below

6) There are multiple cookies setPath("XXX"); When the statement is, the last one shall prevail.

5 cookie. Design cross domain sharing with setdomain method

Domain of machine a: home langchao. COM, a has application webapp_ a

Domain of machine B: jszx COM, B has application webapp_ b

1) In webapp_ A add cookies when setting cookies below setDomain(".jszx.com");, So in webapp_ B you can get the cookie below.

2) Enter the URL to access webapp_ B, you must enter the domain name to resolve. For example, in machine a, enter: http://lc-bsp.jszx.com:8080/webapp_b , you can get webapp_ A sets a cookie on the client, while machine B accesses the application of this machine. Enter: http://localhost:8080/webapp_b Cookies cannot be obtained.

3) Cookie Set setDomain(".jszx.com");, It can also be in the default home langchao. Com share below

The above commonplace talk about the use of cookies in Java is all that Xiaobian has shared with you. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>