JavaWeb实现登录 用户信息来自数据库
时间: 2023-10-05 12:06:26 浏览: 79
如果要从数据库中获取用户信息来实现登录功能,可以参考以下步骤:
1. 创建一个数据库表,用于存储用户信息。表结构至少应包含用户名和密码两个字段。
2. 创建一个JavaBean,用于封装用户信息。该JavaBean应包含与数据库表中字段对应的属性以及get/set方法。
```
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
```
3. 创建一个DAO(Data Access Object)类,用于封装对数据库的访问操作。该类应提供至少以下方法:
- `public User findUserByUsername(String username)`:根据用户名从数据库中查询用户信息并返回一个User对象。
- `public boolean addUser(User user)`:将一个User对象插入到数据库中。
```
public class UserDao {
public User findUserByUsername(String username) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
User user = null;
try {
conn = JDBCUtils.getConnection();
String sql = "SELECT * FROM user WHERE username=?";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
rs = ps.executeQuery();
if (rs.next()) {
user = new User();
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(rs, ps, conn);
}
return user;
}
public boolean addUser(User user) {
Connection conn = null;
PreparedStatement ps = null;
int result = 0;
try {
conn = JDBCUtils.getConnection();
String sql = "INSERT INTO user (username, password) VALUES (?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
result = ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(ps, conn);
}
return result > 0;
}
}
```
4. 修改登录Servlet(loginServlet),从数据库中查询用户信息并进行验证。
```
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
private UserDao userDao = new UserDao();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = userDao.findUserByUsername(username);
if (user != null && user.getPassword().equals(password)) {
// 登录成功
request.getSession().setAttribute("username", username);
response.sendRedirect("index.jsp");
} else {
// 登录失败
request.setAttribute("msg", "用户名或密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
```
以上就是JavaWeb实现从数据库中获取用户信息来实现登录功能的简单示例。在登录Servlet中,首先根据用户名从数据库中查询用户信息,然后与输入的密码进行比对。如果验证通过,则将用户名保存到Session中,并跳转到首页显示登录用户的信息;如果验证失败,则返回登录页面并显示错误信息。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)