When a cookie is set in JSP, the corresponding cookie cannot be read and retrieved in servlet, and a lot of information is searched. The solution is solved by referring to the cookie API documentation. In fact, the solution is very simple. You only need to set the Cookie Path. In addition, if the new Cookie does not set maxage, the cookie becomes invalid after the browser is closed.
After solving this problem, I checked the cookie principle and related Java API documents as follows:
What is Cookie?
Cookie is a small piece of data stored on a user terminal (such as a browser) in HTTP and HTTPS protocols for session management. It is generally used to store user preferences, automatic logon information with low security requirements, and collect user information. It is generally stored in the specified directory of the client user's browser. Therefore, do not use cookies to store sensitive data, and it is best to encrypt cookie data.
Each Cookie has a name and a corresponding value. Cookie has several optional attributes, such as Comment comment, Path, Domain Name Domain, maxage, and version. Because the browser currently supports these attributes with bugs, do not rely too much on these attributes for maximum interoperability.
Cookies are specified on the server side and are implemented by adding fields to the HTTP response header. In Java Servlet, a cookie is added at a time through the response. addcookie method. Users' browsers are generally required to support 20 cookies for each host, and each Cookie must support at least 4 kb. Using a large number of cookies is generally not encouraging. When a browser sends a request, these cookies are added to the HTTP request header and sent to the server. On the server side, you can use request. getcookies to obtain all cookies in this request. A cookie with the same name can exist in different paths. a cookie in a specific path can only be read by this path and Its subdirectories.
1 Public Void Setdomain (string pattern );
Specifies the domain within which this cookie shocould be presented.
The form of the domain name is specified by RFC 2109.A domain name begins with a dot (.foo.com) and means that the cookie is visible to servers in a specified Domain Name System (DNS) Zone (for example, www.foo.com, but not a. B .foo.com ).By default, cookies are only returned to the server that sent them.
1 Public Void Setmaxage ( Int Expiry );
Sets the maximum age of the cookie in seconds.
APositiveValue indicates that the cookie will expire after that specified seconds have passed. Note that the value is the maximum age when the cookie will expire, not the Cookie's current age.NegativeValue means that the cookie is not stored persistently and will be deleted when the web browser exits.ZeroValue causes the cookie to be deleted.
1 Public Void Setpath (string URI );
Specifies a path for the cookie to which the client should return the cookie.
The cookie is visible to all the pages in the directory you specify, and all the pages in that directory's subdirectories. A cookie's path must include the servlet that set the cookie, for example,/catalog, which makes the cookie visible to all directories on the server under/catalog. consult RFC 2109 (available on the Internet) for more information on setting path names for cookies.
Usage:
1. Add COOKIE:
1 // Create a cookie and set the name and value.
2 Cookie = New Cookie ( " Cookiename " , " Cookievalue " );
3 // Set the cookie validity period to 2 days. If this validity period is not set, the cookie becomes invalid after the user closes the browser.
4 Cookie. setmaxage ( 60 * 60 * 24 * 2 );
5 // Set the valid path of the cookie. "/" means that the cookie can be accessed under the application;
6 // If no path is set, only the Cookie Path and its sub-path can be accessed.
7 Cookie. setpath ( " / " );
8 // Add cookie to HTTP Response
9 Response. addcookie (cookie );
2. Obtain the cookie:
1 // Obtain all cookies in this request
2 Cookie [] cookies = Request. getcookies ();
3 If (Cookies ! = Null ){
4 For (Cookie: cookies ){
5 Cookie. getname ();
6 Cookie. getvalue ();
7 }
8 }
Note:
1 do not use special characters in cookie names and values. For cookie names, rfc2109 states that names can only contain letters or numbers, but cannot contain commas, semicolons, spaces, and dollar signs. We recommend that you do not use commas, semicolons, or blank characters in cookie values.
2 if there is a problem when setting and reading cookies between JSP and Servlet, it is generally because of a path problem. In this case, the cookie. setpath is called to set the Cookie Path.