您的位置:首页 > Web前端 > JavaScript

Cookie的使用,自动填充用户名和密码

2016-10-19 13:33 190 查看
test1.jsp

<body>
<%
String name = "";
String word = "";
Cookie[] cc = request.getCookies();
if(cc == null || cc.length == 0){

}else{
for( Cookie c :cc){
if(c.getName().equals("username")){
name = c.getValue();
}
if(c.getName().equals("password")){
word = c.getValue();
}
}
}
%>

<form action="cookie/test2.jsp">
用户名: <input type="text" name="username" value="<%=name %>"/><br/>
密码:<input type="password" name="password" value="<%=word %>"/><br/>
<input type="submit" value="登陆"/>
</form>
</body>

test2.jsp
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
if(username.equals("zhangsan")&& password.equals("123456")){
Cookie c1 = new Cookie("username",username);
Cookie c2 = new Cookie("password",password);
c1.setMaxAge(60*60);
c2.setMaxAge(60*60);
response.addCookie(c1);
response.addCookie(c2);
out.print("登陆成功");
}else{
out.print("登陆失败");
}
%>
</body>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  jsp cookie
相关文章推荐