Use JSP to operate cookie

xiaoxiao2021-03-06  93

First, in the foreword, cookie should be an application for a long time. As early as HTML just appeared, there is no way to record and identify different users between each independent page. Later, people invented cookie technology. When the user accessed the web, it could create a file on the visitor's machine. We call it as cookie, write a content into it, to identify different users. If the next user accesses this web page, it can read the content inside this file so that the page will know that the last user has visited the webpage. Although the production technology of the web page has been developed many years ago. But sometimes, cookies can still help us with a lot of busy. Next, let's take a look, how to operate cookies with JSP when writing JSP files. Second, write cookies actually use JSP to operate cookies is very simple, let's look at the following JSP program: ........ (middle) <% String cookiename = "sender"; cookie cookie = new cookie (cookiename, "test_content"); cookie.setMaxage (10); response.addcookie (cookie);%> ........ (other content) So we set a cookie, very simple? Let's take a closer study: cookie cookie = new cookie (cookiename, "test_content"); this line establishes a cookie object, initializing two parameters, the first parameter cookiename defines the name of the cookie, the latter parameter It is also a string that defines the content of the cookie. That is, we want the web page to identify files on the user's machine. Next line: cookie.setMaxage (10), call the setMaxage method in cookies, setting the Survival period on the user's machine hard disk is 10 seconds. A cookie exists in the user's hard drive. When building a cookie object, we must formulate the survival of cookies. After this survival, the cookie file will no longer work, will be viewed by the user. Delete itself by yourself. If we want users to visit this page next time, the cookie file is still valid and can be read by the web page, we can set the Cookie's survival time. For example, cookie.setMaxage (365 * 24 * 60 * 60) allows the cookie file to be valid within one year. Third, after reading the cookie cookie file, naturally, we need to read it out, otherwise we are not a white fee? Next, let's see how to read the cookie on the user's hard drive.

........ (middle)

name value <% cookie cookies [] = Request.getCookies (); cookie scookie = null; string svalue = null; string sname = null; for (int i = 0; i
<% = name%> <% = svalue%> <%}%> ........ (Other Content) This small segment JSP file can read all the valid of the user's hard drive Cookie, that is, the cookie file still in the survival period. And list each cookie name and content in the form of a table. Let's analyze this code by line: cookie cookies [] = request.getCookies () We use request.getCookies () to read the cookies on the user's hard drive and put all the cookies in an array of cookie objects. Next, we use a loop statement to traverse the Cookie object array, we use scookie = cookies [i] to take out a cookie object in the array, then we get two methods with scookie.getValue () and scookie.getname (). This cookie name and content. By placing the name and content of the cookie and the content of the string, we can do various operations. In the above example, all cookies can be displayed in a table through the traversal of the cycle statement. Fourth, some questions that need to be aware of the above two simple examples, it can be seen that the operation of cookie with JSP is very simple. However, we have to pay attention to some questions in actual operation: 1. Cookie's compatibility problem Cookie has 2 different versions, the first version, we call cookie Version 0, is initially developed by Netscape, Almost all browser support. Newest versions, cookie Version 1 is based on the RFC 2109 document. In order to ensure compatibility, Java regulations, the previously mentioned operations involving cookies are done for the old version of cookies. The new version of Cookie is currently not supported by javax.servlet.http.cookie. 2. Cookie's content The same cookie's character limit is different for different cookie versions. In cookie version 0, some special characters, such as spaces, square brackets, parentheses, equal to the number (=), comma, double quotes, slash, question mark, @ symbol, colon, no semicolon can be cookie content. This is why we set the contents of the cookie in the example "Test_Content".

转载请注明原文地址:https://www.9cbs.com/read-107542.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.036, SQL: 9