Use of JSP cookies

Source: Internet
Author: User

Cookie concept:
The cookie format is actually a piece of plain text information, which is sent by the server along with the webpage to the client and saved in the specified directory on the client's hard disk. it is said that cookies may cause serious security threats. when the server reads the cookie, it can only read the server-related information. in addition, browsers generally only allow 300 cookies, and each site can store up to 20 cookies. The size of each cookie is currently 4 K, which does not occupy much space. moreover, cookies are time-effective. for example, if the cookie survival time is set to 1 minute, the cookie will be deleted by the browser one minute later.

Cookie version:
There are currently two versions:
Version 0: Developed by Netscape and supported by almost all browsers. in Java, to maintain compatibility, only version 0 is supported currently. The cookie content cannot contain spaces, square brackets, Parentheses, equals signs (=), commas, double quotation marks, slashes, question marks, @ symbol, colon, semicolon.
Version 1: According to RFC 2109, many restrictions are relaxed. The characters above can be used. To maintain compatibility, avoid using these special characters.

Cookie operations in JSP: Type method name method explanation
String getcomment () returns the comments in the cookie. If there is no comments, a null value is returned.
String getdomain () returns the domain name applicable to the cookie in the cookie. the getdomain () method can be used to instruct the browser to return the cookie to other servers in the same domain. Generally, the cookie only returns the server with the same name as the server that sent the cookie. Note that the domain name must start with a dot
Int getmaxage () returns the maximum time before the cookie expires, in seconds.
String getname () returns the cookie name.
String getpath () returns the applicable Cookie Path. If no path is specified, the cookie will be returned to all the pages in the directory of the current page and Its subdirectories.
Boolean getsecure (): If the browser sends cookies through the security protocol, the return value is true. If the browser uses the standard protocol, the return value is false.
String getvalue () returns the cookie value. The author will also introduce getvalue/setvalue in detail later.
Int getversion () returns the Protocol version that the cookie complies.
Void setcomment (string purpose) set comments in cookie
Void setdomain (string pattern): Set the domain name applicable to cookies.
Void setmaxage (INT expiry) is calculated in seconds to set the cookie expiration time.
Void setpath (string URI) specifies the path to which the cookie applies.
Void setsecure (Boolean flag) indicates the security protocol used by the browser, such as https or SSL.
Void setvalue (string newvalue) Cookie is created and a new value is set.
Void setversion (INT v) sets the Protocol version that cookie complies.

A simple example
1. Write cookie --- writecookie. jsp
-------------------------------------------------------------
<% @ Page contenttype = "text/html; charset = iso8859_1" %>
<%
Cookie _ cookie = new cookie ("user_delfancom", "delfan ");
_ Cookie. setmaxage (30*60); // set the cookie survival time to 30 minutes.
Response. addcookie (_ cookie); // write to the client Hard Disk
Out. Print ("Cookie writing completed ");
%>

2. Read cookie. jsp --- readcookie. jsp
-------------------------------------------------------------
<%
Cookie Cookies [] = request. getcookies (); // read all cookies in the applicable directory and store them in the cookies Array
Cookie scookie = NULL;
String sname = NULL;
String name = NULL;
If (cookies = NULL) // if no cookie exists
Out. Print ("none any cookie ");
Else
{
Out. Print (cookies. Length + "<br> ");
For (INT I = 0; I <cookies. length; I ++) // list all available cookies cyclically.
{
Scookie = Cookies [I];
Sname = scookie. getname ();
Name = scookie. getvalue ();
Out. println (sname + "->" + name + "<br> ");
}
}
%>

Two issues to be aware:
1. cookie has a problem with the applicable path, that is, if writecookie. JSP and readcookie. JSP should be placed in the consent directory. If not in the same directory, you must set the path to readcookie when writing. the path of the JSP.
2. When reading the cookie array, You need to determine whether it is null. This is not written in many code on the Internet.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/lengyuhong/archive/2010/05/12/5584432.aspx

Related Article

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.