自动登录功能的实现

 一.在login.jsp文件中设置:

<div align="center">
    <h1>Estore_登录</h1>
    <font color="red">${msg}</font>
    <form action="<%=request.getContextPath()%>/LoginServlet" method="post">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="username"  value='<%=ck==null?"":URLDecoder.decode(ck.getValue(),"utf-8") %>' /></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" name="password"/></td>
            </tr>
            <tr><td><input type="checkbox" name="remname" value="true"  <%=ck==null?"":"checked" %>/>记住用户名</td>
                <td><input type="checkbox" name="autologin" value="true"/>30天自动登录</td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="登录"/></td>
            </tr>
        </table>
    </form>
</div>


二.在loginServlet中doPost方法实现自动登录操作:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1 获得jsp页面传入的用户名和密码
        String name = request.getParameter("username");
        String pwd = request.getParameter("password");

       //通过MD5Utils方式进行加密

        pwd = MD5Utils.md5(pwd);
        //2 调用业务(service)类中的方法,来检查数据库中的用户名、密码与传入的用户名密码是否一致,如果一致将用户包装:

        User user=service.findUserByNameAndPwd(name,pwd);

       //如果传入的对象为空,在login.jsp页面返回用户名或密码错误,并且转发页面,因为转发只涉及到一个请求,故使用此方法

        if(user==null){
            request.setAttribute("msg", "用户名或密码错误");
            request.getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
            return;
        }
       
        //3 根据检查的结果来调转页面
        request.getSession().setAttribute("user", user);
        //记住用户名
        if("true".equals(request.getParameter("remname"))){

            Cookie remname = new Cookie("remname",URLEncoder.encode(user.getUsername(),"utf-8"));

           //设置cookie保存的最长时间为30天

            remname.setMaxAge(3600*24*30);
            remname.setPath(request.getContextPath());
            response.addCookie(remname);
            
        }
        else{

            Cookie remname = new Cookie("remname",URLEncoder.encode(user.getUsername(),"utf-8"));
            remname.setMaxAge(0);
            remname.setPath(request.getContextPath());           
            response.addCookie(remname);
        }
        
        //处理30天自动登录
        if("true".equals(request.getParameter("autologin"))){
            Cookie autologinC = new Cookie("autoLogin",URLEncoder.encode(user.getUsername(),"utf-8")+":"+user.getPassword());
            autologinC.setMaxAge(3600*24*30);
            autologinC.setPath(request.getContextPath());
            response.addCookie(autologinC);
            
        }
        response.sendRedirect("/estore/index.jsp");
    }



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世润

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值