JSP Cookie Processing

Source: Internet
Author: User

JspCookie Processing

Cookies are text files that are stored in the client computer, and they hold a large amount of trace information. On the basis of servlet technology, JSP is clearly able to provide support for HTTP cookies.

There are usually three steps to identify repeat customers:

    • The server script sends a series of cookies to the browser. such as name, age, ID number and so on.
    • The browser stores this information in a local machine for a rainy-out.
    • The next time the browser sends any request to the server, it will send the cookie information to the server at the same time, and the server uses that information to identify the user or do something else.

This section will teach you how to set or reset cookies, how to access them, and how to delete them.

JSP Cookie processing requires the encoding and decoding of Chinese, as follows:

Stringstr = 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 (although JavaScript can set cookies directly in the browser). In JSP, setting a cookie requires sending the following header to the server:

HTTP/1.1 200OkDate: Fri, 04 Feb 2015 21st:03:38GmtServer: Apache/1.3.9 (Unix)Php/4.0b3Set-Cookies:Name=Runoob; Expires=friday, 04-feb-07  22:03: 38 Gmt; Path=/; Domain=runoob.connection: Closecontent -type: Text/ html                

As you can see, the Set-cookie information header contains a key-value pair, a GMT (Greenwich mean) time, a path, and a domain name. Key-value pairs are encoded as URLs. The validity field is an instruction that tells the browser when to clear the cookie.

If the browser is configured to store cookies, it will save the information until it expires. If any page accessed by the user matches the path and domain name in the cookie, then the browser will re-send the cookie back to the server. The information header on the browser side looks like this:

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 JSP script accesses these cookies through the GetCookies () method in the Request object, which returns an array of Cookie objects.

Servlet Cookie method

The following table lists the methods that are commonly used in cookie objects:

Serial Number method & Description
1 public void SetDomain (String pattern)

Set the domain name of the cookie, such as runoob.com

2 Public String GetDomain ()

Get the domain name of the cookie, such as runoob.com

3 public void Setmaxage (int expiry)

Sets the lifetime of the cookie, in seconds, and the default validity period is the current session's survival time

4 public int Getmaxage ()

Gets the lifetime of the cookie, in seconds, by default-1, indicating that the cookie will live until the browser is closed

5 Public String GetName ()

Returns the name of the cookie that cannot be modified after the name is created

6 public void SetValue (String newvalue)

Set the value of a cookie

7 Public String GetValue ()

Get the value of a cookie

8 public void SetPath (String uri)

Set the path of the cookie, default to all URLs under the current page directory, and all subdirectories under this directory

9 Public String GetPath ()

Get the path to the cookie

10 public void SetSecure (Boolean flag)

Indicates whether the cookie is to be encrypted for transmission

11 public void Setcomment (String purpose)

Set the comment to describe the purpose of the cookie. Annotations become useful when the browser presents a cookie to the user

12 Public String getcomment ()

Returns a comment describing the purpose of the cookie, or null if not

Setting up cookies with JSP

Using a JSP to set up a cookie consists of three steps:

(1) Create a Cookie object: Call the cookie's constructor, use a cookie name and value to do the argument, they are all strings.

Cookie=newcookie("key","value");       

It is important to keep in mind that neither the name nor the value can contain spaces or characters such as the following:

[]()=," / ? @ : ;

(2) Set validity period: call the Setmaxage () function to indicate how long (in seconds) the cookie is valid. The following actions will be valid for 24 hours.

Cookies.  Setmaxage(* *);        

(3) Send the cookie to the HTTP response header: call the Response.addcookie () function to add a cookie to the HTTP response header.

Response.  Addcookie(cookie);    

JSP Cookie Processing

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.