Java Web 自动登录

一、客户端选择自动登录复选框,LoginServlet得到数据后,判断是否选择了复选框,若成功选中则创建cookies对象,并添加到响应头中

 

复制代码

 1 //若用户选择自动登录,则生成cookies保存必要信息
 2                 if("auto".equals(autoLogin)){
 3          //cookies需要设置编码格式

             Cookie cookie = new Cookie("username", URLEncoder.encode(username, "utf-8"));
 4                     Cookie cookie2 = new Cookie("password",password);
 5                     //设置保存时间
 6                     cookie.setMaxAge(7*24*60*60);
 7                     cookie2.setMaxAge(7*24*60*60);
 8                     //设置保存路径
 9                     cookie.setPath(request.getContextPath()+"/");
10                     //添加到响应头
11                     response.addCookie(cookie);
12                     response.addCookie(cookie2);
13                 }

复制代码

 

 

二、下次当用户来到Login.jsp后,会从cookies(每一个站点有唯一的cookies)中判断是否有存入的username和password,若有则直接发送给LoginServlet判断,若没有则执行普通登录操作(JSP中写代码)

 

复制代码

 1 <%
 2    Cookie[] cookies=request.getCookies();
 3    String username = null;
 4    String password = null;
 5    if(cookies!=null){
 6            for(int i=0;i<cookies.length;i++){
 7                String name = cookies[i].getName();
 8                if("username".equals(name)){
 9          //如果是中文,cookies需要解码

            username = URLDecoder.decode(cookies[i].getValue(), "utf-8");
10                }else if("password".equals(name)){
11                    password = cookies[i].getValue();
12                }
13            }
14    }
15    //当用户名和密码不为空时,自动登录
16    if((username!=null&&!("".equals(username)))&&(password!=null&&!("".equals(password)))){
17            session.setAttribute("username", username);
18            session.setAttribute("password", password);
19            response.sendRedirect(request.getContextPath()+"/LoginServlet");//get请求
20    }
21  %>


复制代码

 三、LoginServlet做自动登录检验,同样是传值到数据库校验,此时,若校验成功则带着用户名信息到Index.jsp,若检验失败则重定向至exitServlet执行删除cookies操作

复制代码

 1 //1.通过session得到参数
 2         HttpSession session = request.getSession();
 3         String username = (String)session.getAttribute("username");
 4         String password = (String)session.getAttribute("password");
 5         //进行if判断,防止有用户直接通过URL带参数的形式进行访问
 6         if(username!=null&&password!=null){
 7         //2.连接数据库进行数据校验
 8             User user = LogSercice.Instance().checkLogin(username,password);
 9             if(user==null){
10                 //返回处理删除cookies的servlet
11                 response.sendRedirect(request.getContextPath()+"/exitServlet");
12             }else {
13                 //登陆成功,跳转到主页面,并显示"欢迎您,XXX"
14                 
15                 response.sendRedirect(request.getContextPath()+"/Index.jsp");
16             }
17         }
18         
19     }

复制代码

 

四、若失败至SexitServlet删除cookies 

 

复制代码

 1 //1.删除会话
 2         HttpSession session = request.getSession();
 3         session.invalidate();
 4         //2.得到cookies
 5         Cookie[] cookies = request.getCookies();
 6         //3.遍历删除cookies中的值
 7         for(int i=0;i<cookies.length;i++){
 8             Cookie cookie = cookies[i];
 9             cookie.setMaxAge(0);
10             cookie.setValue(null);
11             cookie.setPath(request.getContextPath()+"/");
12             response.addCookie(cookie);
13         }
14         response.sendRedirect(request.getContextPath()+"/Login.jsp");
15     }

复制代码

 

 

 

五、当成功进入到Index.jsp中,此时想按下超链接退出登录(即下次不再执行自动登录时), 超链接的href直接链接至exitServlet即可

1 href="<%=basePath%>exitServlet"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值