Review the JSP foundation of Jsp/servlet Technology (detailed second)

Source: Internet
Author: User

Cookies

Today, I would like to say a cookie, because in the classroom I do not understand, so, if it is said to be a blog, rather than my review notes, haha

First publish a cookie schematic

1. What is a Cookie?

"A cookie is a variable stored on a visitor's computer. This cookie is sent whenever a page is requested by the same computer through a browser. You can use JSP to create and retrieve the value of a cookie. ”
A cookie is a file created by a visited website that stores browsing information, such as profile information.

From the JSP point of view, a cookie is some string information. This information is stored on the client computer and is used to pass information between the client computer and the server.

This information can be read or set through Document.cookie in the JSP.

2.Cookie Basic Usage

A. Simple access operation

The cookie property of the Document object must be used when accessing cookies using JavaScript, and a line of code describes how to create and modify a cookie:

Document.cookie  = ' Username=darren '

In the above code ' username ' denotes the cookie name, ' Darren ' indicates the value corresponding to the name. If the cookie name does not exist, then a new cookie is created, and if it exists, the value corresponding to the cookie name is modified. If you want to create a cookie multiple times, use this method again.

3.JSP Cookie in Chinese garbled solution

We may have garbled characters when we use cookies.

This is a workaround found on the Web:

Save cookies

String value = Java.net.URLEncoder.encode ("You need to save the Chinese");

Cookie Namecookie = new Cookie ("Draftflow_username", value);
Cookie Usercookie = new Cookie ("Draftflow_user", user);
Namecookie.setmaxage (60*60*24*30*365);
Namecookie.setpath ("/");
Response.addcookie (Namecookie);

Read cookies

cookie[] cookies = request.getcookies ();   if (cookies!=null)  {     = "";       for (int i = 0; i < cookies.length; i++)     {        = cookies[i];              if (C.getname (). Equalsignorecase ("Draftflow_username"))        {           =java.net.urldecoder.decode (C.getvalue ()); // decoding         }    }
}

4. How to perform the add delete operation

<%new Cookie ("AName", "Avalue"%> =======================================  <%newnull); Killmycookie.setmaxage (0); Killmycookie.setpath ("/"

Just write us a lot, and I'm going to see the jquery easy Ui (tre tree) detailed blog next time goodbye!!!

Review the JSP foundation of Jsp/servlet Technology (detailed second)

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.