实现用户信息更新和重置密码功能
如果用户信息很多的时候,感觉这项实现起来比较麻烦的。因为要考虑用户哪些信息需要更新哪些不需要更新,就必须先更原来的信息比对。呵呵,当然如果考虑到多个数据库表间的操作的话,这些根本不值一提。
首先介绍用户信息更新功能:
用户信息更新JSP页面,userupdate.jsp
pageEncoding="UTF-8"%>
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
用户信息更新界面信息修改 | |
用户ID | ${st.userId } |
用户名 | |
用户账号 | |
οnclick="window.location.href='/UserManage/RecoverPWDServlet'"> |
用户信息更新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 list = new ArrayList();
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 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
pageEncoding="UTF-8"%>
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
重置用户密码界面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;
}
用户账号 | |
用户密码 | |
确认密码 | |
用户密码重置Servlet,RecoverPWDServlet.javapackage 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 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);
}
}
到此,小小的用户管理模块就完成了。当初花不少时间自学和编写的,这是培训开课前老师安排的作业。当时javaSE差不多自学完,因为这个作业,就跳过了javaSE后面的学习。开始学习jdbc、MySQL和SQL语言、Servlet这些看视频教程,其余的参考网络资源。因为这个项目,积累了一些代码量和代码规范,以及养成了尽可能提高代码质量而修改代码的习惯。至今开课差不多2个月了,又做了三个小项目,会陆续贴上博客的。呵呵,谢谢观看!