0x01 登入模块代码实现

  1. 创建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
45
46
47
48
package ruanko.newspublish.entity;

public class Users {
private int uid;
private String username;
private String password;

public Users(){}
public Users(int uid, String username, String password) {
this.uid = uid;
this.username = username;
this.password = password;
}

public int getUid() {
return uid;
}

public void setUid(int uid) {
this.uid = uid;
}

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 "Users{" +
"uid=" + uid +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}

  1. 创建存放User数据的数据库table
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- ----------------------------
-- Table structure for users
-- ----------------------------
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`uid` int unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- ----------------------------
-- Records of users
-- ----------------------------
BEGIN;
INSERT INTO `users` VALUES (1, 'admin', '21232f297a57a5a743894a0e4a801fc3');
COMMIT;
  1. 创建login.jsp文件
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
<%--
Created by IntelliJ IDEA.
User: q1jun
Date: 2022/5/11
Time: 17:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<head>
<base href="<%=basePath%>">
<title>登入</title>
<script type="text/javascript" src="js/weui.min.js" language="JavaScript"></script>
<link href="css/weui.css" type="text/css" rel="stylesheet">
</head>
<body data-weui-theme="light">
<form class="weui-cells" action="login" method="post">
<label for="js_input1" class="weui-cell weui-cell_active">
<div class="weui-cell__hd"><span class="weui-label">用户名</span></div>
<div class="weui-cell__bd">
<input name="username" id="js_input1" class="weui-input" placeholder="填写用户名">
</div>
</label>
<label for="js_input2" class="weui-cell weui-cell_active">
<div class="weui-cell__hd"><span class="weui-label">密码</span></div>
<div class="weui-cell__bd">
<input name="password" id="js_input2" type="password" class="weui-input" placeholder="填写密码">
</div>
</label>
<p style="color: red">
<%
if(request.getAttribute("message")!=null){
out.println(request.getAttribute("message"));
}
%>
</p>
<input role="button" class="weui-btn weui-btn_primary" wah-hotarea="click" type="submit" value="确定">
</form>
</body>
</html>

  1. 修改Web.xml文件,添加servlet标签
1
2
3
4
5
6
7
8
9
10
11
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>ruanko.newspublish.action.login.login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
  1. 创建处理登入的DAO层代码
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
package ruanko.newspublish.dao;

import ruanko.newspublish.entity.Article;
import ruanko.newspublish.entity.Users;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UsersDao extends BaseDao{
public void register(Users users){
String sql = "INSERT INTO users(username, password) VALUES('"+
users.getUsername() +
"','"+
users.getPassword() +
"')";
System.out.println("SQL ={"+sql+"}");
executeUpdate(sql);
}

public Users login(String username){
String sql = "SELECT * FROM users WHERE username= '" + username+"';";
System.out.println("SQL = {"+sql+"}");
Connection connection = getConnection();
Statement statement;
try {
statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
if (resultSet.next()) {
Users users1 = new Users();
users1.setUid(resultSet.getInt("uid"));
users1.setUsername(resultSet.getString("username"));
users1.setPassword(resultSet.getString("password"));
closeAll(connection, statement, resultSet);
return users1;
}
} catch (SQLException e) {
e.printStackTrace();
System.out.println("[mysql]查询用户名或密码错误");
}
return null;
}

public void updatePassword(Users users){
String sql = "UPDATE users SET password = '"+ users.getPassword()+"' WHERE username = '" +
users.getUsername() + "';";
System.out.println("SQL ={"+sql+"}");
executeUpdate(sql);
}
}

  1. 创建处理登入的Servive层代码
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
package ruanko.newspublish.biz;

import ruanko.newspublish.dao.UsersDao;
import ruanko.newspublish.entity.Users;

public class UsersBiz {

private UsersDao usersDao;

public UsersBiz(){
this.usersDao = new UsersDao();
}

public void register(Users users){
usersDao.register(users);
}

public Users login(String username){
return usersDao.login(username);
}

public void updatePassword(Users users){
usersDao.updatePassword(users);
}
}
  1. 创建处理登入操作的Servlet代码
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
54
package ruanko.newspublish.action.login;

import ruanko.newspublish.biz.UsersBiz;
import ruanko.newspublish.entity.Users;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Objects;

public class login extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
Users users = new Users();
users.setUsername(request.getParameter("username"));
String password = request.getParameter("password");
// System.out.println(password);
String hashPassword = md5(password);
// System.out.println("MD5 = "+hashPassword);
users.setPassword(hashPassword);

UsersBiz usersBiz = new UsersBiz();
if (usersBiz.login(users.getUsername())!=null){
System.out.println("MysqlPasswordMd5 = "+usersBiz.login(users.getUsername()).getPassword());
if (Objects.equals(usersBiz.login(users.getUsername()).getPassword(), hashPassword)){
System.out.println("登入成功,跳转页面至 index.jsp");
request.getRequestDispatcher("index.jsp").forward(request,response);
}
}else {
System.out.println("登入失败,返回login.jsp");
request.setAttribute("message","登入失败,请检查用户名和密码是否正确!");
request.getRequestDispatcher("login.jsp").forward(request,response);
}


}
public static String md5(String str){
MessageDigest messageDigest = null;
try {
messageDigest = MessageDigest.getInstance("MD5"); //生成md5计算摘要
messageDigest.update(str.getBytes()); //计算md5
return new BigInteger(1,messageDigest.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
}

0x02 登入模块操作截图

image-20220512163230794

登入成功,跳转到index.jsp主界面

image-20220512163310688

登入失败,提示错误信息

image-20220512163408848