html+sevrlet+mysql+dao实现注册登录
时间: 2023-09-14 09:05:08 浏览: 82
首先,需要在数据库中创建一个用户表,包含id、username、password等字段。
然后,可以使用以下步骤实现注册登录功能:
1. 注册功能
在注册页面中,用户填写用户名和密码,点击提交按钮后,将数据通过POST方式传递给servlet。在servlet中,根据提交的数据创建一个新用户并插入数据库中。
JSP页面:
```html
<form action="register" method="post">
<input type="text" name="username" placeholder="用户名"><br>
<input type="password" name="password" placeholder="密码"><br>
<button type="submit">注册</button>
</form>
```
Servlet代码:
```java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = new User(username, password);
UserDao userDao = new UserDao();
boolean result = userDao.addUser(user);
if (result) {
response.sendRedirect("login.jsp");
} else {
response.sendRedirect("register.jsp");
}
}
```
UserDao代码:
```java
public boolean addUser(User user) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DBUtil.getConnection();
String sql = "INSERT INTO user (username, password) VALUES (?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
int result = ps.executeUpdate();
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, null);
}
return false;
}
```
2. 登录功能
在登录页面中,用户填写用户名和密码,点击提交按钮后,将数据通过POST方式传递给servlet。在servlet中,根据提交的数据查询数据库中是否存在该用户,如果存在且密码正确,则跳转到主页;否则返回登录页面。
JSP页面:
```html
<form action="login" method="post">
<input type="text" name="username" placeholder="用户名"><br>
<input type="password" name="password" placeholder="密码"><br>
<button type="submit">登录</button>
</form>
```
Servlet代码:
```java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
UserDao userDao = new UserDao();
User user = userDao.getUserByUsername(username);
if (user != null && user.getPassword().equals(password)) {
HttpSession session = request.getSession();
session.setAttribute("user", user);
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("login.jsp");
}
}
```
UserDao代码:
```java
public User getUserByUsername(String username) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM user WHERE username=?";
ps = conn.prepareStatement(sql);
ps.setString(1, username);
rs = ps.executeQuery();
if (rs.next()) {
int id = rs.getInt("id");
String password = rs.getString("password");
return new User(id, username, password);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, ps, rs);
}
return null;
}
```
这里使用了一个DBUtil工具类来获取数据库连接和关闭资源,具体实现可以参考以下代码:
```java
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=utf8";
private static final String USERNAME = "root";
private static final String PASSWORD = "root";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
以上就是使用html+sevrlet+mysql+dao实现注册登录的步骤,希望对你有所帮助。
阅读全文