实现用户信息更新和重置密码功能
如果用户信息很多的时候,感觉这项实现起来比较麻烦的。因为要考虑用户哪些信息需要更新哪些不需要更新,就必须先更原来的信息比对。呵呵,当然如果考虑到多个数据库表间的操作的话,这些根本不值一提。
首先介绍用户信息更新功能:
用户信息更新JSP页面,userupdate.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>用户信息更新界面
用户信息更新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;@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.setContentType("text/html;charset=UTF-8");int userId = Integer.parseInt(req.getParameter("userId"));//向session中保存userIdreq.getSession().setAttribute("userId", userId);UserDao ud = new UserDao();User userInfo = ud.selectOneUserInfo(userId);ArrayListlist = 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);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {resp.setContentType("text/html;charset=UTF-8");//从session中获取userIdHttpSession 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();//获取连接对象ConnectionString 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"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>重置用户密码界面
用户密码重置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;@Overrideprotected 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);}@Overrideprotected 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);//跳转到用户管理界面ArrayListlist = 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();//获取连接对象ConnectionString 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个月了,又做了三个小项目,会陆续贴上博客的。呵呵,谢谢观看!