1.自建数据库(数据库产品自选SQL server MySQL Oracle等任选),新建一个user表,至少包含用户名和密码两个字段 2.新建一个登录页面 3.编写处理登录页面的servlet,在servlet中利用JDBC连接数据库,并进行登录的验证操作。 4.根据servlet中处理的结果,跳转到登录成功或者失败的JSP页面。
时间: 2024-03-20 13:40:58 浏览: 83
好的,我可以为您提供一个实现登录验证的示例代码。首先,您需要在您选择的数据库中创建一个名为"user"的表,该表包含id、username和password三个字段。下面是示例代码:
1. 创建user表的SQL语句(以MySQL为例):
```sql
CREATE TABLE user (
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
```
2. 创建登录页面的HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
</head>
<body>
<h2>Login Page</h2>
<form action="login" method="post">
<p>
<label>Username:</label>
<input type="text" name="username" required>
</p>
<p>
<label>Password:</label>
<input type="password" name="password" required>
</p>
<p>
<input type="submit" value="Login">
</p>
</form>
</body>
</html>
```
3. 创建处理登录请求的servlet:
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
boolean isValidUser = validateUser(username, password);
if (isValidUser) {
request.getRequestDispatcher("success.jsp").forward(request, response);
} else {
request.getRequestDispatcher("failure.jsp").forward(request, response);
}
}
private boolean validateUser(String username, String password) {
try (Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD)) {
String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, username);
statement.setString(2, password);
try (ResultSet resultSet = statement.executeQuery()) {
return resultSet.next();
}
}
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
```
4. 创建登录成功和失败的JSP页面:
success.jsp
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Success</title>
</head>
<body>
<h2>Login Success</h2>
<p>Welcome <%= request.getParameter("username") %>!</p>
</body>
</html>
```
failure.jsp
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Failure</title>
</head>
<body>
<h2>Login Failure</h2>
<p>Invalid username or password.</p>
</body>
</html>
```
需要注意的是,示例代码中使用了try-with-resources语句来自动关闭JDBC连接和资源,以确保代码的健壮性和可靠性。
希望这个示例代码能够帮助您实现登录验证功能。
阅读全文