写一个基于jsp+servlet+javabean实现登陆注册的系统的简单代码
时间: 2024-05-03 15:16:20 浏览: 103
好的,下面是一个基于 JSP、Servlet 和 JavaBean 实现的简单登陆注册系统的代码示例:
1. 登录页面 login.jsp
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录</title>
</head>
<body>
<h1>用户登录</h1>
<form action="LoginServlet" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
<p><a href="register.jsp">没有账号?点击注册</a></p>
</body>
</html>
```
2. 注册页面 register.jsp
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<h1>用户注册</h1>
<form action="RegisterServlet" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
重复密码:<input type="password" name="password2"><br>
<input type="submit" value="注册">
</form>
<p><a href="login.jsp">已有账号?点击登录</a></p>
</body>
</html>
```
3. LoginServlet.java
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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");
UserBean user = new UserBean(username, password);
boolean valid = user.validate();
if (valid) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("login.jsp");
}
}
}
```
4. RegisterServlet.java
```java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RegisterServlet 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");
String password2 = request.getParameter("password2");
if (!password.equals(password2)) {
response.sendRedirect("register.jsp");
return;
}
UserBean user = new UserBean(username, password);
boolean success = user.register();
if (success) {
HttpSession session = request.getSession();
session.setAttribute("username", username);
response.sendRedirect("welcome.jsp");
} else {
response.sendRedirect("register.jsp");
}
}
}
```
5. UserBean.java
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserBean {
private String username;
private String password;
public UserBean(String username, String password) {
this.username = username;
this.password = password;
}
public boolean validate() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname?user=root&password=root&useSSL=false&serverTimezone=UTC");
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM users WHERE username = ? AND password = ?");
stmt.setString(1, username);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
boolean found = rs.next();
rs.close();
stmt.close();
conn.close();
return found;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean register() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname?user=root&password=root&useSSL=false&serverTimezone=UTC");
PreparedStatement stmt = conn.prepareStatement("INSERT INTO users (username, password) VALUES (?, ?)");
stmt.setString(1, username);
stmt.setString(2, password);
int rows = stmt.executeUpdate();
stmt.close();
conn.close();
return rows == 1;
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
return false;
}
}
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;
}
}
```
6. welcome.jsp
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>欢迎</title>
</head>
<body>
<h1>欢迎,<%= session.getAttribute("username") %>!</h1>
<p><a href="logout.jsp">退出登录</a></p>
</body>
</html>
```
7. logout.jsp
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>退出</title>
</head>
<body>
<%
session.invalidate();
response.sendRedirect("login.jsp");
%>
</body>
</html>
```
以上代码仅供参考,具体实现需要根据实际情况进行调整和完善。