1.分析
2.UserDao接口
//修改当前用户密码
public int updatePwd(Connection connection,int id,int password) throws SQLException;
3UserDao接口的实现类
@Override
//修改当前用户密码
public int updatePwd(Connection connection, int id, int password) throws SQLException {
PreparedStatement pstm = null;
int execute = 0;
if (connection != null) {
String sql = "UPDATE smbms_user SET userPassword=? WHERE id=?";
Object params[] = {password, id};
execute = BaseDao.execute(connection, pstm, sql, params);
BaseDao.closeResource(null, pstm, null);
}
return execute;
}
4.UserService层
//根据用户id修改密码
public boolean updatePwd(int id, int password);
UserServiceImpl 实现类
//根据用户id修改密码
@Override
public boolean updatePwd(int id, int password) {
Connection connection = null;
boolean flag = false;
//修改密码
try {
connection = BaseDao.getConnection();
if (userDao.updatePwd(connection, id, password) > 0) {
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return flag;
}
5.UserSvervlet
@Override
//实现servlet复用
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getParameter("method");
if (method.equals("savepwd") && method != null) {
this.updatePwd(req, resp);
}else if (method.equals("pwdmodify")&& method != null){
this.updatePwd(req,resp);
}
}
修改密码的方法
public void updatePwd(HttpServletRequest req, HttpServletResponse resp) {
//从Session里面拿ID
Object o = req.getSession().getAttribute(Constants.USER_SESSION);
String newpassword = req.getParameter("newpassword");
System.out.println("UserServlet" + newpassword);
boolean flag = false;
System.out.println(o != null);
System.out.println(StringUtils.isNullOrEmpty(newpassword));
if (o != null && newpassword != null) {
UserService userService = new UserServiceImpl();
//第一次newpassword
flag = userService.updatePwd(((User) o).getId(), newpassword);
if (flag) {
req.setAttribute("message", "修改密码成功,请退出,使用新密码重新登录");
//密码修改成功,移除当前Session
req.getSession().removeAttribute(Constants.USER_SESSION);
} else {
req.setAttribute("message", "修改密码失败。");
}
} else {
req.setAttribute("message", "新密码设置有问题。");
}
try {
req.getRequestDispatcher("pwdmodify.jsp").forward(req, resp);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
旧密码验证的方法
//验证旧密码,session中有用户的密码
public void pwdmodify(HttpServletRequest req, HttpServletResponse resp) {
//从Session里面拿ID
Object o = req.getSession().getAttribute(Constants.USER_SESSION);
String oldpassword = req.getParameter("oldpassword");
//万能的Map:结果集
Map<String, String> resultMap = new HashMap<>();
if (o == null) {//Session失效了,或者Session过期了
resultMap.put("result", "sessionerror");
} else if (StringUtils.isNullOrEmpty("oldpassword")) {
resultMap.put("result", "error");
} else {
String userPassword = ((User) o).getUserPassword();//Session中用户的密码
if (oldpassword.equals(userPassword)) {
resultMap.put("result", "true");
} else {
resultMap.put("result", "true");
}
}
resp.setContentType("application/json");
try {
PrintWriter respWriter = resp.getWriter();
//JSONArray 阿里巴巴的JSON工具类,转换格式
respWriter.write(JSONArray.toJSONString(resultMap));
respWriter.flush();
respWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}