用JSP写一个用户管理系统
时间: 2023-11-11 17:02:17 浏览: 103
基于jsp的用户管理系统
好的,我可以为您提供一个简单的用户管理系统的JSP示例代码,包括用户登录、注册、信息展示和修改功能。
1. 用户登录页面(login.jsp)
```jsp
<%@ 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="loginCheck.jsp" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
<input type="submit" value="登录">
</form>
</body>
</html>
```
2. 登录验证页面(loginCheck.jsp)
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb","root","123456");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from user where username='"+username+"' and password='"+password+"'");
if(rs.next()){
session.setAttribute("username", username);
response.sendRedirect("userInfo.jsp");
} else {
out.print("<script>alert('用户名或密码错误,请重新输入!');window.location.href='login.jsp';</script>");
}
rs.close();
stmt.close();
conn.close();
%>
```
3. 用户注册页面(register.jsp)
```jsp
<%@ 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="registerCheck.jsp" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="password"><br>
确认密码:<input type="password" name="confirm_password"><br>
<input type="submit" value="注册">
</form>
</body>
</html>
```
4. 注册验证页面(registerCheck.jsp)
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
String confirm_password = request.getParameter("confirm_password");
if(!password.equals(confirm_password)){
out.print("<script>alert('两次密码输入不一致,请重新输入!');window.location.href='register.jsp';</script>");
} else {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb","root","123456");
PreparedStatement pstmt = conn.prepareStatement("insert into user(username,password) values(?,?)");
pstmt.setString(1, username);
pstmt.setString(2, password);
pstmt.executeUpdate();
pstmt.close();
conn.close();
out.print("<script>alert('注册成功,请登录!');window.location.href='login.jsp';</script>");
}
%>
```
5. 用户信息页面(userInfo.jsp)
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%
String username = (String)session.getAttribute("username");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb","root","123456");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from user where username='"+username+"'");
if(rs.next()){
String password = rs.getString("password");
%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户信息</title>
</head>
<body>
<h1>用户信息</h1>
<form action="updateInfo.jsp" method="post">
用户名:<input type="text" name="username" value="<%=username%>" readonly><br>
密码:<input type="password" name="password" value="<%=password%>"><br>
<input type="submit" value="修改">
</form>
</body>
</html>
<%
} else {
out.print("<script>alert('用户不存在,请重新登录!');window.location.href='login.jsp';</script>");
}
rs.close();
stmt.close();
conn.close();
%>
```
6. 修改用户信息页面(updateInfo.jsp)
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb","root","123456");
PreparedStatement pstmt = conn.prepareStatement("update user set password=? where username=?");
pstmt.setString(1, password);
pstmt.setString(2, username);
pstmt.executeUpdate();
pstmt.close();
conn.close();
out.print("<script>alert('修改成功!');window.location.href='userInfo.jsp';</script>");
%>
```
以上是一个简单的用户管理系统的JSP示例代码,仅供参考。如果您需要更完整和复杂的用户管理系统,建议您参考一些成熟的开源项目或者自己进行详细的设计和开发。
阅读全文