java web项目用cookie记住用户名、密码

web 项目中一般在登录的时候都会用到记住密码功能。

1.jsp页面:

01 <% String flag = (String)session.getAttribute("flag")==null?"":(String)session.getAttribute("flag");
02
03 String name ="";
04 String password ="";
05 try{
06 Cookie[] cookies=request.getCookies();
07 if(cookies!=null){
08 for(inti=0;i<cookies.length;i++){
09 if(cookies[i].getName().equals("cookie_user")){
10 String value = cookies[i].getValue();
11 if(value!=null&&!"".equals(value)){
12 name=cookies[i].getValue().split("-")[0];
13 if(cookies[i].getValue().split("-")[1]!=null&& !cookies[i].getValue().split("-")[1].equals("null")){
01 password=cookies[i].getValue().split("-")[1];
02 }
03
04 }
05 }
06 request.setAttribute("name",name);
07 request.setAttribute("passward",password);
08 }
09 }
10 }catch(Exception e){
11 e.printStackTrace();
12 }
13 %>
14 <body>
15 <div id="logo"><img src="<%=request.getContextPath() %>/frontsite/Images/Logo.jpg"width="244"height="44"alt=""/></div>
16 <div id="loginfrm">
17 <form action="<%=request.getContextPath() %>/frontsite/login.do?method=login"method="post"id="forms"onsubmit="checkForm();return false;">
18 <divclass="frm">
19 <ul>
20 <liclass="l1">用户名:</li>
21 <liclass="l2"><input name="login_id"type="text"id="login_id"class="input"value="<%=name %>"/></li>
22 <liclass="l3">* 最大限度为20字节</li>
23 <liclass="l1">密码:</li>
24 <liclass="l2"><input name="login_pwd"type="password"id="login_pwd"class="input"value="<%=password %>"/></li>
25 <liclass="l3">* 最大限度为20字节</li>
26
27 <liclass="l1">验证码:
28 <liclass="l2"style="width: 180px;"><input name="checkImg"id="checkImg"type="text"class="input"style="width: 60px;"size="10"/>
29 &nbsp;&nbsp;<img src="<%=request.getContextPath() %>/CheckImg_FT"width="49"height="22"/></li>
30
31 <liclass="l4">
32 <label><input type="checkbox"name="flag"id="flag"value="1"<%if(flag!=null&& flag.equals("1")){%> checked ; value ="1"; <%}else{%> value="0"<%;}%> />记住密码</label>
33 <label><span style="margin-left:10px; color: #F00;"><html:errors /></span></label>
34 </li>
35
36 <liclass="l4"><input type="image"name="imageField"id="imageField"src="<%=request.getContextPath() %>/frontsite/Images/login_bnt.jpg"/></li>
37 </ul>
38 </div>
39 </form>
40 </div>
41 </body>

2.java 类:

01 String flag = request.getParameter("flag");
02 //set cookie
03 if(flag!=null&& flag.equals("1")){
04 Cookie cookie =newCookie("cookie_user", po.getLogin_id()+"-"+form.getLogin_pwd());
05 cookie.setMaxAge(60*60*24*30);//cookie 保存30天
06 response.addCookie(cookie);
07 }else{
08 Cookie cookie =newCookie("cookie_user",po.getLogin_id()+"-"+null);
09 cookie.setMaxAge(60*60*24*30);//cookie 保存30天
10 response.addCookie(cookie);
11 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值