日期:2014-05-16  浏览次数:20674 次

AJAX之开发实例介绍
<html>
<head>
	<title>www.MLDNJAVA.cn</title>
	<script language="javascript">
       var xmlHttp ;
	   function createXMLHttp(){   //AJAX创建代码
	     if(window.XMLHttpRequest){
		     xmlHttp = new XMLHttpRequest();  //firefox浏览器内核
		 } else {
		     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		 }
	   }
	  function checkUserid(userid){
	     createXMLHttp();
		 xmlHttp.open("POST","CheckServlet?userid="+userid);
		 xmlHttp.onreadystatechange = checkUseridCallback
		 xmlHttp.send(null);
		 document.getElementById("msg").innerHTML = "正在验证...";
	  }
	  function checkUseridCallback(){
	     if(xmlHttp.readyState == 4){
		    if(xmlHttp.status == 200){
			  alert("************");
			    var text = xmlHttp.responseText;
				if(text == "true"){
				  document.getElementById("msg").innerHTML = "用户ID重复,无法使用!";
				} else {
				    document.getElementById("msg").innerHTML = "此用户ID可以注册!";
				} 
			}
		 }
	  }
	</script>
</head>
<body>
<form action="regist.jsp" method="post">
  用户ID: <input type="text" name="userid" onblur="checkUserid(this.value)"><span id="msg"></span><br>
  姓&nbsp;&nbsp;&nbsp;&nbsp;名:<input type="text" name="name"><br>
  密&nbsp;&nbsp;&nbsp;&nbsp;码  <input type="password" name="password"><br>
  <input type="submit" value="注册">
  <input type="reset" value="重置">
</form>
</body>
</html>

?

?2,CheckServlet.java

?

package org.lxh.ajaxdemo;
import java.sql.* ;
import java.io.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;
public class CheckServlet extends HttpServlet{
	public static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
	public static final String DBURL = "jdbc:mysql://localhost:3306/mldn";
	public static final String DBUSER = "root" ;
	public static final String DBPASS = "123456" ;

	public void doGet(HttpServletRequest request ,
		               HttpServletResponse response) throws ServletException,IOException {
	 this.doPost(request,response);
	}
	public void doPost(HttpServletRequest request ,
		               HttpServletResponse response) throws ServletException,IOException {
	  request.setCharacterEncoding("GBK");
	  response.setContentType("text/html");
	  Connection conn = null;
	  PreparedStatement pstmt  =null;
	  ResultSet rs = null;
	  PrintWriter out = response.getWriter();
	  String userid =  request.getParameter("userid");
	  try {
    	  Class.forName(DBDRIVER);
		  conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);
		  String sql  = "SELECT COUNT(userid) FROM user WHERE userid=?";
          pstmt = conn.prepareStatement(sql);
		  pstmt.setString(1,userid);
		  rs  = pstmt.executeQuery();
		  if(rs.next()){
		     if(rs.getInt(1) > 0){
			   out.print("true");
			 } else {
			   out.print("false");
			 }
		  }
		System.out.println("**********************");
	  }catch(Exception e){
	     e.printStackTrace();
	  }finally {
	    try
	    {
	    	conn.close();
            
	    }
	    catch (Exception e) { }
	  }
	}
};
?

2,CityServlet.java?

package org.lxh.ajaxdemo ;
import java.io.* ;
import org.w3c.dom.* ;
import javax.xml.parsers.* ;
import javax.xml.transform.* ;
import javax.xml.transform.dom.* ;
import javax.xml.transform.stream.* ;
import javax.servlet.* ;
import javax.servlet.http.* ;

public class CityServlet extends HttpServlet {
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		this.doPost(request,response) ;
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
		response.setContentType("text/xml;charset=GBK") ;
		PrintWriter out = response.getWriter() ;
		ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
		try{
			Docum