加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Ajax异步验证

发布时间:2020-12-15 22:01:23 所属栏目:百科 来源:网络整理
导读:register.jsp注册页面 %@ page language="java" import="java.util.*" pageEncoding="GBK"%html head title用户注册/title script type="text/javascript" //1:创建XMLHttpRequest组件 var req; var isNameTrue="false"; function createXMLHttpRequest(){ i
register.jsp注册页面
 
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>

<html>
  <head>
    <title>用户注册</title>    
    <script type="text/javascript">
        //1:创建XMLHttpRequest组件       
         var req;
        var isNameTrue="false";
    	function createXMLHttpRequest(){
    	   if(window.XMLHttpRequest) { 
              req = new XMLHttpRequest(); 
           }
           else if(window.ActiveXObject) { 
              req = new ActiveXObject("Microsoft.XMLHTTP"); 
           } 
    	}
    	//2:向服务器发送请求
    	function sendToServer(){
    	   alert("send...");
    	   //取得表单里面的值
    	   var name = regForm.userName.value;
    	   var url = "doCheck?flag=reg&userName="+name;
    	   //alert(name+":"+pwd+":-----:"+url);
    	   //创建XMLHttpRequest组件
    		createXMLHttpRequest();
    		if(req){ 
                 req.open("GET",url,true); 
                 req.onreadystatechange = getResult; 
                 req.send(null); 
            } 
    		//alert("fdfd");
    	}
    	//3:取得服务返的结果并处理
    	function getResult(){
    	   //成功接收并解析完成服务端响应的内容
    	   if(req.readyState==4 && req.status==200){
    	   		var data = req.responseText;
    	   		var info="用户名可以使用!";
    	   		if(data=="true"){
    	   		   info="用户已存在!";
    	   		   isNameTrue="true";   	   		
    	   		}    	   
    	   		document.getElementById("showResult").innerHTML=info;
    	   }
    	}
    	
    	//校验用户名、密码等是否为空
    	function checkRegInfo(){
    	    //alert(isNameTrue);
    		if(isNameTrue=="true"){
    		    alert("用户名已存在!请更换名称!");
    		    isNameTrue="false";
    			return false;
    		}else{
    		    return true;
    		}
    		
    	}
   </script>
  </head>
  
  <body>
    <form name="regForm" action="doReg" method="post" onsubmit="return checkRegInfo()">
       <input type="hidden" name="isNameTrue">
       <table>
          <tr>
             <td colspan="2">用户信息</td>
          </tr>
          <tr>
             <td>姓名:</td>
             <td>
                <input type="text" name="userName" onblur="sendToServer()"/>
                <span id="showResult" style="color:red"></span>
             </td>
          </tr>
          <tr>
             <td>密码:</td>
             <td><input type="password" name="userPwd" /></td>
          </tr>
          <tr>
             <td>确认密码:</td>
             <td><input type="password" name="cfgUserPwd"/></td>
          </tr>
           
          <tr align="center">
             <td colspan="2"><input type="submit" value="注册" /></td>
          </tr>
       </table>
    </form>
    
          
  </body>
</html>
checkeBiz.java业务处理
package com.like.Biz;

import com.like.dao.checkDao;
import com.like.entity.User;

public class checkBiz {
	public  boolean checkUser(String userName){
		return  new checkDao().checkUser(userName);
		}
}
servle处理代码

package com.like.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.like.Biz.LogRBiz;
import com.like.Biz.RegBiz;
import com.like.Biz.checkBiz;
import com.like.entity.User;

public class CheckServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public CheckServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request,HttpServletResponse response)
			throws ServletException,IOException {

		this.doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request,IOException {

		request.setCharacterEncoding("GBK");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		
		checkBiz cb = new checkBiz();
		String name = request.getParameter("userName");
		String flag = request.getParameter("flag");
		if (!"".equals(flag) && flag != null) {
			// ajax 检查用户名是否存在
			// User u = new User();
			// u.setName(name);

			boolean b = cb.checkUser(name);
			out.print(b);
		} else {
			// 调用业务逻辑处理
			User u = new User();
			u.setName(name);

			RegBiz rbiz = new RegBiz();
			int n = rbiz.addUser(u);

			// 根据结果转向
			if (n > 0) {
				request.setAttribute("u",u);
				request.getRequestDispatcher("viewUser.jsp").forward(request,response);
			} else {
				response.sendRedirect("register.jsp");
			}

		}

	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读