已知数据库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页面。 提交代码:User实体类、UserDao类、login.html页面、LoginServlet。
时间: 2024-03-18 17:40:00 浏览: 50
以下是代码示例:
User实体类:
```java
public class User {
private String username;
private String password;
private String realname;
private int state;
private int failcount;
public User() {
}
public User(String username, String password, String realname, int state, int failcount) {
this.username = username;
this.password = password;
this.realname = realname;
this.state = state;
this.failcount = failcount;
}
// Getters and setters
}
```
UserDao类:
```java
public class UserDao {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
// 查询用户信息
public User findByUsernameAndPassword(String username, String password) {
User user = null;
try {
conn = DBUtils.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 {
DBUtils.close(rs, ps, conn);
}
return user;
}
// 修改用户状态
public void updateState(String username, int failCount) {
try {
conn = DBUtils.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 {
DBUtils.close(null, ps, conn);
}
}
}
```
login.html页面:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<form action="login" method="post">
<div>
<label for="username">账号:</label>
<input type="text" id="username" name="username">
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" name="password">
</div>
<button type="submit">登录</button>
</form>
</body>
</html>
```
LoginServlet:
```java
public class LoginServlet extends HttpServlet {
private UserDao userDao = new UserDao();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
User user = userDao.findByUsernameAndPassword(username, password);
if (user != null) {
if (user.getState() == 2) {
// 用户被锁定
req.setAttribute("errorMsg", "该用户已被锁定!");
req.getRequestDispatcher("/login.html").forward(req, resp);
} else {
// 登录成功
req.getSession().setAttribute("user", user);
resp.sendRedirect("/main.html");
}
} else {
// 登录失败
int failCount = 0;
User failUser = userDao.findByUsernameAndPassword(username, "");
if (failUser != null) {
failCount = failUser.getFailcount() + 1;
userDao.updateState(username, failCount);
}
if (failCount == 3) {
req.setAttribute("errorMsg", "您已连续三次输入错误密码,该用户已被锁定!");
} else {
req.setAttribute("errorMsg", "账号或密码错误!");
}
req.getRequestDispatcher("/login.html").forward(req, resp);
}
}
}
```
注意:以上代码示例仅供参考,具体实现还需要根据实际情况进行修改和完善。
阅读全文