Cookie是实现记住用户名实例

Cookie是实现记住用户名实例

记住用户名的功能,登入成功后,给响应消息头添加用户名的Cookie信息,在登入页面时,加载cookie信息,通过cookie获取用户名信息,写在输入框中。

SimpleLoginServlet(简单的登入页面):

@WebServlet({ "/SimpleLoginServlet", "/simple/login" })
public class SimpleLoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public SimpleLoginServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        PrintWriter out = response.getWriter();

        String value = "";
        String check="checked='checked'";

        Cookie[] cookies = request.getCookies();
        if(cookies!=null ){
            for (int i = 0;i < cookies.length; i++) {
                if ("username".equals(cookies[i].getName())) {
                    value = cookies[i].getValue();
                    System.out.println(value);
                }
            }
            value = "value='"+value+"'";
        }


        out.print("<form action='"+request.getContextPath()+"/simple/login/success' method='post'>");
        out.print("用户名<input type='text' name='username' "+value+"/><br/>");
        out.print("密码<input type='password' name='password'/><br/>");
        out.print("<input type='checkbox' name='check' "+check+"/>记住用户名<br/>");
        out.print("<input type='submit' value='登入'/><br/>");
        out.print("</form>");

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

LoginSuccessServlet(登入成功页面)

@WebServlet({ "/LoginSuccessServlet", "/simple/login/success" })
public class LoginSuccessServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public LoginSuccessServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String check = request.getParameter("check");

        Cookie cookie = new Cookie("username", username);
        cookie.setPath(request.getContextPath());
        if ("tuhao".equals(username) && "123456".equals(password)) {
            //check部位null,表示记住用户名了
            if(check!=null){
                cookie.setMaxAge(5*60);
                response.addCookie(cookie);
                out.println("登入成功,已经记住用户名");
            }else {
                cookie.setMaxAge(0);
                response.addCookie(cookie);
                out.print("不记住用户名");
            }
        }else {
            cookie.setMaxAge(0);
            response.addCookie(cookie);
            out.print("清除用户名");
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值