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

jsp使用cookie实现记住密码的功能

2011-10-19 10:34 926 查看
这个一个页面模拟的cookie

如果你要实现登录,当用户输入用户名密码时,到控制层用

Cookie user = new Cookie("user",name+"-"+passward); 加到cookie中,

然后在访问此登录页面时,就先调用 Cookie[] cookies=request.getCookies();

看cookie中是否有user这个值,如果有就取得用户名、密码。

代码示例:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String name="nihao";
String passward="nihao";
try{
Cookie user = new Cookie("user",name+"-"+passward);
user.setMaxAge(60);
response.addCookie(user);
Cookie[] cookies=request.getCookies();
if(cookies!=null){
for(int i=0;i<cookies.length;i++){
if(cookies[i].getName().equals("user")){
name=cookies[i].getValue().split("-")[0];
passward=cookies[i].getValue().split("-")[1];
request.setAttribute("name",name);
request.setAttribute("pass",passward);
}
}
}
}catch(Exception e){
e.printStackTrace();
}

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<form action="test10.jsp" method="post">
<input type="text" value="${name}" name=name /></br>
<input type="text" value="${pass}" name=pass />
<input type="submit" value="提交">
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: