servlet和ajax的交互(jsp页面)

时间:2022-08-15 21:10:53

前端jsp页面

$.ajax({
type : “POST”, //提交方式
url : “password?action=change”,//路径
data : {
“oldPassword” : oldPassword,
“newPassword”:newPassword,
“rPassword”:rPassword
},//数据,这里使用的是Json格式进行传输
dataType:’json’,
success : function(data) {//返回数据根据结果进行相应的处理
if(data.msg==”修改成功”){
layer.alert(data.msg);
window.location.href=”login.jsp”;
}else{
layer.tips(data.msg, “#oldPassword”, {
tips: [2, ‘#F70909’],
time: 2000,
tipsMore: true
});
}
},
error:function (XMLHttpRequest, textStatus, errorThrown) {
layer.alert(XMLHttpRequest.status);
layer.alert(XMLHttpRequest.readyState);
layer.alert(textStatus);
}
});

servlet 后端代码

private Object passwordChange(HttpServletRequest request,
HttpServletResponse response)throws ServletException, IOException {
HttpSession session = request.getSession();
Object currentUserType = session.getAttribute(“currentUserType”); //获取参数
String oldPassword =
request.getParameter(“oldPassword”);//获取参数

    String newPassword = request.getParameter("newPassword");//获取参数
Connection con = null;
try {
con = dbUtil.getCon();

if("admin".equals((String)currentUserType)) {
Admin admin = (Admin)(session.getAttribute("currentUser"));
System.out.println("admin.getPassword="+admin.getPassword());
if(oldPassword.equals(admin.getPassword())) {
System.out.println("进入dao层");
userDao.adminUpdate(con, admin.getAdminId(), newPassword);
admin.setPassword(newPassword);
response.setContentType("application/json;charset=utf-8");//返回json类型的
String json="{\"msg\":\"修改成功\"}";
response.getWriter().write(json);
response.getWriter().flush();
response.getWriter().close();
} else {
response.setContentType("application/json;charset=utf-8");
String json="{\"msg\":\"原密码输入错误,请重新!\"}";
response.getWriter().write(json);//写到前端
response.getWriter().flush();
response.getWriter().close();

}
} else if("dormManager".equals((String)currentUserType)) {
DormManager manager = (DormManager)(session.getAttribute("currentUser"));
if(oldPassword.equals(manager.getPassword())) {
System.out.println("managerId="+manager.getDormManagerId());
userDao.managerUpdate(con, manager.getDormManagerId(), newPassword);
response.setContentType("application/json;charset=utf-8");
String json="{\"msg\":\"修改成功\"}";
response.getWriter().write(json);
response.getWriter().flush();
response.getWriter().close();
} else {
response.setContentType("application/json;charset=utf-8");
String json="{\"msg\":\"原密码输入错误,请重新!\"}";
response.getWriter().write(json);
response.getWriter().flush();
response.getWriter().close();
}
} else if("student".equals((String)currentUserType)) {
Student student = (Student)(session.getAttribute("currentUser"));
if(oldPassword.equals(student.getPassword())) {
userDao.studentUpdate(con, student.getStudentId(), newPassword);
student.setPassword(newPassword);
response.setContentType("application/json;charset=utf-8");
String json="{\"msg\":\"修改成功\"}";
response.getWriter().write(json);
response.getWriter().flush();
response.getWriter().close();
} else {
response.setContentType("application/json;charset=utf-8");
String json="{\"msg\":\"原密码输入错误,请重新!\"}";
response.getWriter().write(json);
response.getWriter().flush();
response.getWriter().close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
dbUtil.closeCon(con);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}