Cookie保存用户名密码:10天之内记住密码无需输入

指定时间内无序输入账户密码,自动登录

jsp–Cookie控制代码

<% String username = null;
	       String pwd = null;
	       Cookie cookies[] = request.getCookies();
	       if(null != cookies){
		       for(Cookie cookie:cookies){
		           if(cookie.getName().equals("username")){
		               username = cookie.getValue();
		           }
		           if(cookie.getName().equals("pwd")){
		               pwd = cookie.getValue();
		           }
		       } 
	       }
	       if(username != null && pwd!=null){
		       request.getRequestDispatcher("LoginServlet?userName="+username+"&pwd="+pwd).forward(request, response);
	       }
 %>

jsp登录表单

<form action="LoginServlet" method="get">
    用户名:<input type="text"  size="20" name="userName"/><br><br>
    密码 &nbsp;&nbsp;&nbsp;:<input type="password" size="20" name="pwd"/><br/><br/>
    自动登录:<select name="timelength">
			    <option value="0" select>每天都登录</option>
			    <option value="10">10天登录</option>
			    <option value="30">30天登录</option>
		    </select> 
    <br/><br/>
    <div style="margin-left: 2px;">
	    <input type="submit" value="登录" />
	    <INPUT  TYPE="RESET" VALUE="重置" style="margin-left: 30px;" />
    </div> 							
</form>

2、对应的LoginServlet

     int dayLenth = 0;
        if (null != request.getParameter("timelength")) {
            dayLenth = Integer.valueOf(request.getParameter("timelength"));
            Cookie userNameCookie = new Cookie("username",
                request.getParameter("userName"));
            Cookie pwdCookie = new Cookie("pwd",
                    request.getParameter("pwd"));
            userNameCookie.setMaxAge(20);
            pwdCookie.setMaxAge(20);

            response.addCookie(userNameCookie);
            response.addCookie(pwdCookie);
        }
        request.getRequestDispatcher("main.jsp").forward(request, response);

查看自己当前客户端Cookie

谷歌浏览器:

  1. 点击感叹号

在这里插入图片描述
2、查看详情
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值