JSP development website uses a little experience of cookie

xiaoxiao2021-03-05  26

In the process of developing a website application, some information using cookie records users is a relatively common method, while cookie is very easy to use. If we want to get a cookie value in the JSP program, you only need to use httprequest.getCookies () to get all the cookie's values, and the Cookie file that is written to the client is also very easy. You need to create a cookie, then call httpreponse .addcookie (cookIEC). But we often ignore a problem during use, that is, if you write a cookie multiple times in a page, what is the result?

Let's look at the code below the following two pages.

Test.jsp code is as follows:

<%

Cookie C = New cookie ("Test_cookie_name", "Test_cookie_Value);

Response.addcookie

Cookie C1 = New cookie ("Test_cookie_name", "Test_cookie_Value_New");

Response.addcookie (C1);

%>

show cookie value

Test1.jsp code is as follows:

<%

Cookie [] CS = Request.getCookies ();

For (int I = 0; i

Out.println (CS [i] .Getname () "" CS [i] .GetValue () "
");

}

%>

We open TEST.JSP, then click on the link, enter Test1.jsp, we will find that the content in the page is as follows:

TEST_COOKIE_NAME TEST_COOKIE_VALUE

JSessionIDQIV2X8CVZYA6T0HNZRVNHFJUEPEAIG8MAGIZ2BREKIUP1PYIIEBQ! -1263017589! -1062731417! 80! 443

We can see the value of Test_cookie_name is Test_cookie_Value, which means that our second call response.addcookie () does not play any effect. In order to make more sure that I made a certain change in Test.jsp's code:

<%

For (int i = 0; i <8; i ) {

Cookie C = New cookie ("Test_cookie_name", "Test_cookie_Value" i);

Response.addcookie

}

%>

test

By testing the results, the same is true, and the first assigned value is really written in the cookie. Some people may say that we can get all cookies through request.getCookies (), then find this cookie to write, then change the value, the test code is as follows:

Test.jsp code:

<%

Cookie C = New cookie ("Test_cookie_name", "Test_cookie_Value);

Response.addcookie

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

IF (CS [i] .GetName (). Equals ("Test_cookie_name1")) {

CS [I] .SetValue ("Test_cookie_Value1_New);

Response.addcookie (C2);

Break;

}

}

%>

show cookie value

Test results still prove that this approach cannot solve the problem we have encountered, Test_cookie_name1 is still TEST_COOKIE_VALUE1, not Test_cookie_Value1_new, in fact, we can know carefully, this solution is not possible. Because we know, for Request and Response in a page, it is an HTTP request generated. Request is all parameters in the HTTP request, so it contains the value of cookie when this HTTP request is issued, and Response is for this HTTP request. Web

The response generated by Application, so it can write the value of the cookie. In this way, the value of the cookie obtained by Request, and the value of the cookie you want to write, it can be said to be completely different, simply saying that the value of the cookie obtained by request is in The value of the cookie before this HTTP request, and the response is written as the value of the cookie after this HTTP request. So the above solution is unable to do.

And I haven't found any good solutions now can be the value of the Cookie records, so we can only do it in the code for each HTTP request, for each cookie Value, only write once, to ensure the correctness of the cookie.

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

New Post(0)