jsp开发网站使用cookie的一点经验

在开发网站应用程序的过程中,使用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> &nbsp; &nbsp; &nbsp; Cookie[] cs = request.getCookies();<br> &nbsp; &nbsp; &nbsp; for(int i=0;i&lt;cs.length;i++){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.println(cs[i].getName()+&quot; &nbsp;&quot;+cs[i].getValue()+&quot;&lt;br&gt;&quot;);<br> &nbsp; &nbsp; &nbsp; }<br>%&gt;<br><br></span>",1] ); D(["mb","我们打开test.jsp,然后点击链接,进入test1.jsp,我们会发现<wbr>,页面里的内容如下:<br>",1] ); D(["mb","<span class=q>test_cookie_name &nbsp;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>&lt;%<br>for(int i=0;i&lt;8;i++){<br> &nbsp; &nbsp; &nbsp; Cookie c = new Cookie(&quot;test_cookie_name&quot;,<wbr>&quot;test_cookie_value&quot;+i);<br> &nbsp; &nbsp; &nbsp; response.addCookie(c);<br> &nbsp; &nbsp; }<br>%&gt;<br>&lt;a href=&quot;test1.jsp&quot;&gt;test&lt;/a&gt;<br><br>通过测试可知结果还是一样,已有第一次赋的值真正的被写入了co<wbr>okie。有人也许会说,我们可以通过request<wbr>.getCookies()得到所有cookie<wbr>,然后找出这个要写的cookie,然后改变值,测试代码如下:<br>test.jsp代码:<br>&lt;%<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> &nbsp; &nbsp; &nbsp; Cookie c = new Cookie(&quot;test_cookie_name&quot;,<wbr>&quot;test_cookie_value&quot;);<br> &nbsp; &nbsp; &nbsp; response.addCookie(c);<br> &nbsp; &nbsp; &nbsp; Cookie c1 = new Cookie(&quot;test_cookie_name&quot;,<wbr>&quot;test_cookie_value_new&quot;);<br> &nbsp; &nbsp; &nbsp; response.addCookie(c1);<br> &nbsp; &nbsp; &nbsp; Cookie c2 = new Cookie(&quot;test_cookie_name1&quot;,<wbr>&quot;test_cookie_value1&quot;);<br> &nbsp; &nbsp; &nbsp; response.addCookie(c2);<br> &nbsp; &nbsp; &nbsp; Cookie[] cs = request.getCookies();<br> &nbsp; &nbsp; &nbsp; for(int i=0;i&lt;cs.length;i++){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(cs[i].getName().equals(<wbr>&quot;test_cookie_name1&quot;)){<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cs[i].setValue(&quot;test_cookie<wbr>_value1_new&quot;);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.addCookie(c2);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<br> &nbsp; &nbsp; &nbsp; }<br><br>%&gt;<br></span>",1] ); D(["mb","&lt;a href=&quot;test1.jsp&quot;&gt;show cookie value&lt;/a&gt;<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的正确性。
版权所有:idilent 网站转载请注明作者 其他转载方式请与作者联系( idilent@yahoo.com.cn)。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值