在开发网站应用程序的过程中,使用cookie记录用户的一些信
息是比较常用的一种方法,而cookie的使用也非常简单
。如果我们在jsp程序中希望得到cookie的值
,只需要使用HttpRequest.getCookies(
)即可得到所有cookie的值,而把值写入客户端的cooki
e文件也非常容易,是需要创建一个cookie,然后调用Htt
pReponse.addCookie(Cookie
c)即可。但是我们在使用过程中往往会忽略一个问题 ,就是如果在一个页面中多次写一个cookie,那么结果如何呢
?
我们现在看一下下面两个页面的代码,
test.jsp代码如下:
<%
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代码如下:
<%
<script type="text/javascript"> <!-- D(["mb","<span class=q> Cookie[] cs = request.getCookies();<br> for(int i=0;i<cs.length;i++){<br> out.println(cs[i].getName()+" "+cs[i].getValue()+"<br>");<br> }<br>%><br><br></span>",1] ); D(["mb","我们打开test.jsp,然后点击链接,进入test1.jsp,我们会发现<wbr>,页面里的内容如下:<br>",1] ); D(["mb","<span class=q>test_cookie_name test_cookie_value<br></span>",1] ); D(["mb","JSESSIONIDQiv2X8CVzyA6T0hNzrVN<wbr>HFJUepeAiG8magiz2bREKiUP1Pyiie<wbr>bq!-1263017589!-1062731417!80<wbr>!443<br><br>我们可以看到test_cookie_name的值是test<wbr>_cookie_value,这说明我们第二次调用respon<wbr>se.addCookie()没有起到任何作用。为了更加确定这<wbr>一说法我把test.jsp的代码做了一定的改动:<br><%<br>for(int i=0;i<8;i++){<br> Cookie c = new Cookie("test_cookie_name",<wbr>"test_cookie_value"+i);<br> response.addCookie(c);<br> }<br>%><br><a href="test1.jsp">test</a><br><br>通过测试可知结果还是一样,已有第一次赋的值真正的被写入了co<wbr>okie。有人也许会说,我们可以通过request<wbr>.getCookies()得到所有cookie<wbr>,然后找出这个要写的cookie,然后改变值,测试代码如下:<br>test.jsp代码:<br><%<br>",1] ); //--> </script> Cookie[] cs = request.getCookies();
for(int i=0;i<cs.length;i++){
out.println(cs[i].getName()+" "+cs[i].getValue()+"<br>");
}
%>
我们打开test.jsp,然后点击链接,进入test1.jsp,我们会发现 ,页面里的内容如下:
test_cookie_name test_cookie_value
JSESSIONIDQiv2X8CVzyA6T0hNzrVN HFJUepeAiG8magiz2bREKiUP1Pyiie
bq!-1263017589!-1062731417!80
!443
我们可以看到test_cookie_name的值是test _cookie_value,这说明我们第二次调用respon
se.addCookie()没有起到任何作用。为了更加确定这
一说法我把test.jsp的代码做了一定的改动:
<%
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>
通过测试可知结果还是一样,已有第一次赋的值真正的被写入了co okie。有人也许会说,我们可以通过request
.getCookies()得到所有cookie
,然后找出这个要写的cookie,然后改变值,测试代码如下:
test.jsp代码:
<%
<script type="text/javascript"> <!-- D(["mb","<span class=q> Cookie c = new Cookie("test_cookie_name",<wbr>"test_cookie_value");<br> response.addCookie(c);<br> Cookie c1 = new Cookie("test_cookie_name",<wbr>"test_cookie_value_new");<br> response.addCookie(c1);<br> Cookie c2 = new Cookie("test_cookie_name1",<wbr>"test_cookie_value1");<br> response.addCookie(c2);<br> Cookie[] cs = request.getCookies();<br> for(int i=0;i<cs.length;i++){<br> if(cs[i].getName().equals(<wbr>"test_cookie_name1")){<br> cs[i].setValue("test_cookie<wbr>_value1_new");<br> response.addCookie(c2);<br> break;<br> }<br> }<br><br>%><br></span>",1] ); D(["mb","<a href="test1.jsp">show cookie value</a><br><br>测试结果还是证明这种做法不能解决我们遇到的问题,test<wbr>_cookie_name1的值仍是test_cookie<wbr>_value1,而不是test_cookie_value1<wbr>_new,其实我们仔细想一下就可以知道,这个解决方案是行不通<wbr>的。<br><br>===================<br>Sincerely yours,<br>",1] ); //--> </script> 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">show cookie value</a>
测试结果还是证明这种做法不能解决我们遇到的问题,test _cookie_name1的值仍是test_cookie
_value1,而不是test_cookie_value1
_new,其实我们仔细想一下就可以知道,这个解决方案是行不通
的。因为我们知道,对于一个页面中的request和respons
e,是一次http请求产生的,request是http请求中
的所有参数,因此包含了发出这次http请求时cookie的值
,而response是对于这次http请求web
application产生的反应,所以它可以写cookie的 值,这样看来,request得到的cookie的值
,和response要写的cookie的值可以说是完全不同的
,简单地说request得到的cookie的值是在这次htt
p请求之前的cookie的值,而response要写得值是这
次http请求之后的cookie的值。所以上面的解决方案是无
法行得通的。
而我现在还没有发现有什么好的解决方案能够是的cookie记录 下我们最后一次写入的值,因此对于这个问题我们只能在代码中做到
,对于每个http请求,对于每个cookie的值,只写一次
,从而保证cookie的正确性。
c)即可。但是我们在使用过程中往往会忽略一个问题
我们现在看一下下面两个页面的代码,
test.jsp代码如下:
<%
Cookie c = new Cookie("test_cookie_name",
response.addCookie(c);
Cookie c1 = new Cookie("test_cookie_name",
response.addCookie(c1);
%>
<a href="test1.jsp">show cookie value</a>
test1.jsp代码如下:
<%
<script type="text/javascript"> <!-- D(["mb","<span class=q> Cookie[] cs = request.getCookies();<br> for(int i=0;i<cs.length;i++){<br> out.println(cs[i].getName()+" "+cs[i].getValue()+"<br>");<br> }<br>%><br><br></span>",1] ); D(["mb","我们打开test.jsp,然后点击链接,进入test1.jsp,我们会发现<wbr>,页面里的内容如下:<br>",1] ); D(["mb","<span class=q>test_cookie_name test_cookie_value<br></span>",1] ); D(["mb","JSESSIONIDQiv2X8CVzyA6T0hNzrVN<wbr>HFJUepeAiG8magiz2bREKiUP1Pyiie<wbr>bq!-1263017589!-1062731417!80<wbr>!443<br><br>我们可以看到test_cookie_name的值是test<wbr>_cookie_value,这说明我们第二次调用respon<wbr>se.addCookie()没有起到任何作用。为了更加确定这<wbr>一说法我把test.jsp的代码做了一定的改动:<br><%<br>for(int i=0;i<8;i++){<br> Cookie c = new Cookie("test_cookie_name",<wbr>"test_cookie_value"+i);<br> response.addCookie(c);<br> }<br>%><br><a href="test1.jsp">test</a><br><br>通过测试可知结果还是一样,已有第一次赋的值真正的被写入了co<wbr>okie。有人也许会说,我们可以通过request<wbr>.getCookies()得到所有cookie<wbr>,然后找出这个要写的cookie,然后改变值,测试代码如下:<br>test.jsp代码:<br><%<br>",1] ); //--> </script> Cookie[] cs = request.getCookies();
for(int i=0;i<cs.length;i++){
out.println(cs[i].getName()+" "+cs[i].getValue()+"<br>");
}
%>
我们打开test.jsp,然后点击链接,进入test1.jsp,我们会发现
test_cookie_name test_cookie_value
JSESSIONIDQiv2X8CVzyA6T0hNzrVN
我们可以看到test_cookie_name的值是test
<%
for(int i=0;i<8;i++){
Cookie c = new Cookie("test_cookie_name",
response.addCookie(c);
}
%>
<a href="test1.jsp">test</a>
通过测试可知结果还是一样,已有第一次赋的值真正的被写入了co
test.jsp代码:
<%
<script type="text/javascript"> <!-- D(["mb","<span class=q> Cookie c = new Cookie("test_cookie_name",<wbr>"test_cookie_value");<br> response.addCookie(c);<br> Cookie c1 = new Cookie("test_cookie_name",<wbr>"test_cookie_value_new");<br> response.addCookie(c1);<br> Cookie c2 = new Cookie("test_cookie_name1",<wbr>"test_cookie_value1");<br> response.addCookie(c2);<br> Cookie[] cs = request.getCookies();<br> for(int i=0;i<cs.length;i++){<br> if(cs[i].getName().equals(<wbr>"test_cookie_name1")){<br> cs[i].setValue("test_cookie<wbr>_value1_new");<br> response.addCookie(c2);<br> break;<br> }<br> }<br><br>%><br></span>",1] ); D(["mb","<a href="test1.jsp">show cookie value</a><br><br>测试结果还是证明这种做法不能解决我们遇到的问题,test<wbr>_cookie_name1的值仍是test_cookie<wbr>_value1,而不是test_cookie_value1<wbr>_new,其实我们仔细想一下就可以知道,这个解决方案是行不通<wbr>的。<br><br>===================<br>Sincerely yours,<br>",1] ); //--> </script> Cookie c = new Cookie("test_cookie_name",
response.addCookie(c);
Cookie c1 = new Cookie("test_cookie_name",
response.addCookie(c1);
Cookie c2 = new Cookie("test_cookie_name1",
response.addCookie(c2);
Cookie[] cs = request.getCookies();
for(int i=0;i<cs.length;i++){
if(cs[i].getName().equals(
cs[i].setValue("test_cookie
response.addCookie(c2);
break;
}
}
%>
<a href="test1.jsp">show cookie value</a>
测试结果还是证明这种做法不能解决我们遇到的问题,test
application产生的反应,所以它可以写cookie的
而我现在还没有发现有什么好的解决方案能够是的cookie记录
版权所有:idilent 网站转载请注明作者 其他转载方式请与作者联系(
idilent@yahoo.com.cn)。