登录时“记住用户名”

系统 1216 0
登录时需要用户“记住用户名”,如图:
登录时“记住用户名”

红色部分需要注意的,
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="#" onclick="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>




登录时“记住用户名”


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论