1 What is a cookie
The browser and the WEB server communicate using the HTTP protocol. When a user issues a page request, the WEB server simply responds and then closes the connection with the user. Therefore, when a request is sent to a WEB server, regardless of whether it is the first visit, the server will treat it as the first time. This is a bad thing that can be imagined. To make up for this flaw, Netscape has developed a cookie, an effective tool to save the identification information of a certain user, so people nicknamed it "sweet cookies". Cookies is a means by which WEB servers store information on visitors' hard disk through a browser: Netscape Navigator uses a local file called cookies.txt to save cookie information received from all sites; while IE browser saves cookie information in a directory similar to C:/windows/cookies. When the 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 attributes of cookies
max-age Specifies the lifetime of the Ccookie (in seconds)! By default, the value of the Cookie only exists during the browser's session, and these values disappear when the user exits the browser!
path Specifies the web page associated with the cookie. By default, the cookie is associated with the web page that creates it, as well as the web page in the same directory as the web page and the subdirectories in the directory.
domain Setting the access domain For example: the server located at order.example.com needs to read the cookie set by catalog.example.com. Here you need to introduce the domain attribute. Assuming that the cookie created by the page located at catalog.example.com sets its path attribute to "/" and the domain attribute to ".example.com", then all web pages located at "catalog.example.com" and all web pages located at "orders.example.com" and all web pages located at "orders.example.com" and all other servers located at example.com domain can access this cookie. If the domain value of the cookie is not set, the default value of this attribute is the host name of the server where the web page where the cookie is created. Note: The domain of a cookie cannot be set to a domain outside the domain where the server resides.
seure Specifies how the value of a cookie is transmitted on the network
3 Java Cookie Operation
Create a Cookie
// new Cookie object, key-value pair is the parameter Cookie cookie = new Cookie("key", "cookie value"); If the value of the cookie contains Chinese, the cookie needs to be encoded, otherwise garbled code will occur. Use URLEncoder.encode("cookie value","utf-8");// Set the maximum survival time of the cookie, in seconds, if the negative number is the browser process, close the browser cookie disappears cookie.setMaxAge(*24*60*60); // One day // Add the cookie to the Response to make it take effect response.addCookie(cookie); // After the addCookie, if the cookie with the same name already exists, the latest overwrites the old cookieNote: In Struts, you can use ServletActionContext.getResponse() to get the respone object
Read cookies
Reading cookies can only get all cookies from the request and then iterate in a loop.
In Struts, you can use ServletActionContext.getRequest() to obtain the request object
// Get the cookie from the request, and you get an array of cookies Cookie[] cookies = request.getCookies(); // Then iterate if (cookies != null && cookies.length > 0) { // If you have not set a cookie, it will return null for (Cookie cookie : cookies) {...} } Delete cookies
If you delete a cookie, you only need to 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.getName(); // Find the cookie that needs to be deleted if (name.compareTo("target-key") == 0) { // Set the lifetime to 0 cookie.setMaxAge(0); // Set back to the response.addCookie(cookie); } } }The setPath method of 4 cookies is used:
Normal cookies can only be shared in one app, i.e. a cookie can only be obtained by the app that created it.
1. The method can be shared within the same application server: set cookie.setPath("/");
There are two applications under the native tomcat/webapp: webapp_a and webapp_b.
1) It turns out that the cookie set under webapp_a cannot be obtained under webapp_b. The path is the path of the application that generates the cookie by default.
2) If you set a cookie under webapp_a, add a cookie.setPath("/"); or cookie.setPath("/webapp_b/");
You can get the cookie set by cas under webapp_b.
3) The parameters here are relative to the root directory of the application folder stored by the application server (such as the webapp under tomcat), so cookie.setPath("/"); after that, cookie can be shared in all applications under the webapp folder, and cookie.setPath("/webapp_b/");
It means that the cookie set by the cas application can only be obtained under the webapp_b application, and even the webapp_a application that generates this cookie cannot be used.
4) When setting cookie.setPath("/webapp_b/jsp") or cookie.setPath("/webapp_b/jsp/"), cookie can only be obtained under webapp_b/jsp, but cookies cannot be obtained outside the jsp folder.
5) Set cookie.setPath("/webapp_b"); means that cookies can only be used under webapp_b, so that cookies cannot be obtained under webapp_a that generates cookies.
6) When there are multiple cookies.setPath("XXX"); statements, the last one shall prevail.
5 cookie.setDomain method design cross-domain sharing
The domain where machine A is located: home.langchao.com, A has application webapp_a
The domain where the B machine is located: jszx.com, B has application webapp_b
1) When setting cookies under webapp_a, add cookie.setDomain(".jszx.com"); so that you can get cookies under webapp_b.
2) When entering url to access webapp_b, you must enter the domain name to resolve. For example, when entering: http://lc-bsp.jszx.com:8080/webapp_b in machine A, you can get the cookie set by webapp_a on the client, while when entering: http://localhost:8080/webapp_b, you can't get the cookie.
3) Cookie.setDomain(".jszx.com"); can also be shared under the default home.langchao.com
The above cliché talk about the use of cookies in Java is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.