Cookie|js in developing Web site applications, using cookies to record some of the user's information is a more common method, and cookies are very simple to use. If we want to get the value of the cookie in the JSP program, we just need to use httprequest.getcookies () to get the value of all cookies, and it is very easy to write the value to the client's cookie file, which is to create a cookie, Then call Httpreponse.addcookie (Cookiec). But we tend to ignore a problem when we use it, that is, if you write a cookie multiple times on a page, what happens? Let's take a look at the code for the following two pages, test.jsp code below:<% 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 ">show Cookie value</a> Test1.jsp code is as follows:<% cookie[] cs = request.getcookies (); for (int i=0;i< cs.length;i++) { OUT.PRINTLN (Cs[i].getname () + " " +cs[i].getvalue () + "<br>"); }%> We open test.jsp, then click on the link to enter test1.jsp, we will find that the contents of the page are as follows: Test_cookie_name Test_cookie_valuejsessionidqiv2x8cvzya6t0hnzrVnhfjuepeaig8magiz2brekiup1pyiiebq!-1263017589!-1062731417!80!443 we can see that the value of Test_cookie_name is Test_cookie_value, This means that our second call to Response.addcookie () does not work. To be more certain, I made some changes to the TEST.JSP code: <%for (int i=0;i<8;i++) { Cookie c = new Cookie ("Test_cookie_ Name "," Test_cookie_value "+i); Response.addcookie (c); }%><a href=" test1.jsp " >test</a> by testing the results are still the same, the first assignment of the value is actually written to the cookie. One might say that we can get all the cookies through Request.getcookies (), then find the cookie to write, then change the value and test the code as follows: test.jsp code:<% 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") &nb Sp Response.addcookie (C2); & nbsp break; } }%& Gt;<a href= "test1.jsp" >show cookies value</a> test results still prove that this practice does not solve the problem we encounter, test_cookie_name1 value is still test_cookie_ Value1, not test_cookie_value1_new, in fact, we think carefully to know that this solution is not workable. Because we know that for requests and response in a page, an HTTP request is generated, request is all the parameters in the HTTP request, and therefore contains the value of the cookie when the HTTP request is issued. and response is the response to this HTTP request WebApplication, so it can write the value of the cookie, so it seems that the value of the cookie the request obtains, and response to write the value of the cookie can be said to be completely different, Simply put, the value of the cookie that the request was given was the value of the cookie before the HTTP request, and response was to write the value of the cookie after the HTTP request. So the solution above is not going to work. And I haven't found a good solution right now. The cookie records the value we wrote the last time, soFor this problem we can only do in the code, for each HTTP request, for each cookie value, write only once, so that the cookie is correct. Copyright: Idilent website reprint Please indicate the author other reprint way please contact the author (idilent@yahoo.com.cn).
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.