ajax判断用户输入的密码是否正确

控制层的代码段

	@RequestMapping("CheckPass")//ajax请求后台查询密码是否正确
	@ResponseBody
	public void CheckPass(User user , HttpServletResponse response) throws IOException {

		User u = userService.getDetail(user.getLoginname1());

		PrintWriter out = response.getWriter();
		
		 
		if(u.getPasswords1().equals(user.getPasswords1())) {
			out.print(true);
		}else {
			out.print(false);
		}

	}

jsp页面的代码段

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
   <title>超市账单管理系统</title>
    <link rel="stylesheet" href="css/public.css"/>
    <link rel="stylesheet" href="css/style.css"/>

    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
 <body>
<!--头部-->
    <%@include file="head.jsp" %>
<!--时间-->
    <section class="publicTime">
        <span id="time">2015年1月1日 11:11  星期一</span>
        <a href="#">温馨提示:为了能正常浏览,请使用高版本浏览器!(IE10+)</a>
    </section>
<!--主体内容-->
    <section class="publicMian ">
        <div class="left">
            <h2 class="leftH2"><span class="span1"></span>功能列表 <span></span></h2>
            <%@include file="menum.jsp" %>
        </div>
        <div class="right">
            <div class="location">
                <strong>你现在所在的位置是:</strong>
                <span>密码修改页面</span>
            </div>
            <div class="providerAdd">
                <form action="UpdatePWdServlet" method="post" onsubmit="return checkAll();">
                    <!--div的class 为error是验证错误,ok是验证成功-->
                    <input type="hidden" name="loginname" value="${sessionScope.user.loginname1 }">
                    <div class="">
                        <label for="oldPassword">旧密码:</label>
                        <input type="password" name="passwords1" id="oldPassword" onblur="isExist(this)"/>
                        <span id="pid"></span>
                    </div>
                    <script type="text/javascript">
    	function createXmlHttpRequest(){//ajax
		// 兼容写法
			//如果是 IE7+,FF等高级浏览器
			if(window.XMLHttpRequest) {
						return  new XMLHttpRequest();
			}else
			//如果是IE 5,IE6低版本浏览器
			if( window.ActiveXObject) {
						return new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		var req;
		function isExist(obj){
			var pass=obj.value;//用户输入的原密码
			alert(pass);
			//创建xmlHttpRequest对象
			req=createXmlHttpRequest();
			var loginname="${sessionScope.user.loginname1}";
			//设置回调函数
			req.onreadystatechange=okCallme;
			
			var url="CheckPass?loginname1="+loginname+"&passwords1="+pass;
			//初始化
			alert(url);
			req.open("get",url,true);
			
			//发送请求
			req.send(null);					
		}
		var is;
    	function okCallme(){
    		if(req.readyState==4 && req.status==200){
				if(req.responseText=="true"){
					document.getElementById("pid").innerHTML = "<span style='color: green'>密码正确!</span>";
					is=true;
				}else{	
					document.getElementById("pid").innerHTML = "<span style='color: red'>密码错误!</span>";
					is=false;
				}
			}
    	}
    				function checkUnique(){
                    		var input=document.getElementById("newPassword").value;
                    		var reinput=document.getElementById("reNewPassword").value;
                    		if(input==reinput){
                    			document.getElementById("reNewPass").innerHTML="两次密码输入一致";
                    			return true;
                    		}else{
                    			document.getElementById("reNewPass").innerHTML="<span style='color: red'>两次密码输入不一致</span>";
                    			return false;
                    		}
                    	} 
                    	function checkAll(){
                    		if(checkUnique() && is){
                    				return true;
                    		}else{
                    			return false;
                    		}
                    	}
                    
    	
    </script>
                    
                    <div>
                        <label for="newPassword">新密码:</label>
                        <input type="password" name="newPassword" id="newPassword"  />
                        <span >*请输入新密码</span>
                    </div>
                   
                    <div>
                        <label for="reNewPassword">确认新密码:</label>
                        <input type="password" name="reNewPassword" id="reNewPassword" onblur="checkUnique()"/>
                        <span id="reNewPass"></span>
                    </div>      
                    <div class="providerAddBtn">
                      <input type="submit" value="保存" />
                       <input type="button" value="返回" onclick="history.back(-1)"/>
                    </div>
                </form>
            </div>
        </div>
    </section>
   <%@include file="foot.jsp" %>
<script src="js/time.js"></script>

</body>

</html>

猜你喜欢

转载自blog.csdn.net/BearDie/article/details/82634585
今日推荐