归子莫的博客

「笔杆揭不起,绘不出青烟别春泥 ————归子莫」

Java–用户登录(JDBC,MYSQL,Servlet)

博客说明

文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢!

用户登录案例需求

  1. 编写login.html登录页面
    username & password 两个输入框
  2. 使用Druid数据库连接池技术,操作mysql,day14数据库中user表
  3. 使用JdbcTemplate技术封装JDBC
  4. 登录成功跳转到SuccessServlet展示:登录成功!用户名,欢迎您
  5. 登录失败跳转到FailServlet展示:登录失败,用户名或密码错误

登录页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/login_test/loginServlet" method="post">
用户名:<input type="text" name="username"> <br>
密码:<input type="password" name="password"><br>

<input type="submit" value="登录">

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

创建数据库

1
2
3
4
5
6
7
8
9
CREATE DATABASE login;

USE login;

CREATE TABLE user(
id INT PRIMARY KEY auto_increment,
username VARCHAR(32) UNIQUE NOT NULL,
password VARCHAR(32) NOT NULL
)

配置文件

1
2
3
4
5
6
7
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///login
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000

项目依赖

image-20200622115607312

创建用户实体类

创建包cn.guizimo.domain,创建类User

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package cn.guizimo.domain;

/**
* @author tanglei
* @date 2020/6/22 10:28 上午
*/
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;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}

创建工具类

创建包cn.guizimo.util,编写工具类JDBCUtils

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cn.guizimo.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
* @author tanglei
* @date 2020/6/22 10:38 上午
*/
public class JDBCUtils {
private static DataSource ds ;

static {

try {
//1.加载配置文件
Properties pro = new Properties();
//使用ClassLoader加载配置文件,获取字节输入流
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
pro.load(is);

//2.初始化连接池对象
ds = DruidDataSourceFactory.createDataSource(pro);

} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 获取连接池对象
*/
public static DataSource getDataSource(){
return ds;
}


/**
* 获取连接Connection对象
*/
public static Connection getConnection() throws SQLException {
return ds.getConnection();
}

}

创建数据操作类

创建包cn.guizimo.dao,创建类UserDao,提供login方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package cn.guizimo.dao;

import cn.guizimo.domain.User;
import cn.guizimo.util.JDBCUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
* @author tanglei
* @date 2020/6/22 10:34 上午
*/
public class UserDao {


private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

public User login(User loginUser) {
try {
String sql = "select * from user where username = ? and password = ?";
User user = template.queryForObject(sql,
new BeanPropertyRowMapper<User>(User.class),
loginUser.getUsername(), loginUser.getPassword());
return user;
} catch (DataAccessException e) {
e.printStackTrace();
return null;
}
}
}

创建servlet类

cn.guizimo.web.servlet.LoginServlet类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package cn.guizimo.web.servlet;

import cn.guizimo.dao.UserDao;
import cn.guizimo.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author tanglei
* @date 2020/6/22 11:15 上午
*/

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.设置编码
req.setCharacterEncoding("utf-8");
//2.获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
//3.封装user对象
User loginUser = new User();
loginUser.setUsername(username);
loginUser.setPassword(password);

//4.调用UserDao的login方法
UserDao dao = new UserDao();
User user = dao.login(loginUser);

//5.判断user
if(user == null){
//登录失败
req.getRequestDispatcher("/failServlet").forward(req,resp);
}else{
//登录成功
//存储数据
req.setAttribute("user",user);
//转发
req.getRequestDispatcher("/successServlet").forward(req,resp);
}
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}

cn.guizimo.web.servlet.FailServlet类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package cn.guizimo.web.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author tanglei
* @date 2020/6/22 11:26 上午
*/
@WebServlet("/failServlet")
public class FailServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
resp.setContentType("text/html;charset=utf-8");
//输出
resp.getWriter().write("登录失败,用户名或密码错误");

}
}

cn.guizimo.web.servlet.SuccessServlet类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package cn.guizimo.web.servlet;

import cn.guizimo.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* @author tanglei
* @date 2020/6/22 11:31 上午
*/
@WebServlet("/successServlet")
public class SuccessServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取request域中共享的user对象
User user = (User) req.getAttribute("user");

if(user != null){
//设置编码
resp.setContentType("text/html;charset=utf-8");
//输出
resp.getWriter().write("登录成功!"+user.getUsername()+",欢迎您");
}
}
}

运行

在浏览器中打开http://localhost:8080/login_test/login.html

image-20200622124825184

登录成功

image-20200622125016804

登录失败

image-20200622124935764

感谢

万能的网络

以及勤劳的自己

评论