jsp+servlet+javabean+jdbc实现数据库的登录注册验证代码
时间: 2023-12-18 15:03:36 浏览: 227
登陆实例(JSP+Servlet+JavaBean)
5星 · 资源好评率100%
好的,以下是一个简单的代码示例(仅供参考):
1. 创建数据库表
```sql
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
2. 编写 JavaBean
User.java
```java
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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 类
UserDao.java
```java
public class UserDao {
// 数据库连接信息
private String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8";
private String username = "root";
private String password = "root";
// 注册用户
public boolean register(User user) {
Connection conn = null;
PreparedStatement stmt = null;
try {
// 获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// 编写 SQL 语句
String sql = "INSERT INTO user(username, password) VALUES(?, ?)";
// 创建 PreparedStatement 对象
stmt = conn.prepareStatement(sql);
// 设置参数
stmt.setString(1, user.getUsername());
stmt.setString(2, user.getPassword());
// 执行 SQL
int result = stmt.executeUpdate();
// 处理结果
return result > 0;
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
// 关闭资源
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 验证用户信息
public boolean login(User user) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// 获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// 编写 SQL 语句
String sql = "SELECT * FROM user WHERE username=? AND password=?";
// 创建 PreparedStatement 对象
stmt = conn.prepareStatement(sql);
// 设置参数
stmt.setString(1, user.getUsername());
stmt.setString(2, user.getPassword());
// 执行 SQL
rs = stmt.executeQuery();
// 处理结果
return rs.next();
} catch (SQLException e) {
e.printStackTrace();
return false;
} finally {
// 关闭资源
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
```
4. 编写 Servlet
UserServlet.java
```java
public class UserServlet extends HttpServlet {
private UserDao userDao = new UserDao();
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置请求编码和响应编码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
// 获取请求参数
String action = request.getParameter("action");
String username = request.getParameter("username");
String password = request.getParameter("password");
// 根据请求参数执行相应的操作
if ("register".equals(action)) {
// 注册用户
User user = new User();
user.setUsername(username);
user.setPassword(password);
if (userDao.register(user)) {
response.getWriter().println("注册成功");
} else {
response.getWriter().println("注册失败");
}
} else if ("login".equals(action)) {
// 登录验证
User user = new User();
user.setUsername(username);
user.setPassword(password);
if (userDao.login(user)) {
response.getWriter().println("登录成功");
} else {
response.getWriter().println("登录失败");
}
} else {
response.getWriter().println("无效的请求");
}
}
}
```
5. 创建 JSP 页面
register.jsp
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户注册</title>
</head>
<body>
<h2>用户注册</h2>
<form action="UserServlet" method="post">
<input type="hidden" name="action" value="register">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
```
login.jsp
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<h2>用户登录</h2>
<form action="UserServlet" method="post">
<input type="hidden" name="action" value="login">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
6. 部署项目
将 UserServlet、UserDao、User.java、register.jsp 和 login.jsp 等文件放到 Web 项目的相应目录下,然后启动服务器,即可通过访问 register.jsp 和 login.jsp 页面进行注册和登录。
阅读全文