已知数据库cake_db,包含用户表tbl_user,该表包括username(varchar,账号,primarykey),password(varchar,密码),realname(varchar,姓名),state(int,状态,1-正常,2-锁定),failcount(int,登陆失败次数)5个字段。 1.要求编写UserDao,实现 (1)用户的登录功能,方法定义如下: public User findByUsernameAndPassword(String username, String password); (2)修改用户状态的功能,方法声明如下: public void updateState(String username, int failCount); 2.编写login.html页面用于登录,要求包含1个表单,又账号和密码表单元素。 3.编写LoginServlet,获取login.html页面表单数据,并调用UserDao的登录功能,如果登录成功,跳转到main.html页面;如果登录失败则将该用户登陆失败次数加1,再跳回login.html页面。
时间: 2024-03-24 20:40:10 浏览: 106
《SQL入门经典 第五版 》数据库创建脚本
1. UserDao的实现:
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
// 数据库连接池
private ConnectionPool connectionPool;
public UserDao() {
connectionPool = ConnectionPool.getInstance();
}
/**
* 用户登录功能
* @param username 用户名
* @param password 密码
* @return 如果登录成功,返回用户对象;否则返回null
*/
public User findByUsernameAndPassword(String username, String password) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
conn = connectionPool.getConnection();
String sql = "SELECT * FROM tbl_user WHERE username = ? AND password = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
ps.setString(2, password);
rs = ps.executeQuery();
if (rs.next()) {
user = new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setRealname(rs.getString("realname"));
user.setState(rs.getInt("state"));
user.setFailcount(rs.getInt("failcount"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
connectionPool.releaseConnection(conn);
}
}
return user;
}
/**
* 修改用户状态的功能
* @param username 用户名
* @param failCount 失败次数
*/
public void updateState(String username, int failCount) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = connectionPool.getConnection();
String sql = "UPDATE tbl_user SET state = ?, failcount = ? WHERE username = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, failCount >= 3 ? 2 : 1);
ps.setInt(2, failCount);
ps.setString(3, username);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
connectionPool.releaseConnection(conn);
}
}
}
}
```
2. login.html页面:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<form action="login" method="POST">
<label>Username:</label>
<input type="text" name="username"><br>
<label>Password:</label>
<input type="password" name="password"><br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
3. LoginServlet的实现:
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
private UserDao userDao;
@Override
public void init() throws ServletException {
userDao = new UserDao();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取表单数据
String username = req.getParameter("username");
String password = req.getParameter("password");
// 调用UserDao的登录功能
User user = userDao.findByUsernameAndPassword(username, password);
if (user != null) {
// 登录成功,将用户信息存储到Session中
HttpSession session = req.getSession();
session.setAttribute("user", user);
// 跳转到main.html页面
resp.sendRedirect("main.html");
} else {
// 登录失败,将该用户登陆失败次数加1,再跳回login.html页面
HttpSession session = req.getSession();
Integer failCount = (Integer) session.getAttribute("failCount");
failCount = failCount == null ? 1 : failCount + 1;
session.setAttribute("failCount", failCount);
userDao.updateState(username, failCount);
resp.sendRedirect("login.html");
}
}
}
```
阅读全文