使用Ajax优化密码登录
导入阿里巴巴的fastjson
com.alibaba
fastjson
1.2.78
设置session过期时间
30
后台代码修改
//验证旧密码,session中有用户的密码
public void pwdmodify(HttpServletRequest req, HttpServletResponse resp){
//从session里面拿ID;
Object o = req.getSession().getAttribute(Constants.USER_SESSION);
String oldpassword = req.getParameter("oldpassword");
//万能的map
HashMap 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","false");
}
}
resp.setContentType("application/json");
try {
PrintWriter writer = resp.getWriter();
//JSONAray阿里巴巴的工具类,转换格式
writer.write(JSONArray.toJSONString(resultMap));
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}