码迷,mamicode.com
首页 > 编程语言 > 详细

Java Web 自动登录

时间:2016-11-03 09:48:52      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:不为   信息   ons   生成   etc   redirect   传值   for   ice   

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

 

 1 //若用户选择自动登录,则生成cookies保存必要信息
 2                 if("auto".equals(autoLogin)){
 3                     Cookie cookie = new Cookie("username", username);
 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                    username = cookies[i].getValue();
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"

 

Java Web 自动登录

标签:不为   信息   ons   生成   etc   redirect   传值   for   ice   

原文地址:http://www.cnblogs.com/ccw95/p/6025216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!