案例:自动登录,记住密码(Session+JSP)

2022-07-27,,,,

用来判断cookie有没有值

package com.bdit;

import javax.servlet.http.Cookie;

public class DBCookie {
    public static Cookie getCookie(Cookie[] cookies,String key){
        if(cookies!=null&&cookies.length>0){
            for(Cookie cookie:cookies){
                if(cookie.getName().equals(key)){
                    return cookie;
                }
            }
        }
        return null;
    }
}

servlet

package com.bdit.servlet;

import com.bdit.sevice.TreeSevice;
import com.bdit.vo.Tree;

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(urlPatterns = {"/treeServlet"})
public class TreeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");

        String username=req.getParameter("username");
        String pass=req.getParameter("pass");
        String hobby=req.getParameter("hobby");
        String hobby2=req.getParameter("hobby2");

        TreeSevice sevice=new TreeSevice();
        //根据用户名和密码查找
        Tree tree=sevice.find(username,pass);

        if(tree!=null&&tree.getUsername().equals(username)&&tree.getPass().equals(pass)){
            if(hobby2!=null&&hobby2.equals("2")){
            if(hobby!=null&&hobby.equals("1")){
                Cookie cookie1=new Cookie("username",username);
                Cookie cookie2=new Cookie("pass",pass);
                cookie1.setPath(req.getContextPath());
                cookie2.setPath(req.getContextPath());
                cookie1.setMaxAge(60*60*24);
                cookie2.setMaxAge(60*60*24);
                resp.addCookie(cookie1);
                resp.addCookie(cookie2);
                Cookie cookie3=new Cookie("hobby","checked");
                cookie3.setPath(req.getContextPath());
                cookie3.setMaxAge(60*60*24);
                resp.addCookie(cookie3);
                Cookie cookie4=new Cookie("hobby2","checked");
                cookie4.setPath(req.getContextPath());
                cookie4.setMaxAge(15);
                resp.addCookie(cookie4);
                resp.sendRedirect("success.jsp");
            }
            else{
                resp.sendRedirect("success.jsp");
            }
            }else{
                if(hobby!=null&&hobby.equals("1")) {
                    Cookie cookie1 = new Cookie("username", username);
                    Cookie cookie2 = new Cookie("pass", pass);
                    cookie1.setPath(req.getContextPath());
                    cookie2.setPath(req.getContextPath());
                    cookie1.setMaxAge(60 * 60 * 24);
                    cookie2.setMaxAge(60 * 60 * 24);
                    resp.addCookie(cookie1);
                    resp.addCookie(cookie2);
                    Cookie cookie3 = new Cookie("hobby", "checked");
                    cookie3.setPath(req.getContextPath());
                    cookie3.setMaxAge(60 * 60 * 24);
                    resp.addCookie(cookie3);
                }else{
                    resp.sendRedirect("success.jsp");
                }
            }
        }else if(tree==null){
            resp.sendRedirect("error.jsp");
        }

    }
}

主页面index.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/10/20
  Time: 21:10
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.bdit.DBCookie" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>

  <%
      String username="";
      String pass="";
      String hobby="";
      String hobby2="";

    Cookie cookie4=DBCookie.getCookie(request.getCookies(),"hobby2");
    if(cookie4!=null){
        Cookie cookie5=DBCookie.getCookie(request.getCookies(),"hobby");
        if(cookie5!=null){
            Cookie cookie1= DBCookie.getCookie(request.getCookies(),"username");
            if(cookie1!=null){
                username=cookie1.getValue();
            }
            Cookie cookie2=DBCookie.getCookie(request.getCookies(), "pass");
            if(cookie2!=null){
                pass=cookie2.getValue();
            }
            hobby=cookie5.getValue();
        }
        hobby2=cookie4.getValue();
    }else{
        Cookie cookie3=DBCookie.getCookie(request.getCookies(),"hobby");
        if(cookie3!=null){
            Cookie cookie1= DBCookie.getCookie(request.getCookies(),"username");
            if(cookie1!=null){
                username=cookie1.getValue();
            }
            Cookie cookie2=DBCookie.getCookie(request.getCookies(), "pass");
            if(cookie2!=null){
                pass=cookie2.getValue();
            }
            hobby=cookie3.getValue();
        }
    }
  %>
 <form action="treeServlet" method="post">
     账号:<input type="text" name="username" value=<%=username%> ><br>
     密码:<input type="text" name="pass" value=<%=pass%> ><br>
     <input type="radio" name="hobby" <%=hobby%> value="1">记住密码<br>
     <input type="radio" name="hobby2" <%=hobby2%> value="2">自动登录<br>
     <input type="submit">
     <%
        if(hobby2.equals("checked")){
            response.sendRedirect("treeServlet?username="+username+"&pass="+pass+"");
        }
     %>

 </form>

  </body>
</html>

成功页面success.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/10/21
  Time: 21:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>登录成功</h2>

<form action="index.jsp">
    <input type="submit" value="返回主页">
</form>
</body>
</html>

失败页面error.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/10/21
  Time: 21:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h2>登录失败</h2>
<a href="index.jsp">回主页</a>
</body>
</html>

本文地址:https://blog.csdn.net/vlucky_long/article/details/110249647

《案例:自动登录,记住密码(Session+JSP).doc》

下载本文的Word格式文档,以方便收藏与打印。