[導入]一個ajax例子
上午比較閑,做了一個ajax的例子,功能是在一個表單中輸入用戶名,光標離開后,到服務器端判斷用戶輸入的名稱是否可用。
1。頁面
2.服務器端:
1。頁面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>注冊頁面</title> <script type="text/javascript"> var xmlHttpRequest = null; function fValidUserName(){ xmlHttpRequest = init(); var url = "ValidUserName?userName="+document.getElementById("userName"); xmlHttpRequest.open("Get", url, true); xmlHttpRequest.send(null); xmlHttpRequest.onreadystatechange=processRequest; } function init(){ if(window.XMLHttpRequest){ return new XMLHttpRequest(); }else if(window.ActiveXObject){ return new ActiveXObject("Microsoft.XMLHTTP"); } } function processRequest(){ if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){ processResponse(); } } function processResponse(){ var msg = xmlHttpRequest.responseXML; var text = msg.getElementsByTagName("Response")[0].firstChild.nodeValue; var span = document.getElementById("validationMessage"); var html; if(text == "valid"){ html = "您輸入的用戶名可以使用"; span.style.backgroundColor="#FFCC66"; }else{ html = "您輸入的用戶名已經被使用"; span.style.backgroundColor="#FF0000"; } span.innerHTML=html; } </script> </head> <body> <form action="register"> <table> <tr><td>用戶名:</td><td><input type="text" name="userName" id="userName" onkeyup="fValidUserName()"><span id="validationMessage" style="background-color:ffcc66"></span></td></tr> </table> </form> </body> </html> |
2.服務器端:
import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ValidUserName extends HttpServlet { //to fix bug,原來沒有發送xml頭,導致一些不確定的因素發生。 private final String XML_HEAD = "<?xml version=\"1.0\" encoding=\"gbk\"?>"; /** * Constructor of the object. */ public ValidUserName() { 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 { response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out = response.getWriter(); out.print(XML_HEAD);//發送xml頭 Random random = new Random(System.currentTimeMillis()); if(random.nextBoolean()){ out.println("<Response>valid</Response>"); System.out.println("valid"); }else{ out.println("<Response>faild</Response>"); System.out.println("not valid"); } out.flush(); out.close(); } } |
文章來源:http://huxiaofei590.blog.163.com/blog/static/3259612200711612415966
posted on 2007-12-06 13:24 ThinkInJava 閱讀(186) 評論(0) 編輯 收藏