Javaweb:cookie and Sessioncookie dealing with what is a cookie
A Cookie is a text file that is stored on a client computer and retains various trace information . The Java Servlet obviously supports HTTP cookies.
The identification of the returned user consists of three steps:
- The server script sends a set of cookies to the browser. For example: Name, age, or identification number.
- The browser stores this information on the local computer for future use.
- When a browser sends any request to the WEB server, the browser sends the Cookie information to the server and the server uses that information to identify the user.
This chapter will show you how to set or reset cookies, how to access them, and how to delete them.
The Servlet Cookie processing requires encoding and decoding Chinese in the following ways:
String str = java.net.URLEncoder.encode ("Chinese", "UTF-8"); Encode string str = Java.net.URLDecoder.decode ("encoded string", "UTF-8"); Decoding
Cookie Profiling
cookies are usually set in the HTTP header information (although JavaScript can also set a cookie directly on the browser). The Servlet that sets the Cookie sends the following header information:
HTTP/1.1 200OkDate: Fri, 04 Feb 2000 21st:03:38GmtServer: Apache/1.3.9 (Unix)Php/4.0b3Set-Cookies:Name=Xyz; Expires=friday, 04-feb-07 22:03: 38 Gmt; Path=/; Domain=runoob.connection: Closecontent -type: Text/ html
As you can see, the Set-cookie header contains a name-value pair, a GMT date, a path, and a domain. The name and value are encoded by the URL. The expires field is an instruction that tells the browser to "forget" the Cookie after a given time and date.
If the browser is configured to store cookies, it will retain this information until the expiration date. If the user's browser points to any page that matches the cookie's path and domain, it will resend the cookie to the server. The browser header information might look like the following:
GET/HTTP/1.0Connection: Keep-AliveUser-Agent: Mozilla/4.6 (X11;I linux 2.2. 6-15apmac Ppc) host: Zink.. Co. Uk:1126accept: Image/gif,*/*accept-encoding:gzipaccept-language:enaccept-charset:iso-8859-1,*, UTF-8COOKIE:NAME=XYZ
the Servlet is able to access the cookie through the request method Request.getcookies () , which returns an array of cookie objects .
Servlet Cookie method
The following is a list of useful methods that you can use when manipulating cookies in a Servlet.
Serial Number |
Method & Description |
1 |
public void SetDomain (String pattern) This method sets the domain that the cookie applies to, such as runoob.com. |
2 |
Public String GetDomain () This method gets the domain that the cookie applies to, such as runoob.com. |
3 |
public void Setmaxage (int expiry) This method sets the time, in seconds, for the cookie to expire. If this is not set, the cookie will only continue to be valid for the current session. |
4 |
public int Getmaxage () The method returns the maximum lifetime of the cookie, in seconds, by default, 1 means that the cookie will persist until the browser is closed. |
5 |
Public String GetName () The method returns the name of the cookie. The name cannot be changed after it is created. |
6 |
public void SetValue (String newvalue) This method sets the value associated with the cookie. |
7 |
Public String GetValue () This method gets the value associated with the cookie. |
8 |
public void SetPath (String uri) This method sets the path that the cookie applies to. If you do not specify a path, all URLs under the same directory as the current page (including subdirectories) will return cookies. |
9 |
Public String GetPath () This method gets the path that the cookie applies to. |
10 |
public void SetSecure (Boolean flag) This method sets a Boolean value that indicates whether the cookie should be sent only on an encrypted (or SSL) connection. |
11 |
public void Setcomment (String purpose) Sets the comment for the cookie. This comment is useful when the browser renders a cookie to the user. |
12 |
Public String getcomment () Gets a comment for the cookie that returns null if the cookie does not have a comment. |
Javaweb:cookie and Session