Ajax异步验证

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" οnsubmit="return checkRegInfo()">
       <input type="hidden" name="isNameTrue">
       <table>
          <tr>
             <td colspan="2">用户信息</td>
          </tr>
          <tr>
             <td>姓名:</td>
             <td>
                <input type="text" name="userName" οnblur="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, HttpServletResponse response)
			throws ServletException, 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
	}

}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值