登录时需要用户“记住用户名”,如图:
红色部分需要注意的,
if("index".equals(action)){//显示首页登录
CookieUtil util=new CookieUtil(request,response,365*24*3600);
Cookie cookie=util.getCookie("userId");
if(cookie!=null){//需要判断是否记录了用户名
request.getSession().setAttribute("userId", util.getCookieValue("userId"));
request.getSession().setAttribute("remember", "1");
}
return mapping.findForward("index");
}
else
if ("login".equals(action)) {//点击登录时
System.out.println("----------------login--------------");
String userId = request.getParameter("userId");
String password = request.getParameter("password");
CookieUtil util=new CookieUtil(request,response,-1);
String authCode=util.getCookieValue("AUTHCODE");
System.out.println("authCode="+authCode);
if (!authCode.equals(form.getAuthCode())) {//验证码不正确
msg="验证码不正确!";
request.setAttribute("msg", msg);
return mapping.getInputForward();
}
User user = (User)this.getBaseService().get("from User where userId = '"+userId+"'");
if(user==null){
msg="用户名不存在!";
request.setAttribute("msg", msg);
return mapping.getInputForward();
}
if(!user.getPassword().equals(StringUtil.hash(password))){
msg="密码不正确!";
request.setAttribute("msg", msg);
return mapping.getInputForward();
}
//是否保存用户名到cookie
String remember = request.getParameter("remember");
if(remember==null){//不勾选,则删除之
CookieUtil util2=new CookieUtil(request,response,0);
util2.deleteCookie("userId");
}else{//勾选,则保存之
CookieUtil util2=new CookieUtil(request,response,365*24*3600);
util2.addCookie("userId", userId);//保存用户名到cookies
}
........
)
需要CookieUtil类,见: http://gundumw100.iteye.com/admin/blogs/494328
//jsp文件如下:
<table width="100%" style="margin-top:30px">
<tbody>
<tr>
<td align="right">用户名:</td>
<td align="left"> <input type="text" id="userId" name="userId" value="${sessionScope.userId }" /></td>
</tr>
<tr>
<td align="right">密码:</td>
<td align="left"><input type="password" id="password" name="password" value="" /></td>
</tr>
<tr>
<td align="right">验证码:</td>
<td align="left"><input type="text" style="width:90px" id="authCode" name="authCode"/><a href="#" οnclick="javascript:refresh();"><img height="18" src="authimg" id="checkCodeImg" border="0"></a></td>
</tr>
<tr>
<td align="right"></td>
<td align="left">
<input type="checkbox" id="remember" name="remember" value="1" ${sessionScope.remember==1?'checked':'' }>记住用户名
</td>
</tr>
<tr>
<td colspan="2"><div align="center" style="color:red">${requestScope.msg }</div></td>
</tr>
<tr>
<td colspan="2" align="center">
<input id="imgButton" name="imgButton" type="image" src="images/login.gif" />
<a href="login.portal?action=register">注册</a>
</td>
</tr>
</tbody>
</table>
![](http://dl.iteye.com/upload/attachment/193473/262c99e8-3147-3cf3-b6fd-5c878f92e803.png)
红色部分需要注意的,
if("index".equals(action)){//显示首页登录
CookieUtil util=new CookieUtil(request,response,365*24*3600);
Cookie cookie=util.getCookie("userId");
if(cookie!=null){//需要判断是否记录了用户名
request.getSession().setAttribute("userId", util.getCookieValue("userId"));
request.getSession().setAttribute("remember", "1");
}
return mapping.findForward("index");
}
else
if ("login".equals(action)) {//点击登录时
System.out.println("----------------login--------------");
String userId = request.getParameter("userId");
String password = request.getParameter("password");
CookieUtil util=new CookieUtil(request,response,-1);
String authCode=util.getCookieValue("AUTHCODE");
System.out.println("authCode="+authCode);
if (!authCode.equals(form.getAuthCode())) {//验证码不正确
msg="验证码不正确!";
request.setAttribute("msg", msg);
return mapping.getInputForward();
}
User user = (User)this.getBaseService().get("from User where userId = '"+userId+"'");
if(user==null){
msg="用户名不存在!";
request.setAttribute("msg", msg);
return mapping.getInputForward();
}
if(!user.getPassword().equals(StringUtil.hash(password))){
msg="密码不正确!";
request.setAttribute("msg", msg);
return mapping.getInputForward();
}
//是否保存用户名到cookie
String remember = request.getParameter("remember");
if(remember==null){//不勾选,则删除之
CookieUtil util2=new CookieUtil(request,response,0);
util2.deleteCookie("userId");
}else{//勾选,则保存之
CookieUtil util2=new CookieUtil(request,response,365*24*3600);
util2.addCookie("userId", userId);//保存用户名到cookies
}
........
)
需要CookieUtil类,见: http://gundumw100.iteye.com/admin/blogs/494328
//jsp文件如下:
<table width="100%" style="margin-top:30px">
<tbody>
<tr>
<td align="right">用户名:</td>
<td align="left"> <input type="text" id="userId" name="userId" value="${sessionScope.userId }" /></td>
</tr>
<tr>
<td align="right">密码:</td>
<td align="left"><input type="password" id="password" name="password" value="" /></td>
</tr>
<tr>
<td align="right">验证码:</td>
<td align="left"><input type="text" style="width:90px" id="authCode" name="authCode"/><a href="#" οnclick="javascript:refresh();"><img height="18" src="authimg" id="checkCodeImg" border="0"></a></td>
</tr>
<tr>
<td align="right"></td>
<td align="left">
<input type="checkbox" id="remember" name="remember" value="1" ${sessionScope.remember==1?'checked':'' }>记住用户名
</td>
</tr>
<tr>
<td colspan="2"><div align="center" style="color:red">${requestScope.msg }</div></td>
</tr>
<tr>
<td colspan="2" align="center">
<input id="imgButton" name="imgButton" type="image" src="images/login.gif" />
<a href="login.portal?action=register">注册</a>
</td>
</tr>
</tbody>
</table>