Servlet cookie Processing

Source: Internet
Author: User

Servlet cookie Processing

Cookies are usually set in the HTTP header information (although JavaScript can also directly set a Cookie in the browser ). The Servlet that sets the Cookie sends the following header information:

HTTP/1.1 200 OKDate: Fri, 04Feb 2000 21:03:38 GMTServer:Apache/1.3.9 (UNIX) PHP/4.0b3Set-Cookie: name=xyz; expires=Friday, 04-Feb-0722:03:38 GMT;path=/;domain=w3cschool.ccConnection:closeContent-Type:text/html

The Set-Cookie header contains a name-value pair, a gmt date, a path, and a domain. The name and value are URL encoded. The expires field is an instruction that tells the browser to "forget" the Cookie after the specified time and date.

The following is a list of useful methods that can be used to operate Cookies in Servlet.

1 public voidSetDomain(String pattern)

This method sets the domains applicable to cookies, such as w3cschool. cc.

2 public StringGetDomain()

This method obtains the domains applicable to cookies, such as w3cschool. cc.

3 public voidSetMaxAge(Int expiry)

This method sets the cookie expiration time (in seconds ). If this parameter is not set, the cookie will only remain valid in the current session.

4 public intGetMaxAge()

This method returns the maximum life cycle (in seconds) of the cookie. By default,-1 indicates that the cookie will continue until the browser closes.

5 public StringGetName()

This method returns the cookie name. 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 StringGetValue()

This method gets the value associated with the cookie.

8 public voidSetPath(String uri)

This method sets the applicable path of the cookie. If you do not specify a path, all URLs in the same directory (including subdirectories) as the current page will return cookies.

9 public StringGetPath()

This method obtains the applicable path of the cookie.

10 public voidSetSecure(Boolean flag)

This method sets a Boolean value, indicating whether the cookie should be sent only over an encrypted (SSL) connection.

11 public voidSetComment(String purpose)

This method specifies the comment describing the cookie purpose. This comment is useful when the browser presents cookies to users.

12 public StringGetComment()

This method returns a comment describing the purpose of the cookie. If the cookie does not have a comment, null is returned.

Create a Cookie object:

Cookie cookie = newCookie("key","value");

No name or value should contain spaces or any of the following characters:

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

Set the maximum lifecycle:

Cookie. setMaxAge (60*60*24); // in seconds

Add Cookies:

Response. addCookie (cookie );

To delete a Cookie, set the lifecycle to 0.

Cookie. setMaxAge (0 );

Set the Cookie usage path

Cookie. setPath ("/sessionWeb/"); // The cookie can be accessed only by a program whose contextPath is "/sessionWeb". The parameter must end "/"

Set the Cookie Access Domain Name

Cookie. setDomain (".google.com"); // All domain names ending with ".google.com" can access this cookie

Chinese characters are Unicode encoded and contain four characters in the memory, while English letters are ASCII characters. Memory only occupies two characters. When Unicode characters are used in cookies, Unicode characters must be encoded, otherwise, garbled characters may occur. You can use java.net. methods In the URLEncoder class

Cookie cookie = new Cookie (URLEncoder. encode ("name", "UTF-8"), URLEncoder. encode ("Zhang San", "UTF-8"); response. addCookie (cookie); if (request. getCookies ()! = Null) {for (Cookie cookie: request. getCookies () {String name = URLEncoder. decode (cookie. getName (), "UTF-8"); String name = URLEncoder. decode (cookie. getValue (), "UTF-8 ");}}


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.