Servlet记住用户名

package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(
        name = "ToLoginLServlet",
        urlPatterns = "/toLogin"
)
public class ToLoginLServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String msg = "";//复选框被勾住
        //2.获取客户端cookie中的username
        Cookie[] cookies = req.getCookies();
        String username = "";//用户名
        //判断是否勾选 ,如果勾了就是on,刚开始肯定没有,就不会进if
        //如果勾了,就进去找对应的Cookie,并修改值。
        boolean cRemember = false;
        if (cookies != null && cookies.length > 0) {
            for (Cookie cookie : cookies) {
                if ("cRemember".equals(cookie.getName())) {
                    if ("on".equals(cookie.getValue())) {
                        //代表上次勾了记住密码,前台要显示勾住
                        cRemember = true;
                        break;
                    }

                }
            }
            //如果勾了,用户名回显操作

            if (cRemember) {
                msg = "checked=\"checked\"";
                for (Cookie cookie : cookies) {
                    if ("username".equals(cookie.getName())) {
                        username = cookie.getValue();
                        break;
                    }
                }
            }
        }
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>\n" +
                "<html>\n" +
                "<head>\n" +
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
                "<title>登陆</title>\n" +
                "<script type=\"text/javascript\">\n" +
                "\tfunction checkLogin(){\n" +
                "\t\t//1.获取用户名\n" +
                "\t\tvar tel = document.getElementById(\"tel\").value;\n" +
                "\t\t//2.获取密码\n" +
                "\t\tvar pwd = document.getElementById(\"pwd\").value;\n" +
                "\t\t//3.判断\n" +
                "\t\tif(tel==null || tel.length==0){\n" +
                "\t\t\talert(\"用户名不能为空\");\n" +
                "\t\t\treturn false;\n" +
                "\t\t}\n" +
                "\t\tif(pwd==null || pwd.length==0){\n" +
                "\t\t\talert(\"密码不能为空\");\n" +
                "\t\t\treturn false;\n" +
                "\t\t}\n" +
                "\t\treturn true;\n" +
                "\t}\n" +
                "</script>\n" +
                "</head>\n" +
                "<body>\n" +
                "\t<form action=\"login\" method=\"post\" onsubmit=\"return checkLogin()\">\n" +
                "\t\t用户名:<input value=\"" + username + "\" type=\"text\" name=\"username\" id=\"tel\"><br/>\n" +
                "\t\t密码:<input type=\"password\" name=\"password\" id=\"pwd\"><br/>\n" +
                "\t\t<input type=\"checkbox\" name=\"remember\" " + msg + ">记住密码<br/><input type=\"submit\" value=\"登陆\">\n" +
                "\t</form>\n" +
                "</body>\n" +
                "</html>");


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}
package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(
        name = "LoginServlet",
        urlPatterns = "/login"
)
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //获得用户名
        String username = req.getParameter("username");
        //获得密码
        String password = req.getParameter("password");
        //获取是否勾选
        String remember = req.getParameter("remember");
        //对接,用户名密码是否正确,如果是第一次,下面都不会进入!!!
        if ("root".equals(username) && "52Java".equals(password)) {
            //成功,判断用户是否勾选,如果勾了就存入Cookie
            if ("on".equals(remember)) {
                //1.准备一个Cookie
                Cookie cUsername = new Cookie("username", username);
                Cookie cRemember = new Cookie("cRemember", "on");
                //存最大年龄 秒
                cUsername.setMaxAge(60 * 10);
                cRemember.setMaxAge(60 * 10);
                //2.响应到客户端
                resp.addCookie(cUsername);
                resp.addCookie(cRemember);
            } else {//如果没有勾,就清除掉
                Cookie[] cookies = req.getCookies();

                if (cookies != null && cookies.length > 0) {
                    for (Cookie cookie : cookies) {
                        if ("username".equals(cookie.getName())) {
                            cookie.setMaxAge(0);

                        }
                        if ("cRemember".equals(cookie.getName())) {
                            cookie.setMaxAge(0);

                        }
                        resp.addCookie(cookie);
                    }
                }
            }
            //3.转发,这个页面随意,代表登录成功要看到的页面,比如显示所有。。。
            req.getRequestDispatcher("index.jsp").forward(req, resp);
        } else {
            //重定向到登陆页,这说明用户名,密码不匹配。
            resp.sendRedirect("toLogin");
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

汤永红

一分也是爱

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

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

打赏作者

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

抵扣说明:

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

余额充值