mysql+servlet+tomcat实现登陆注册代码
时间: 2023-08-12 21:08:58 浏览: 58
好的,以下是一份基本的MySQL + Servlet + Tomcat实现登录注册的代码示例,仅供参考:
1. 创建数据库和用户表:
```sql
CREATE DATABASE user_db;
USE user_db;
CREATE TABLE user (
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(50) DEFAULT NULL,
password VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (id)
);
```
2. 编写Servlet代码:
- 注册Servlet代码:
```java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class RegisterServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取表单数据
String username = request.getParameter("username");
String password = request.getParameter("password");
// 连接数据库
Connection con = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user_db", "root", "password");
// 判断用户名是否已存在
ps = con.prepareStatement("SELECT * FROM user WHERE username = ?");
ps.setString(1, username);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
response.sendRedirect("register.jsp?error=1");
return;
}
// 插入用户数据
ps = con.prepareStatement("INSERT INTO user (username, password) VALUES (?, ?)");
ps.setString(1, username);
ps.setString(2, password);
ps.executeUpdate();
// 注册成功,跳转到登录页面
response.sendRedirect("login.jsp?success=1");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
- 登录Servlet代码:
```java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取表单数据
String username = request.getParameter("username");
String password = request.getParameter("password");
// 连接数据库
Connection con = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/user_db", "root", "password");
// 验证用户名和密码
ps = con.prepareStatement("SELECT * FROM user WHERE username = ? AND password = ?");
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
// 登录成功,跳转到主页
response.sendRedirect("index.jsp");
} else {
response.sendRedirect("login.jsp?error=1");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
3. 在Web页面中添加注册和登录表单:
- 注册表单:
```html
<form method="post" action="RegisterServlet">
<div>
<label>用户名:</label>
<input type="text" name="username" required>
</div>
<div>
<label>密码:</label>
<input type="password" name="password" required>
</div>
<div>
<input type="submit" value="注册">
</div>
</form>
```
- 登录表单:
```html
<form method="post" action="LoginServlet">
<div>
<label>用户名:</label>
<input type="text" name="username" required>
</div>
<div>
<label>密码:</label>
<input type="password" name="password" required>
</div>
<div>
<input type="submit" value="登录">
</div>
</form>
```
4. 在Web页面中添加JavaScript代码,实现表单验证和提交表单的功能。
以上代码仅为示例代码,需要根据实际情况进行修改和优化。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)