javaweb-34:Ajax验证旧密码实现

优化密码修改,使用ajax

1.阿里巴巴的fastjson

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.61</version>
</dependency>

2.后台代码修改

@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);
    } else if (method.equals("pwdmodify") && method != null) {
        this.pwdModify(req, resp);
    }
}

//验证旧密码,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<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();//Session中用户的密码
        if (oldpassword.equals(userPassword)) {
            resultMap.put("result", "true");
        } else {
            resultMap.put("result", "false");
        }
    }

    try {
        resp.setContentType("application/json");
        PrintWriter writer = resp.getWriter();
        //JSONArray 阿里巴巴的JSON工具类,转换格式
        /*
            resultMap = ["result","sessionerror","result","error"];
            json格式={key:value}
             */
        writer.write(JSONArray.toJSONString(resultMap));
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

web.xml配置session过期时间

<!--默认session过期时间:真实业务需求-->
<session-config>
    <session-timeout>30</session-timeout>
</session-config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值