pwdmodify.js 代码:
// 失去焦点
oldpassword.on("blur", function () {
$.ajax({
// 请求方式 GET
type: "GET",
// 提交的请求和参数
// 此处相当于发起 path + "/user.do?method=pwdmodify&oldpassword=oldpassword.val()" 请求
url: path + "/user.do",
data: {method: "pwdmodify", oldpassword: oldpassword.val()}, //ajax传递的参数
// 数据格式 json 主流开发都用json进行前后端交互
dataType: "json",
success: function (data) {
if (data.result == "true") {//旧密码正确
validateTip(oldpassword.next(), {"color": "green"}, imgYes, true);
} else if (data.result == "false") {//旧密码输入不正确
validateTip(oldpassword.next(), {"color": "red"}, imgNo + " 原密码输入不正确", false);
} else if (data.result == "sessionerror") {//当前用户session过期,请重新登录
validateTip(oldpassword.next(), {"color": "red"}, imgNo + " 当前用户session过期,请重新登录", false);
} else if (data.result == "error") {//旧密码输入为空
validateTip(oldpassword.next(), {"color": "red"}, imgNo + " 请输入旧密码", false);
}
},
error: function (data) {
//请求出错
validateTip(oldpassword.next(), {"color": "red"}, imgNo + " 请求错误", false);
}
});
pwdmodify.jsp代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@include file="/jsp/common/head.jsp"%>
<div class="right">
<div class="location">
<strong>你现在所在的位置是:</strong>
<span>密码修改页面</span>
</div>
<div class="providerAdd">
<form id="userForm" name="userForm" method="post" action="${pageContext.request.contextPath }/user.do">
<input type="hidden" name="method" value="savepwd">
<!--div的class 为error是验证错误,ok是验证成功-->
<div class="info">${message}</div>
<div class="">
<label for="oldPassword">旧密码:</label>
<input type="password" name="oldpassword" id="oldpassword" value="">
<font color="red"></font>
</div>
<div>
<label for="newPassword">新密码:</label>
<input type="password" name="newpassword" id="newpassword" value="">
<font color="red"></font>
</div>
<div>
<label for="rnewpassword">确认新密码:</label>
<input type="password" name="rnewpassword" id="rnewpassword" value="">
<font color="red"></font>
</div>
<div class="providerAddBtn">
<!--<a href="#">保存</a>-->
<input type="submit" name="save" id="save" value="保存" class="input-button">
</div>
</form>
</div>
</div>
</section>
<%@include file="/jsp/common/foot.jsp" %>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/pwdmodify.js"></script>
UserServlet 类:
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// Servlet复用,UserServlet不只修改密码,还要增删改查
// 怎么在一个doGet中执行多个方法?
String method = req.getParameter("method");
if ("savepwd".equals(method)) {
this.updatePwd(req, resp);
} else if ("pwdmodify".equals(method)) {
this.pwdModify(req, resp);
}
}
//验证旧密码,Session中有用户的密码
public void pwdModify(HttpServletRequest req,HttpServletResponse resp) throws IOException {
// 从Session中获取用户
Object object = req.getSession().getAttribute(Constants.USER_SESSION);
// 获取前端传过来的oldpassword参数
String oldpassword = req.getParameter("oldpassword");
// 使用 Map 保存result的值
Map<String, String> resultMap = new HashMap<String, String>();
if(object == null){ // 取不到用户,Session失效了(设置Session失效时间)
// 和前端约定好的返回值
resultMap.put("result","sessionerror");
}else if(StringUtils.isNullOrEmpty(oldpassword)){ //旧密码为空 前端后端都应该验证
resultMap.put("result","error");
}else{
// 获取旧密码
String userPassword = ((User) object).getUserPassword();
if( userPassword.equals(oldpassword)){ //旧密码输入正确
resultMap.put("result","true");
}else{ //旧密码输入错误
resultMap.put("result","false");
}
}
// 设置响应数据的格式
resp.setContentType("application/json");
PrintWriter writer = resp.getWriter();
// JSONArray 阿里的JSON工具类,此处将Map转换为JSON
writer.write(JSONArray.toJSONString(resultMap));
writer.flush();
writer.close();
}
}
最后是web.xml映射:
<servlet>
<servlet-name>UserServlet</servlet-name>
<servlet-class>com.kuang.servlet.user.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserServlet</servlet-name>
<url-pattern>/user.do</url-pattern>
</servlet-mapping>
这是错误的截图,它去找了我的user/check吗?
没搞懂啊~~搞了几小时没找到错误,帮看看解决以下 小厮这厢有礼了,谢谢了!
是路径没找到,可以用远程吗
初步判断是路径多了一层user,多打印几下就能发现。