优化密码修改,使用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>