smbms实现修改密码

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();
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值