编写一段忘记密码修改密码的javaweb代码
时间: 2023-07-08 13:53:18 浏览: 92
用java做的修改用户密码
5星 · 资源好评率100%
以下是一个简单的忘记密码修改密码的 JavaWeb 代码示例:
首先,创建一个名为 "ForgotPasswordServlet" 的 Servlet:
```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 ForgotPasswordServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
String newPassword = request.getParameter("newPassword");
// 验证邮箱是否存在于数据库中
if (UserDAO.userExists(email)) {
// 更新用户密码
UserDAO.updatePassword(email, newPassword);
// 重定向到登录页面
response.sendRedirect("login.jsp");
} else {
// 邮箱不存在,重定向回忘记密码页面
HttpSession session = request.getSession();
session.setAttribute("errorMessage", "该邮箱尚未注册");
response.sendRedirect("forgot_password.jsp");
}
}
}
```
然后,创建一个名为 "forgot_password.jsp" 的 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>
<%
String errorMessage = (String) session.getAttribute("errorMessage");
if (errorMessage != null) {
out.println("<p style='color:red'>" + errorMessage + "</p>");
session.removeAttribute("errorMessage");
}
%>
<form action="ForgotPasswordServlet" method="POST">
<label for="email">邮箱:</label>
<input type="email" name="email" required><br>
<label for="newPassword">新密码:</label>
<input type="password" name="newPassword" required><br>
<input type="submit" value="确定">
</form>
</body>
</html>
```
最后,创建一个名为 "UserDAO" 的 DAO 类,用于与数据库交互:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class UserDAO {
private static final String URL = "jdbc:mysql://localhost:3306/mydb?useSSL=false";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public static boolean userExists(String email) {
boolean exists = false;
try {
// 连接数据库
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// 查询用户是否存在
PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) FROM users WHERE email = ?");
statement.setString(1, email);
exists = (statement.executeQuery().getInt(1) > 0);
// 关闭连接
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
return exists;
}
public static void updatePassword(String email, String newPassword) {
try {
// 连接数据库
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
// 更新用户密码
PreparedStatement statement = connection.prepareStatement("UPDATE users SET password = ? WHERE email = ?");
statement.setString(1, newPassword);
statement.setString(2, email);
statement.executeUpdate();
// 关闭连接
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
以上是一个简单的忘记密码修改密码的 JavaWeb 代码示例,仅供参考。实际生产环境下,需要对代码进行优化和安全性方面的考虑。
阅读全文