上午比较闲,做了一个ajax的例子,功能是在一个表单中输入用户名,光标离开后,到服务器端判断用户输入的名称是否可用。
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