天天看點

Servlet+JSP+MySQL實作使用者管理子產品之七、實作使用者資訊更新和重置密碼

     資料庫表設計及建立

    實作使用者注冊

    實作使用者登入

    實作使用者清單顯示

    實作使用者删除與恢複

    實作使用者資訊顯示

    實作使用者資訊更新與重置密碼

實作使用者資訊更新和重置密碼功能

    如果使用者資訊很多的時候,感覺這項實作起來比較麻煩的。因為要考慮使用者哪些資訊需要更新哪些不需要更新,就必須先更原來的資訊比對。呵呵,當然如果考慮到多個資料庫表間的操作的話,這些根本不值一提。

首先介紹使用者資訊更新功能:

使用者資訊更新JSP頁面,userupdate.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>使用者資訊更新界面</title>
</head>
<body>
<form name="form1" action ="UserUpdateServlet" method = "post">
<table border ="1" align = "center" style="border-collapse:collapse;">
<c:forEach items="${list}" var="st">
<tr>
<td colspan="2">資訊修改</td>
</tr>
<tr>
<td>使用者ID</td>
<td>${st.userId }</td>
</tr>
<tr>
<td>使用者名</td>
<td><input type="text" name="userName" value="${st.userName }"></td>
</tr>
<tr>
<td>使用者賬号</td>
<td><input type="text" name="userAccount" value="${st.userAccount }"></td>
</tr>
</c:forEach>
<tr>
<td colspan="2">
<input type="submit" value="修改">
<input type="button" value="重置密碼" 
onclick="window.location.href='/UserManage/RecoverPWDServlet'">
</td>
</tr>
</table>
</form>
</body>
</html>      

使用者資訊更新Servlet,

package servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import dao.UserDao;
import entity.User;
/**
 * 使用者資訊更新Servlet
 */
@WebServlet("/UserUpdateServlet")
public class UserUpdateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
int userId = Integer.parseInt(req.getParameter("userId"));
//向session中儲存userId
req.getSession().setAttribute("userId", userId);
UserDao ud = new UserDao();
User userInfo = ud.selectOneUserInfo(userId);
ArrayList<User> list = new ArrayList<User>();
list.add(userInfo);
req.setAttribute("list", list);
String msg = "使用者資訊已更新!";
req.setAttribute(msg, msg);
String path = resp.encodeURL("userupdate.jsp");
req.getRequestDispatcher(path).forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
//從session中擷取userId
HttpSession session = req.getSession();
int userId = (int) session.getAttribute("userId");
//擷取使用者表單資料
String userName = req.getParameter("userName");
String userAccount = req.getParameter("userAccount");
//執行個體化使用者對象
User user = new User();
user.setUserId(userId);
user.setUserName(userName);
user.setUserAccount(userAccount);
//更新使用者資訊
UserDao ud = new UserDao();
ud.updateUser(user);
//跳轉到使用者管理界面
ArrayList<User> list = ud.selectNotDeleteList();
req.setAttribute("list", list);
String path = resp.encodeURL("userlist.jsp");
req.getRequestDispatcher(path).forward(req, resp);
}
}      

資料庫操作--使用者資訊更新方法

/**
 * 使用者資訊更新
 * @param user User對象
 */
public void updateUser(User user) {
Connection conn = null;
PreparedStatement ps = null;
//将使用者資料寫入資料庫
try {
conn = DBUtils.getConnection();//擷取連接配接對象Connection
String sql = "UPDATE users SET user_name=?,user_account=?"
+ " WHERE user_id=?";
ps = conn.prepareStatement(sql);//格式化sql語句
//為?指派
ps.setString(1, user.getUserName());
ps.setString(2, user.getUserAccount());
ps.setInt(3, user.getUserId());
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
//釋放資源
DBUtils.closeAll(null, ps, conn);
}
}      

其次介紹使用者密碼重置功能:

使用者密碼重置JSP頁面,recoverpwd.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>重置使用者密碼界面</title>
<script language="javascript" type="text/javascript"> 
function check1() {
if (document.form1.userAccount.value==""){
alert("請輸入使用者賬号!");
return false;
}
if (document.form1.userPassword.value==""){
alert("請輸入使用者密碼");
return false;
}
if (document.form1.userPassword1.value==""){
alert("請輸入确認密碼!");
return false;
}
if (document.form1.userPassword.value!=document.form1.userPassword1.value){
alert("對不起!确認密碼不等于登入密碼");
return false;
}
return true; 
}
</script> 
</head>
<body>
<form name="form1" action="/UserManage/RecoverPWDServlet" method="post">
<table align="center">
<tr>
<td>使用者賬号</td>
<td><input type="text" name="userAccount" ></td>
</tr>
<tr>
<td>使用者密碼</td>
<td><input type="password" name="userPassword" ></td>
</tr>
<tr>
<td>确認密碼</td>
<td><input type="password" name="userPassword1" ></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="确認更改" onclick="return check1()">
</td>
</tr>
</table>
</form>
</body>
</html>      

使用者密碼重置Servlet,RecoverPWDServlet.java

package servlet;
import java.io.IOException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import dao.UserDao;
import entity.User;
/**
 * 重置使用者密碼Servlet
 */
@WebServlet("/RecoverPWDServlet")
public class RecoverPWDServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
String path1 = resp.encodeURL("recoverpwd.jsp");
req.getRequestDispatcher(path1).forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
HttpSession session = req.getSession();
User user = null;
UserDao ud = new UserDao();
String userAccount = (String) req.getAttribute("userAccount");
String pwd = (String) req.getAttribute("userPassword");
ud.updateUserPWD(pwd,userAccount);
user = ud.selectOneUserInfo(userAccount);
//更新session對象中的user中屬性。
session.setAttribute("user", user);
//跳轉到使用者管理界面
ArrayList<User> list = ud.selectNotDeleteList();
req.setAttribute("list", list);
String msg = "使用者密碼已重置!";
req.setAttribute(msg, msg);
String path1 = resp.encodeURL("userlist.jsp");
req.getRequestDispatcher(path1).forward(req, resp);
}
}      
/**重置使用者密碼
 * @param pwd 使用者密碼
 * @param userAccount 使用者賬号
 */
public void updateUserPWD(String pwd, String userAccount) {
Connection conn = null;
PreparedStatement ps = null;
//将使用者資料寫入資料庫
try {
conn = DBUtils.getConnection();//擷取連接配接對象Connection
String sql = "UPDATE users SET user_password=?"
+ " WHERE user_account=?";
ps = conn.prepareStatement(sql);//格式化sql語句
//為?指派
ps.setString(1, pwd);
ps.setString(2, userAccount);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
//釋放資源
DBUtils.closeAll(null, ps, conn);
}
}