首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

AJAX之开发范例介绍

2012-06-26 
AJAX之开发实例介绍htmlheadtitlewww.MLDNJAVA.cn/titlescript languagejavascriptvar xmlHt

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{DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance() ;DocumentBuilder builder = factory.newDocumentBuilder() ;Document doc = builder.newDocument() ;String data[] = {"北京","南京","上海","天津","香港"} ;Element allarea = doc.createElement("allarea") ;for(int x=0;x<data.length;x++){Element area = doc.createElement("area") ;Element id = doc.createElement("id") ;Element title = doc.createElement("title") ;id.appendChild(doc.createTextNode("" + (x+1))) ;title.appendChild(doc.createTextNode(data[x])) ;area.appendChild(id) ;area.appendChild(title) ;allarea.appendChild(area) ;}doc.appendChild(allarea) ;TransformerFactory tf = TransformerFactory.newInstance() ;Transformer t = tf.newTransformer() ;t.setOutputProperty(OutputKeys.ENCODING,"GBK") ;DOMSource source = new DOMSource(doc) ;StreamResult result = new StreamResult(bos) ;t.transform(source,result) ;out.println(bos) ;System.out.println(bos) ;out.close() ;}catch(Exception e){e.printStackTrace() ;}}}
?

?

热点排行