添加用户登录
This commit is contained in:
4
src/com/hellogithub/dao/AdminDao.java
Normal file
4
src/com/hellogithub/dao/AdminDao.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package com.hellogithub.dao;
|
||||||
|
|
||||||
|
public class AdminDao {
|
||||||
|
}
|
||||||
48
src/com/hellogithub/dao/UserDao.java
Normal file
48
src/com/hellogithub/dao/UserDao.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package com.hellogithub.dao;
|
||||||
|
|
||||||
|
import com.hellogithub.entity.UserEntity;
|
||||||
|
import com.hellogithub.utils.JdbcUtils;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户Dao层
|
||||||
|
* */
|
||||||
|
public class UserDao {
|
||||||
|
private UserEntity userEntity=new UserEntity();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户登录
|
||||||
|
* @param userName
|
||||||
|
* @param userPassword
|
||||||
|
* */
|
||||||
|
public UserEntity UserLogin(String userName,String userPassword){
|
||||||
|
ResultSet resultSet = null;
|
||||||
|
PreparedStatement preparedStatement = null;
|
||||||
|
Connection connection = null;
|
||||||
|
try {
|
||||||
|
connection = JdbcUtils.getConnection();
|
||||||
|
String loginSql = "select * from user where userName=? and userPassword=? and isValid=1;";
|
||||||
|
preparedStatement = connection.prepareStatement(loginSql);
|
||||||
|
preparedStatement.setString(1, userName);
|
||||||
|
preparedStatement.setString(2, userPassword);
|
||||||
|
resultSet = preparedStatement.executeQuery();
|
||||||
|
if (!resultSet.next()) { // 查询不到用户数据
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
// 将db中数据 返回给客户端 查询到数据
|
||||||
|
Integer id = resultSet.getInt(1);
|
||||||
|
String dbUserName = resultSet.getString(2);
|
||||||
|
String dbUserPassword = resultSet.getString(3);
|
||||||
|
UserEntity userEntity = new UserEntity(dbUserName, dbUserPassword);
|
||||||
|
return userEntity;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
JdbcUtils.closeConnection(resultSet, preparedStatement, connection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
4
src/com/hellogithub/entity/AdminEntity.java
Normal file
4
src/com/hellogithub/entity/AdminEntity.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package com.hellogithub.entity;
|
||||||
|
|
||||||
|
public class AdminEntity {
|
||||||
|
}
|
||||||
107
src/com/hellogithub/entity/UserEntity.java
Normal file
107
src/com/hellogithub/entity/UserEntity.java
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package com.hellogithub.entity;
|
||||||
|
|
||||||
|
import java.sql.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户实体层
|
||||||
|
*/
|
||||||
|
public class UserEntity {
|
||||||
|
/**
|
||||||
|
* CREATE TABLE `user` (
|
||||||
|
* `userId` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
|
||||||
|
* `userName` varchar(255) DEFAULT NULL COMMENT '用户名',
|
||||||
|
* `userPassword` varchar(255) DEFAULT NULL COMMENT '密码',
|
||||||
|
* `creatTime` datetime DEFAULT NULL COMMENT '创建时间',
|
||||||
|
* `isValid` int(11) DEFAULT NULL COMMENT '是否有效(0/1)',
|
||||||
|
* `projectNum` int(11) DEFAULT NULL COMMENT '提交的项目总数',
|
||||||
|
* PRIMARY KEY (`userId`)
|
||||||
|
* ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COMMENT='用户表';
|
||||||
|
*/
|
||||||
|
private Integer userId;
|
||||||
|
private String userName;
|
||||||
|
private String userPassword;
|
||||||
|
|
||||||
|
private Date creatTime;
|
||||||
|
|
||||||
|
private Integer isValid;
|
||||||
|
|
||||||
|
private Integer projectNum;
|
||||||
|
|
||||||
|
public UserEntity() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntity(String userName, String userPassword) {
|
||||||
|
this.userName=userName;
|
||||||
|
this.userPassword=userPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(Integer userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserName() {
|
||||||
|
return userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserName(String userName) {
|
||||||
|
this.userName = userName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUserPassword() {
|
||||||
|
return userPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserPassword(String userPassword) {
|
||||||
|
this.userPassword = userPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getCreatTime() {
|
||||||
|
return creatTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreatTime(Date creatTime) {
|
||||||
|
this.creatTime = creatTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getIsValid() {
|
||||||
|
return isValid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsValid(Integer isValid) {
|
||||||
|
this.isValid = isValid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getProjectNum() {
|
||||||
|
return projectNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProjectNum(Integer projectNum) {
|
||||||
|
this.projectNum = projectNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserEntity(Integer userId, String userName, String userPassword, Date creatTime, Integer isValid, Integer projectNum) {
|
||||||
|
this.userId = userId;
|
||||||
|
this.userName = userName;
|
||||||
|
this.userPassword = userPassword;
|
||||||
|
this.creatTime = creatTime;
|
||||||
|
this.isValid = isValid;
|
||||||
|
this.projectNum = projectNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "UserEntity{" +
|
||||||
|
"userId=" + userId +
|
||||||
|
", userName='" + userName + '\'' +
|
||||||
|
", userPassword='" + userPassword + '\'' +
|
||||||
|
", creatTime=" + creatTime +
|
||||||
|
", isValid=" + isValid +
|
||||||
|
", projectNum=" + projectNum +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,7 +14,7 @@ import java.io.IOException;
|
|||||||
*/
|
*/
|
||||||
@WebFilter("/system/*")// 过滤器所有的请求
|
@WebFilter("/system/*")// 过滤器所有的请求
|
||||||
public class SessionFilter implements Filter {
|
public class SessionFilter implements Filter {
|
||||||
private String[] excludeUrls = new String[]{"/VerifycodeServlet"}; //过滤器排除的请求url 格式:”/login“
|
private String[] excludeUrls = new String[]{"/VerifycodeServlet","/userLogin"}; //过滤器排除的请求url 格式:”/login“
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 过滤器
|
* 过滤器
|
||||||
|
|||||||
4
src/com/hellogithub/service/AdminService.java
Normal file
4
src/com/hellogithub/service/AdminService.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package com.hellogithub.service;
|
||||||
|
|
||||||
|
public class AdminService {
|
||||||
|
}
|
||||||
14
src/com/hellogithub/service/UserService.java
Normal file
14
src/com/hellogithub/service/UserService.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package com.hellogithub.service;
|
||||||
|
|
||||||
|
import com.hellogithub.dao.UserDao;
|
||||||
|
import com.hellogithub.entity.UserEntity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户Service层
|
||||||
|
* */
|
||||||
|
public class UserService {
|
||||||
|
private UserDao userDao=new UserDao();
|
||||||
|
public UserEntity UserLogin(String userName, String userPassword){
|
||||||
|
return userDao.UserLogin(userName,userPassword);
|
||||||
|
}
|
||||||
|
}
|
||||||
86
src/com/hellogithub/servlet/UserLoginServlet.java
Normal file
86
src/com/hellogithub/servlet/UserLoginServlet.java
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
package com.hellogithub.servlet;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.hellogithub.entity.UserEntity;
|
||||||
|
import com.hellogithub.service.UserService;
|
||||||
|
import com.hellogithub.utils.RandomValidateCode;
|
||||||
|
import jakarta.servlet.ServletException;
|
||||||
|
import jakarta.servlet.annotation.WebServlet;
|
||||||
|
import jakarta.servlet.http.*;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
@WebServlet("/userLogin")
|
||||||
|
public class UserLoginServlet extends HttpServlet {
|
||||||
|
private UserService userService = new UserService();
|
||||||
|
|
||||||
|
@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 {
|
||||||
|
PrintWriter writer =resp.getWriter();
|
||||||
|
String userName = req.getParameter("username");
|
||||||
|
if (StringUtils.isEmpty(userName)) {
|
||||||
|
setResultError("用户名称不能够是为空",writer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String userPwd = req.getParameter("password");
|
||||||
|
// 参数验证
|
||||||
|
if (StringUtils.isEmpty(userPwd)) {
|
||||||
|
//转发到错误页面
|
||||||
|
setResultError("密码不能够是为空",writer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String userCode = req.getParameter("code"); // 用户输入的图形验证码
|
||||||
|
// 从session中获取图形验证码
|
||||||
|
HttpSession session = req.getSession();
|
||||||
|
String sessionCode = (String) session.getAttribute(RandomValidateCode.RANDOMVALIDATECODE);
|
||||||
|
if (!sessionCode.equalsIgnoreCase(userCode)) {
|
||||||
|
setResultError("验证码不能够是为空",writer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 在调用业务逻辑层
|
||||||
|
UserEntity adminUserEntity = userService.UserLogin(userName, userPwd);
|
||||||
|
if (adminUserEntity == null) {
|
||||||
|
// 用户名称或者密码错误!
|
||||||
|
setResultError("用户名或者密码错误!!!",writer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 判断用户是否记住密码
|
||||||
|
String rememberPassword = req.getParameter("remember");
|
||||||
|
if ("on".equals(rememberPassword)) {
|
||||||
|
// 如果有记住密码则 将密码保存在cookie中
|
||||||
|
Cookie userNameCookie = new Cookie("userName", userName);
|
||||||
|
Cookie userPwdCookie = new Cookie("userPwd", userPwd);
|
||||||
|
resp.addCookie(userNameCookie);
|
||||||
|
resp.addCookie(userPwdCookie);
|
||||||
|
}
|
||||||
|
// 能够db中查询到对象 登录成功了 将用户数据存放在session中
|
||||||
|
session = req.getSession();
|
||||||
|
session.setAttribute("user", adminUserEntity);
|
||||||
|
setResultOK("登录成功!",writer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResult(Integer code, String msg, PrintWriter writer) {
|
||||||
|
HashMap<String, Object> result = new HashMap<>();
|
||||||
|
result.put("code", code);
|
||||||
|
result.put("msg", msg);
|
||||||
|
String jsonString = JSONObject.toJSONString(result);
|
||||||
|
writer.println(jsonString);
|
||||||
|
writer.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResultError(String msg, PrintWriter writer) {
|
||||||
|
setResult(500, msg, writer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResultOK(String msg, PrintWriter writer) {
|
||||||
|
setResult(200, msg, writer);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user