jsp利用cookie記住使用者名稱,下次登入時顯示在文字框中
阿新 • • 發佈:2019-02-09
<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>");