mysql和jsp Servlet的简单登录
时间: 2023-11-19 13:05:07 浏览: 49
实现MySQL和JSP/Servlet的简单登录,具体步骤如下:
1.创建数据库和表格
首先需要在MySQL中创建一个数据库和一个用户表格。可以使用以下命令:
```
CREATE DATABASE user_db;
USE user_db;
CREATE TABLE users (
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL,
PRIMARY KEY (id)
);
```
2.编写JSP页面
在JSP页面中,需要编写一个表单来收集用户输入的用户名和密码。输入框的name属性应该与数据库表格中的列名相同。
例如,以下是一个简单的登录表单:
```
<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>
```
3.编写Servlet代码
在Servlet中,需要编写一个POST请求处理程序来接收表单提交的数据,并将其与数据库中的数据进行比较。
以下是一个简单的Servlet代码示例:
```
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;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// Connect to the database
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/user_db", "root", "password");
// Check the user's credentials
stmt = conn.prepareStatement("SELECT * FROM users WHERE username=? AND password=?");
stmt.setString(1, username);
stmt.setString(2, password);
rs = stmt.executeQuery();
if (rs.next()) {
// If the user's credentials are valid, redirect to the home page
response.sendRedirect("home.jsp");
} else {
// If the user's credentials are not valid, show an error message
request.setAttribute("errorMessage", "Invalid username or password");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
} catch (SQLException | ClassNotFoundException e) {
throw new ServletException(e);
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
throw new ServletException(e);
}
}
}
}
```
在此示例中,我们首先获取从表单传递的用户名和密码参数。然后,我们连接到数据库,使用PreparedStatement对象执行SELECT查询,并将查询结果与表单数据进行比较。如果用户的凭据有效,则使用sendRedirect()方法将用户重定向到主页。否则,我们将显示一个错误消息并返回到登录页面。
请注意,此示例中的数据库连接字符串、用户名和密码是硬编码的。在实际应用程序中,应该将这些值存储在配置文件中,并使用资源文件或环境变量来读取它们。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)