请给我一个包含了用户登录和注册的jsp代码
时间: 2024-02-13 13:04:30 浏览: 26
好的,以下是一个包含了用户登录和注册的jsp代码,你可以根据自己的需要进行修改:
1. 注册页面(register.jsp)
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>注册页面</title>
</head>
<body>
<h1>用户注册</h1>
<form action="register.do" method="post">
<label for="username">用户名:</label>
<input type="text" name="username" id="username" required><br>
<label for="password">密码:</label>
<input type="password" name="password" id="password" required><br>
<label for="confirm_password">确认密码:</label>
<input type="password" name="confirm_password" id="confirm_password" required><br>
<input type="submit" value="注册">
</form>
<br>
<p>已有账号?<a href="login.jsp">点击登录</a></p>
</body>
</html>
```
2. 登录页面(login.jsp)
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<h1>用户登录</h1>
<form action="login.do" method="post">
<label for="username">用户名:</label>
<input type="text" name="username" id="username" required><br>
<label for="password">密码:</label>
<input type="password" name="password" id="password" required><br>
<input type="submit" value="登录">
</form>
<br>
<p>没有账号?<a href="register.jsp">点击注册</a></p>
</body>
</html>
```
3. 注册处理(register.do)
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>注册处理</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String confirm_password = request.getParameter("confirm_password");
if(!password.equals(confirm_password)) {
out.print("两次输入的密码不一致,请重新输入!");
return;
}
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123456");
String sql = "insert into user(username, password) values(?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.executeUpdate();
out.print("注册成功!");
} catch (Exception e) {
e.printStackTrace();
out.print("注册失败!");
} finally {
if(pstmt != null) {
pstmt.close();
}
if(conn != null) {
conn.close();
}
}
%>
</body>
</html>
```
4. 登录处理(login.do)
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<title>登录处理</title>
</head>
<body>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123456");
String sql = "select * from user where username=? and password=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, username);
pstmt.setString(2, password);
rs = pstmt.executeQuery();
if(rs.next()) {
out.print("登录成功!");
} else {
out.print("用户名或密码错误,请重新登录!");
}
} catch (Exception e) {
e.printStackTrace();
out.print("登录失败!");
} finally {
if(rs != null) {
rs.close();
}
if(pstmt != null) {
pstmt.close();
}
if(conn != null) {
conn.close();
}
}
%>
</body>
</html>
```
以上代码仅供参考,具体实现还需要根据实际情况进行修改和完善。另外,为了安全起见,建议在存储密码时使用加密算法来保护用户信息。
相关推荐
![](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)