日期:2014-05-16  浏览次数:20480 次

jsp中的cookie用法小实例
这个小实例有三个页面

index.jsp页面内容如下:

You can choose the list you want to operate:
<br>
    <a href="setCookie.jsp">Write Cookie</a>
    <br>
    <a href="getCookie.jsp">Read Cookie</a>
  
  
    setCookie.jsp页面内容如下:
     <%
    Cookie cookie = new Cookie("password","anony");
    cookie.setMaxAge(60);
    response.addCookie(cookie);
    cookie = new Cookie("userName","liming");
    response.addCookie(cookie);
    %>
  
    getCookie.jsp页面内容如下:
    <%
      Cookie cook[] = request.getCookies();
    
      for(int i = 0;i < cook.length;i++){
       if(cook[i].getName().equals("userName")){
        request.setAttribute("userName",cook[i].getValue());
       }
       if(cook[i].getName().equals("password")){
        request.setAttribute("password",cook[i].getValue());
       }
      }
   %>
   <input type="text" name="userName" value="${userName }"><br>
   <input type="text" name="password" value="${password }">
 
   说明:在进入index.jsp页面的时候先点击Read Cookie,看到的输入框中什么也没有,
         返回,先点击Write Cookie,然后点击Read Cookie 你会看到输入框中就有值
         了,这就是Cookie的自动填充功能。




















http://hi.baidu.com/qq295361921/blog/item/d9a19908060f67226a60fb56.html