Experience in using cookies for jsp development websites

Source: Internet
Author: User

When developing website applications, use cookies to record users' emailsInformation is a commonly used method, and the use of cookies is also very simple.. If we want to get the cookie value in the jsp (preferred for SUN Enterprise Applications) Program, You only need to use HttpRequest. getCookies () To get the values of all cookies, and write the values to the cooki of the client.E files are also very easy. You need to create a cookie and then call HttPReponse. addCookie (Cookie
C. However, a problem is often ignored during use.If you write a cookie multiple times on a page, what is the result??

Now let's take a look at the code on the two pages below the ghost,
The test. jsp code is as follows:
<%
Cookie c = new Cookie ("test_cookie_name ","Test_cookie_value ");
Response. addCookie (c );
Cookie c1 = new Cookie ("test_cookie_name ","Test_cookie_value_new ");
Response. addCookie (c1 );
%>
<A href = "test1.jsp (preferred for SUN Enterprise Applications)"> show cookie value </a>

The test1.jsp code is as follows:
<%
Cookie c = new Cookie ("test_cookie_name ","Test_cookie_value ");
Response. addCookie (c );
Cookie c1 = new Cookie ("test_cookie_name ","Test_cookie_value_new ");
Response. addCookie (c1 );
Cookie c2 = new Cookie ("test_cookie_name1 ","Test_cookie_value1 ");
Response. addCookie (c2 );
Cookie [] cs = request. getCookies ();
For (int I = 0; I <cs. length; I ++ ){
If (cs [I]. getName (). equals ("Test_cookie_name1 ")){
Cs [I]. setValue ("test_cookie_ Value1_new ");
Response. addCookie (c2 );
Break;
}
}

%>
<A href = "test1.jsp (preferred for SUN Enterprise Applications)"> show cookie value </a>

The test results prove that this practice cannot solve our problem. testThe value of _ cookie_name1 is still test_cookie._ Value1, instead of test_cookie_value1_ New: in fact, we can think about it carefully. This solution won't work.. Because we know that for a request and respons in a pageE. The request is generated in an http request.So it contains the cookie value when the http request is sent.While response is for this http request web
The response generated by the application, so it can write the cookieThe cookie value obtained by the request., And the value of the cookie to be written by response is completely different.In short, the cookie value obtained by the request isThe value of the cookie before the p request, and the value of response to be written isThe cookie value after the http request. So the above solution is noneIt works.

However, I have not found any good solution to the cookie record.Next we write the value for the last time, so we can only do this in the code.For each http request, write the value of each cookie only once.To ensure the correctness of the cookie. Copyright: idilent site reprint please indicate the author of other reprint Methods please contact the author (idilent@yahoo.com.cn ).

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.