美文网首页ssh
Cookie应用案例——记住用户名

Cookie应用案例——记住用户名

作者: 嗷老板 | 来源:发表于2018-04-08 18:37 被阅读9次

      我们在网站上登陆的时候,一般都会有一个选择框,记住用户名,这个功能的实现方式就是使用cookie实现的。

    完成这个案例我们需要三个页面:

    • LoginUIServlet 登录UI界面,只负责展示页面
    • LoginServlet 提交之后的跳转页面,负责处理逻辑判断
    • IndexServlet 登录成功之后的跳转页面

    完成LoginUIServlet

    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.itheima.cookie.CookieUtils;
    
    
    public class LoginUIServlet extends HttpServlet {
       
       protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           //设置响应类型和编码
           response.setContentType("text/html;charset=UTF-8");
           //获取输出流对象
           PrintWriter out = response.getWriter();
           Cookie[] cookies = request.getCookies();
           Cookie cookie = CookieUtils.getCookie(cookies, "username");
           String username = "";
           String checked = "";
           
           //如果cookie存在,那么设置用户名和复选框选择状态
           if(cookie != null){
               username = cookie.getValue();
               checked = "checked = 'checked'";
           }
           
           //输出html页面
           out.println("<html>");
           out.println("<head>");
           out.println("<title>登录页面</title>");
           out.println("</head>");
           out.println("<body>");
           out.println("<form action='/web08/LoginServlet' method='post'>");
           out.println("用户名:<input type='text' name='username' value='"+username+"'/>");
           out.println("<br />");
           out.println("密码:<input type='text' name='password' />");
           out.println("<br />");
           out.println("<input type='submit' value='登录' />");
           out.println("<input type='checkbox' name='remember' value='1' "+checked+"/>记住用户名");
           out.println("</form>");
           out.println("</body>");
           out.println("</html>");
       }
    
       
       protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           // TODO Auto-generated method stub
           doGet(request, response);
       }
    
    }
    

    完成LoginServlet

    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class LoginServlet extends HttpServlet {
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //设置响应类型和编码
            response.setContentType("text/html;charset=UTF-8");
            //获取输出流
            PrintWriter out = response.getWriter();
            
            //获取表单中的数据
            String username = request.getParameter("username");
            String password = request.getParameter("password");
            String remember = request.getParameter("remember");
            
            //判断用户名和密码是否正确
            if("zhangsan".equals(username) && "123456".equals(password)){
                //用户名和密码正确,返回提示信息,跳转到首页
                out.println("登陆成功!3秒后跳转到首页!");
                response.setHeader("Refresh", "3;URL=/web08/IndexServlet");
                
                //判断用户是否记住用户名
                if("1".equals(remember)){
                    //用户需要记住用户名,则创建cookie对象保存用户名
                    Cookie c = new Cookie("username",username);
                    c.setMaxAge(Integer.MAX_VALUE);
                    response.addCookie(c);
                }else{
                    //用户不需要记住用户名
                    Cookie c =new Cookie("username","");
                    c.setMaxAge(0);
                    response.addCookie(c);
                }
            }else{
                //用户名或者密码错误
                out.println("用户名或密码错误!3秒后跳转到登陆页面");
                response.setHeader("Refresh", "3;URL=/web08/LoginUIServlet");
            }
        }
    
        
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    

    完成简单的首页IndexServlet

    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    public class IndexServlet extends HttpServlet {
       
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //设置响应类型和编码
            response.setContentType("text/html;charset=UTF-8");
            //获取输出流对象
            PrintWriter out = response.getWriter();
            
            out.println("<h1>我是首页</h1>");
        }
    
        
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }
    

    案例实现结果

    登录页面
    登录成功
    再次打开登录界面

    相关文章

      网友评论

        本文标题:Cookie应用案例——记住用户名

        本文链接:https://www.haomeiwen.com/subject/dsdmhftx.html