Cookie实现登录记住密码

Cookie实现记住登录密码,用户可以自由选择是否记住密码,或者用户之前选择记住了,但是某一次又不想记住了,需要将之前对应的Cookie删除掉

Cookie相当于map 也是键值对的形式,但是并不相同

response.addCookie(Cookie对象名)才会生效;

表单提交用的get 方便地址栏查看数据变化,

登录Servlet:

package ni.jun.yang.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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;


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

    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        Cookie [] cookies = request.getCookies();
        //声明用户名和密码为空字符串
        String uName = "";    
        String uPsw = "";
        //获取根据名字获取对应的cookie 的值 
        if(cookies!=null){
            for(Cookie x : cookies){
                if(x.getName().equals("userName")){
                    uName = x.getValue();
                }else if(x.getName().equals("userPsw")){
                    uPsw = x.getValue();                            
                }
            }
        }
        
        out.println("Cookie");    
        out.println("");
        out.println("");        
        //  input 标签的value 为声明的字符串,没有cookie 就是空字符串,有cookie就是获取到的用户名和密码
        out.println("
"); out.println("用户名:"); out.println("密码:"); out.println("用户名:); //如果用户名和密码都获取到内容了,就将复选框默认选中记住密码,如果没有cookie,则不会默认选中,需要手动选择 if (uName != "" && uPsw != "") { out.println("checked='checked'"); } out.println(" >记住密码"); out.println(""); out.println("); out.println(""); out.println(""); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }

数据处理代码:

package ni.jun.yang.servlet;

import java.io.IOException;
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;


@WebServlet("/ResultServlet")
public class ResultServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
           
    public ResultServlet() {
 
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //获取用户名和密码以及复选框是否选中
        String userName = request.getParameter("userName");
        String userPsw = request.getParameter("userPsw");
        String remember = request.getParameter("remember");
        // remember !=null 复选框被选中,需要设置cookie记住用户明和密码;
        if(remember != null){
            //没有连接数据库,给一个设定的用户和密码
            if(userName.equals("lisi") && userPsw.equals("123")){
                //成功匹配,说明登录成功,设置cookie
                Cookie cName = new Cookie("userName", userName);
                Cookie cPsw = new Cookie("userPsw", userPsw);
                cName.setMaxAge(300);
                cPsw.setMaxAge(300);
                response.addCookie(cName);
                response.addCookie(cPsw);
            }
            // remember =null  没有选中记住密码,或以前记住了密码,但是后来登录时取消了记住密码,则将对应的cookie清除
        }else{
            Cookie [] cookies = request.getCookies();
            if(cookies != null){
                for(Cookie x : cookies){
                    if(x.getName().equals("userName")){
                        //设置时间0 清除cookie
                        x.setMaxAge(0);
                        response.addCookie(x);
                    }else if(x.getName().equals("userPsw")){
                        x.setMaxAge(0);    
                        response.addCookie(x);
                    }
                }
            }
            
        }
    }

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

}

 

你可能感兴趣的:(Cookie实现登录记住密码)