Cookie introduction and JSP method for processing cookies
1. What are cookies?
As we all know, the browser communicates with the Web server over HTTP. When a user sends a page request, the web server simply responds
Yes, and then close the connection with the user. Therefore, when a request is sent to the Web server, whether it is the first visit or not, the server treats it as the first visit.
Treat, such a bad thing can be imagined. To make up for this defect, Netscape has developed the effective cookie tool to save the recognition information of a user.
They are nicknamed "cookies ". Cookies are a means for Web servers to store information on visitors' hard disks through Browsers: Netscape Navigator uses a name
The cookie information received from all sites is saved for the local cookies.txt file, while the IE browser stores the cookie information in a directory similar to c: \ windows \ cookies.
When a user accesses a site again, the server requires the browser to search for and return the cookie information sent previously to identify the user.
Cookies bring many benefits to websites and users:
1. Cookie allows the site to track the number of visits, the last visit time, and the path of visitors to the site
2. Cookies can tell online advertisers the number of clicks on ads, so that they can deliver ads more accurately.
3. When the cookie validity period is not reached, the cookie enables users to access some websites they have browsed without entering their passwords and usernames.
4. Cookies can help Websites collect users' personal data for various personalized services.
In JSP, we can also use cookies to write some powerful applications.Program.
Next, I would like to introduce how to use JSP to create and process cookies.
Ii. How to Create a cookie
After talking about this, you must be curious about how JSP creates cookies. JSP uses the following syntax format to create a cookie:
Cookie cookie_name = new cookie ("parameter", "value ");
For example, Cookie newcookie = new cookie ("username", "waynezheng"); response. addcookie (newcookie );
Explanation: JSP calls the constructor cookie (name, value) corresponding to the cookie object to create a cookie with a proper name and value.
The addcookie method of httpservletresponse is added to the Set-Cookie response header. In this example, the cookie object has two string parameters: username and waynezheng. Note
The name and value cannot contain white spaces or the following characters :@:;? , "/[] () =
Process cookie attributes
Some friends have asked me again: I know how to create cookies? Yes, it is not enough to know how to create a cookie without knowing how to use it.
. In JSP, the Program sets various attributes through cookie. setxxx and reads the cookie attributes using cookie. getxxx. The main attributes and method columns of the cookie are
For your reference:
Read client cookies
Before sending a cookie to the client, you must create a cookie and then use the addcookie method to send an HTTP header. JSP will call
Request. getcookies () read cookies from the client. The getcookies () method returns an array of cookie objects corresponding to the content in the HTTP request header. You only need to use
Cyclically access each element of the array, call the getname method to check the names of each cookie until the target cookie is found, and then call the getvalue method to obtain the cookie.
The value associated with the specified name.
For example
<%
String username = request. getparameter ("username"); // obtain the username from the submitted HTML form.
Cookie theusername = new cookie ("username", username); // create a cookie with "username", username value/pair
Response. addcookie (theusername );
%>
..............
<%
Cookie mycookie [] = request. getcookies (); // create a cookie object Array
For (INT n = 0; n = cookie. Length-1; I ++); // sets up a loop to access each element of the cookie object array.
Cookie newcookie = mycookie [N];
If (newcookie. getname (). Equals ("username"); // determines whether the element value is a value in username.
{%>
Hello, <% = newcookie. getvalue () %>! // If you find it, say hello to him.
<%}
%>
Set the cookie existence time and delete the cookie in JSP. Use the setmaxage (INT expiry) method to set the cookie existence time. The expiry parameter should be
An integer. A positive value indicates that the cookie will expire after so many seconds. Note that this value is the maximum time for the cookie to exist, rather than the current time when the cookie exists.
A negative value indicates that when the browser is closed, the cookie will be deleted. If the value is zero, the cookie is to be deleted. For example:
<%
Cookie deletenewcookie = new cookie ("newcookie", null );
Deletenewcookie. setmaxage (0 );
Deletenewcookie. setpath ("/");
Response. addcookie (deletenewcookie );
%>