JavaWed登录页面实现自动登录

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

//若用户选择自动登录,则生成cookies保存必要信息
                if("auto".equals(autoLogin)){
          //cookies需要设置编码格式             Cookie cookie = new Cookie("username", URLEncoder.encode(username, "utf-8"));
                    Cookie cookie2 = new Cookie("password",password);
                    //设置保存时间
                    cookie.setMaxAge(7*24*60*60);
                    cookie2.setMaxAge(7*24*60*60);
                    //设置保存路径
                    cookie.setPath(request.getContextPath()+"/");
                    //添加到响应头
                    response.addCookie(cookie);
                    response.addCookie(cookie2);
                }

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

<%
   Cookie[] cookies=request.getCookies();
   String username = null;
   String password = null;
   if(cookies!=null){
           for(int i=0;i<cookies.length;i++){
               String name = cookies[i].getName();
               if("username".equals(name)){
          //如果是中文,cookies需要解码            username = URLDecoder.decode(cookies[i].getValue(), "utf-8");
               }else if("password".equals(name)){
                   password = cookies[i].getValue();
               }
           }
   }
   //当用户名和密码不为空时,自动登录
   if((username!=null&&!("".equals(username)))&&(password!=null&&!("".equals(password)))){
           session.setAttribute("username", username);
           session.setAttribute("password", password);
           response.sendRedirect(request.getContextPath()+"/LoginServlet");//get请求
   }
 %>

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

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

                response.sendRedirect(request.getContextPath()+"/Index.jsp");
            }
        }

    }

四、若失败至SexitServlet删除cookies

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

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值