jsp+servlet实现cookie保存用户信息的问题。没分了,大神么帮忙看看吧
小弟写了个登陆的模块,想用cookie保存用户的信息,再次打开浏览器登陆时,cookie中的信息就可以显示在登陆的表单上面。写了半天,cookie是存进去了,就是显示在表...
小弟写了个登陆的模块,想用cookie保存用户的信息,再次打开浏览器登陆时,cookie中的信息就可以显示在登陆的表单上面。写了半天,cookie是存进去了,就是显示在表单上这点一直想不明白。下面是我的代码:
Jsp部分:
<form action="${pageContext.request.contextPath }/before/LoginServlet" method="POST">
<table border="1px">
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="${name }"/></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password" value="${pass }"/></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="checkcode"/></td>
<td><img src="${pageContext.request.contextPath}/images/checkcode.jpg" title="点击图片换一张" onclick="changeImg(this)"/></td>
</tr>
<tr>
<td><input type="submit" value="登录"/></td>
</tr>
</table>
<font color="red" size="+2">${error }</font>
</form>
UserDao userDao = new UserDaoImpl();
String username = request.getParameter("username");
String password = request.getParameter("password");
String checkcode = request.getParameter("checkcode");
String codes = (String) request.getSession().getAttribute("checkcode"); 展开
Jsp部分:
<form action="${pageContext.request.contextPath }/before/LoginServlet" method="POST">
<table border="1px">
<tr>
<td>用户名:</td>
<td><input type="text" name="username" value="${name }"/></td>
</tr>
<tr>
<td>密 码:</td>
<td><input type="password" name="password" value="${pass }"/></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="checkcode"/></td>
<td><img src="${pageContext.request.contextPath}/images/checkcode.jpg" title="点击图片换一张" onclick="changeImg(this)"/></td>
</tr>
<tr>
<td><input type="submit" value="登录"/></td>
</tr>
</table>
<font color="red" size="+2">${error }</font>
</form>
UserDao userDao = new UserDaoImpl();
String username = request.getParameter("username");
String password = request.getParameter("password");
String checkcode = request.getParameter("checkcode");
String codes = (String) request.getSession().getAttribute("checkcode"); 展开
2个回答
展开全部
<script type="text/javascript">
function setCookie(name, value) {
var exp = new Date();
exp.setTime(exp.getTime() + 30 * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
function getCookie(name) {
var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr == null) {
return null;
}
return unescape(arr[2]);
}
</script>
<form action="login">
<table>
<tr>
<td>username :</td>
<td>
<input id="username_input" type="text" name="username" label="username"
onblur="setCookie('username_input',this.value);" />
</td>
</tr>
<tr>
<td>password :</td>
<td>
<input type="password" name="password" label="password" />
</td>
</tr>
<tr>
<td align="right" colspan="2">
<button type="submit">submit</button>
</td>
</tr>
</table>
</form>
<script type="text/javascript">
var username_input = getCookie("username_input");
if(username_input!=null){
document.getElementById("username_input").value = username_input;
}
</script>
2013-05-24
展开全部
session是会话,退出无效。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询