protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String formType = req.getParameter("formType"); req.setCharacterEncoding("UTF-8"); HttpSession session = req.getSession(); if("update".equals(formType)){ String id = req.getParameter("id"); String name = req.getParameter("name"); String sex = req.getParameter("sex"); String phone = req.getParameter("phone"); String relation = req.getParameter("relation"); String userId = req.getParameter("userId"); contactRepository.updateById(id,name,sex,phone,relation,userId); resp.sendRedirect("/contact"); } else if("insert".equals(formType)){ String name = req.getParameter("name"); String sex = req.getParameter("sex"); String phone = req.getParameter("phone"); String relation = req.getParameter("relation"); String userId = req.getParameter("userId"); contactRepository.add(name,sex,phone,relation,userId); resp.sendRedirect("/contact");
时间: 2024-04-16 07:25:22 浏览: 210
这段代码是一个使用Java编写的Servlet的doPost方法,用于处理HTTP POST请求。根据传入的formType参数的不同值,执行不同的操作。
如果formType的值为"update",则表示进行联系人信息的更新操作。代码中获取了id、name、sex、phone、relation和userId等参数,并调用contactRepository.updateById方法来更新联系人信息。更新完成后,通过resp.sendRedirect方法将请求重定向到"/contact"页面。
如果formType的值为"insert",则表示进行联系人信息的插入操作。代码中获取了name、sex、phone、relation和userId等参数,并调用contactRepository.add方法来插入联系人信息。插入完成后,同样通过resp.sendRedirect方法将请求重定向到"/contact"页面。
这段代码假设了一个contactRepository对象用于处理数据库中的联系人信息。具体的数据库操作逻辑不在这段代码中展示,可能需要根据具体情况进行实现。
此外,代码中还设置了字符编码为UTF-8,并获取了当前会话的HttpSession对象。
请注意,这只是代码片段,并不能独立运行,可能还需要其他部分的代码来完善功能。
相关问题
已知数据库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。
以下是代码示例:
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);
}
}
}
```
注意:以上代码示例仅供参考,具体实现还需要根据实际情况进行修改和完善。
已知数据库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页面。
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");
}
}
}
```
阅读全文