密码修改
-
导入前端页面
<li><a href="${pageContext.request.contextPath }/jsp/pwdmodify.jsp">密码修改</a></li>
-
写项目,建议从底层往上写
-
UserDao 接口
//修改当前用户密码 public int updatePwd(Connection connection, String userCode , String password) throws SQLException;
-
UserDao 接口实现类
//修改当前用户密码 public int updatePwd(Connection connection, String userCode, String password){ PreparedStatement statement = null; int update = 0; if (connection != null){ String sql = "update smbms_user set userPassword = ? where userCode = ?;"; Object[] params = {password, userCode}; try { update = BaseDao.update(connection, statement, sql, params); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { BaseDao.release(connection,statement,null); } } return update; } }
-
UserService层
//修改密码 boolean updatePwd(String userCode, String password);
-
UserService层实现类
public boolean updatePwd(String userCode, String password) { Connection connection = null; boolean flag = false; try { connection = BaseDao.getConnection(); if (userDao.updatePwd(connection, userCode, password) > 0){ flag = true; } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { BaseDao.release(connection,null,null); } return flag; }
-
注册servlet
<servlet> <servlet-name>UserServlet</servlet-name> <servlet-class>com.tr.servlet.user.UserServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>UserServlet</servlet-name> <url-pattern>/jsp/user.do</url-pattern> </servlet-mapping>
-
记得实现复用,必须提取出方法
@Override 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); } }
-
修改密码方法
public void updatePwd(HttpServletRequest req, HttpServletResponse resp){ String newPassword = req.getParameter("newpassword"); UserService userService = null; boolean flag = false; Object o = req.getSession().getAttribute(Constant.USER_SESSION); if (o != null && newPassword != null /*!StringUtils.isNullOrEmpty(newPassword)*/){ userService = new UserServiceImpl(); flag = userService.updatePwd(((User) o).getUserCode(), newPassword); if (flag){ req.setAttribute(Constant.MESSAGE, "修改密码成功,请重新登录"); //密码修改成功,移除当前Session req.getSession().removeAttribute(Constant.USER_SESSION); } else { req.setAttribute(Constant.MESSAGE, "修改密码失败"); } } else { req.setAttribute(Constant.MESSAGE, "新密码有误"); } try { resp.sendRedirect("pwdmodify.jsp"); } catch (IOException e) { e.printStackTrace(); } }
-
测试
优化密码修改使用Ajax;
-
阿里巴巴的fastjson
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version> </dependency>
-
后台密码修改
//验证旧密码 session中有用户的密码 public void pwdModify(HttpServletRequest req, HttpServletResponse resp){ Object o = req.getSession().getAttribute(Constant.USER_SESSION); String oldpassword = req.getParameter("oldpassword"); //万能的Map:结果集 Map<String, String> resultMap = new HashMap<String, String>(); if (o == null){//Session过期了或者Session失效了 resultMap.put("result","sessionerror"); } else if (StringUtils.isNullOrEmpty(oldpassword)) { //如果密码为空 resultMap.put("result","error"); } else { String userPassword = ((User) o).getUserPassword(); if (userPassword.equals(oldpassword)){ resultMap.put("result","true"); //如果前端传过来的密码和session中相同 //真正开发中还要考虑多端登录 多端修改密码 } else { resultMap.put("result","false");//如果不同 } } try { resp.setContentType("application/json"); PrintWriter writer = resp.getWriter(); writer.write(JSONArray.toJSONString(resultMap)); writer.flush(); } catch (IOException e) { e.printStackTrace(); } }
-
测试