java web登录页面上记住密码

来源:互联网 发布:海森矩阵的意义 编辑:程序博客网 时间:2024/05/17 06:47

利用cookie编程

     login.jsp 页面上的java代码:

 

<% String flag = (String)session.getAttribute("flag")==null?"":(String)session.getAttribute("flag"); String name = "";String password = "";try{     Cookie[] cookies=request.getCookies();     if(cookies!=null){     for(int i=0;i<cookies.length;i++){         if(cookies[i].getName().equals("cookie_user")){         String value =  cookies[i].getValue();        if(value!=null&&!"".equals(value)){            name=cookies[i].getValue().split("-")[0];             if(cookies[i].getValue().split("-")[1]!=null && !cookies[i].getValue().split("-")[1].equals("null")){                password=cookies[i].getValue().split("-")[1];             }                                }           }        request.setAttribute("loginName",name);        request.setAttribute("passwd",password);    }    } }catch(Exception e){    e.printStackTrace(); } %> 

login.jsp页面上的html代码:

<tr><td>用户名:</td><td><input type="text" id="loginName" name="loginName" <%if(flag!=null && flag.equals("1")){%>  value ="<%=name%>"; <%}else {%> value="" <%;}%> required  style="width:180px; line-height:24px; height:24px;" /></td></tr><tr><td>密 码:</td><td><input type="password" id="passwd" name="passwd" <%if(flag!=null && flag.equals("1")){%>  value ="<%=password%>"; <%}else {%> value="" <%;}%> required   required style="width:180px; line-height:24px; height:24px;"/></td></tr><tr><td>验证码:</td><td><input type="text" id="kaptcha" name="kaptcha" style="width:80px; line-height:24px; height:24px;"/>                <span class="verification"><img src="images/verification.jpg" id="kaptchaImage" name="photoimg"  alt="验证码" title="点击换图片"  onclick="changeImage();"/><a class="refresh" href="javascript:changeImage();"></a>  </span></td></tr> <tr><td colspan="2"><input type="checkbox" name="flag" id="flag" value="1" <%if(flag!=null && flag.equals("1")){%> checked ; value ="1"; <%}else {%> value="0" <%;}%> />记住密码</td></tr>

login.java类中的login.do方法中增加如下代码:

    //set cookie        if(flag!=null && flag.equals("1")){            Cookie cookie = new Cookie("cookie_user", secPrivilegeUser.getLoginName()+"-"+this.passwd);                            cookie.setMaxAge(60*60*24*30); //cookie 保存30天            this.getResponse().addCookie(cookie);        }else{              Cookie cookie = new Cookie("cookie_user",secPrivilegeUser.getLoginName()+"-"+null);                            cookie.setMaxAge(60*60*24*30); //cookie 保存30天            this.getResponse().addCookie(cookie);                     }        this.getRequest().getSession().setAttribute("flag", flag);            
  private String   flag;
public String getFlag() {return flag;}public void setFlag(String flag) {this.flag = flag;}






0 0
原创粉丝点击