需求:1. 设计一个登录页面 2. 存在记住密码按钮 3. 登录成功,勾选 记住密码 再次登录时,默认将之前的用户名和密码填写上去

1. 设置登录界面(JSP文件)

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/12
  Time: 10:16
  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>
<form action="${ pageContext.request.contextPath}/login" method="post">

    <input type="text" name="user">
    <br>
    <input type="password" name="pwd">
    <br>
    <input type="checkbox" name="check">
    记住密码
    <input type="submit" value="登录">

</form>
</body>
</html>

<%@ page import="com.fy.domain.User" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/12
  Time: 11:16
  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>

欢迎您,<span><%= ((User)session.getAttribute("user")).getUsername() %></span>

</body>
</html>

2. 连接导数据库

  1. 配置文件导入c3p0
  2. 导入包
  3. 创建连接池
package com.fy.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;

public class JdbcUtils {
    
    
    //提供c3p0的day0904的连接池
    private static DataSource ds = new ComboPooledDataSource("myc3p0");

    public static DataSource getDataSource(){
    
    
        return ds;
    }
}

3. User

package com.fy.domain;

public class User {
    
    
    private int id;
    private String username;
    private String password;

    public int getId() {
    
    
        return id;
    }
    public void setId(int id) {
    
    
        this.id = id;
    }
    public String getUsername() {
    
    
        return username;
    }
    public void setUsername(String username) {
    
    
        this.username = username;
    }
    public String getPassword() {
    
    
        return password;
    }
    public void setPassword(String password) {
    
    
        this.password = password;
    }
}

4. UserDao(Dbutils)

package com.fy.dao;

import com.fy.domain.User;
import com.fy.utils.JdbcUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
//登录功能
public class UserDao {
    
    
    public User login(User u) throws Exception{
    
    
        QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
        String sql = "select * from user where username = ? and password = ?";
        return qr.query(sql, new BeanHandler<User>(User.class) ,u.getUsername(),u.getPassword());
    }

}

5. 代码实现

package com.fy.servlet;

import com.fy.dao.UserDao;
import com.fy.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.net.URLEncoder;

//自定义到服务器名称
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    
        //编码设置
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("user");
        String password = request.getParameter("pwd");
        User u = new User();
        u.setUsername(username);
        u.setPassword(password);

        UserDao dao = new UserDao();
        User user = null;
        try {
    
    
            user = dao.login(u);
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        if(user == null){
    
    // 登录失败

            Cookie userCookie = new Cookie("username", "");
            Cookie pwdCookie = new Cookie("password", "");
            Cookie checkCookie = new Cookie("check", "");
            userCookie.setMaxAge(0);
            pwdCookie.setMaxAge(0);
            checkCookie.setMaxAge(0);
            userCookie.setPath(request.getContextPath()+"/login.jsp");
            pwdCookie.setPath(request.getContextPath()+"/login.jsp");
            checkCookie.setPath(request.getContextPath()+"/login.jsp");
            response.addCookie(userCookie);
            response.addCookie(pwdCookie);
            response.addCookie(checkCookie);

            HttpSession session = request.getSession();
            session.setAttribute("login_error", "用户名或者密码有误");
            response.sendRedirect(request.getContextPath()+"/login.jsp");

        }
        else{
    
    // 登录成功
            String check = request.getParameter("check");
            System.out.println(check);
            if(check != null){
    
    // 勾选了记住密码
                username = URLEncoder.encode(username, "utf-8");
                Cookie userCookie = new Cookie("username", username);
                Cookie pwdCookie = new Cookie("password", password);
                Cookie checkCookie = new Cookie("check", "checked");
                userCookie.setMaxAge(600);
                pwdCookie.setMaxAge(600);
                checkCookie.setMaxAge(600);
                userCookie.setPath(request.getContextPath()+"/login.jsp");
                pwdCookie.setPath(request.getContextPath()+"/login.jsp");
                checkCookie.setPath(request.getContextPath()+"/login.jsp");
                response.addCookie(userCookie);
                response.addCookie(pwdCookie);
                response.addCookie(checkCookie);
            }else{
    
    
                Cookie userCookie = new Cookie("username", "");
                Cookie pwdCookie = new Cookie("password", "");
                Cookie checkCookie = new Cookie("check", "");
                userCookie.setMaxAge(0);
                pwdCookie.setMaxAge(0);
                checkCookie.setMaxAge(0);
                userCookie.setPath(request.getContextPath()+"/login.jsp");
                pwdCookie.setPath(request.getContextPath()+"/login.jsp");
                checkCookie.setPath(request.getContextPath()+"/login.jsp");
                response.addCookie(userCookie);
                response.addCookie(pwdCookie);
                response.addCookie(checkCookie);
            }
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            response.sendRedirect(request.getContextPath()+"/home.jsp");
        }

    }

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

6. 运行结果

数据库数据

数据库数据
服务器打开访问, 将数据库中的存储数据信息进行登录

在这里插入图片描述

登录成功

2

项目总览
3

猜你喜欢

转载自blog.csdn.net/zhu_fangyuan/article/details/108547507