fix<登录流程>

master
liyansheng 2024-12-20 23:41:14 +08:00
parent d2febc02e5
commit f507def213
6 changed files with 105 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package example.controller;
import example.service.IUserService;
import example.service.impl.UserServiceImpl;
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;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
IUserService userService = new UserServiceImpl();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
String username = req.getParameter("username");
String password = req.getParameter("password");
try {
boolean login = userService.login(username, password);
if(login){
req.getSession().setAttribute("username",username);
resp.sendRedirect("/index.jsp");
}else{
req.getSession().setAttribute("msg","用户名或密码错误");
resp.sendRedirect("/msg.jsp");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("/login.jsp");
}
}

View File

@ -21,6 +21,11 @@ public class UserDao {
return queryRunner.query(sql, new BeanHandler<>(User.class), id);
}
public User getUserByUsername(String username) throws Exception {
String sql = "SELECT * FROM user WHERE username = ?";
return queryRunner.query(sql, new BeanHandler<>(User.class), username);
}
public List<User> getAllUser() throws Exception {
String sql = "SELECT * FROM user";
return queryRunner.query(sql, new BeanListHandler<>(User.class));
@ -40,4 +45,5 @@ public class UserDao {
UserDao userDao = new UserDao();
System.out.println(userDao.getUserById(1).getUsername());
}
}

View File

@ -0,0 +1,5 @@
package example.service;
public interface IUserService {
boolean login(String username, String password) throws Exception;
}

View File

@ -0,0 +1,15 @@
package example.service.impl;
import example.dao.UserDao;
import example.model.User;
import example.service.IUserService;
public class UserServiceImpl implements IUserService {
UserDao userDao=new UserDao();
@Override
public boolean login(String username, String password) throws Exception {
User user = userDao.getUserByUsername(username);
return user != null && user.getPassword().equals(password);
}
}

21
src/main/webapp/login.jsp Normal file
View File

@ -0,0 +1,21 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<script src="https://www.liyansheng.top/cdn/watermark.js"></script>
</head>
<body>
<h2>-电脑商城-用户登录</h2>
<form action="login" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required><br><br>
<button type="submit">登录</button>
</form>
<br>
</body>
</html>

16
src/main/webapp/msg.jsp Normal file
View File

@ -0,0 +1,16 @@
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
<script src="https://www.liyansheng.top/cdn/watermark.js"></script>
</head>
<body>
<h2>提示:</h2>
<h3>${sessionScope.msg}</h3>
<button onclick="window.history.back();">返回</button>
</body>
</html>