<body> <% String cardid = null ; Cookie[] cks = request.getCookies() ; if(cks != null) { // 如果已经设置了cookie , 则得到它的值,将该值放在卡号文本框的value中 for(int i=0; i<cks.length; i++) { if(cks[i].getName().equals("cardid")) cardid = cks[i].getValue(); } } %> <form action="Cookie2.jsp" method="post"> 卡号<input type="text" name="cardid" value="<% if(cardid != null) out.println(cardid); %>"><br> 密码<input type="password" name="password"><br> <input type="submit" value="提交"> </form> </body>
然后在servlet里面判断是否选中复选框
//复选框被选中
if(rem!=null)
{
cookie.setMaxAge(Integer.MAX_VALUE);
}
else
{
cookie.setMaxAge(0);
}
response.addCookie(cookie);
out.println("<h2>登陆成功<h2>");