使用Ajax优化密码登录
导入阿里巴巴的fastjson
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>
设置session过期时间
<!--默认session过期时间-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
后台代码修改
//验证旧密码,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<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");
}
}
resp.setContentType("application/json");
try {
PrintWriter writer = resp.getWriter();
//JSONAray阿里巴巴的工具类,转换格式
/*
resultMap = ["result","sessionerror","result","error"]
Json格式 = {key:value}
*/
writer.write(JSONArray.toJSONString(resultMap));
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}