写了个ajax验证用户名是否可用的小例子,记在这,以备后查。
在Eclipse中新建一个dynamic web project,新建两个servlet,一个名为ClassicServlet,使用传统方式进行用户名验证,另一个名为AjaxServlet,使用Ajax方式进行用户名验证。代码如下:
ClassicServlet.java:
package com.dcr.javascript.ajax01; 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; public class ClassicServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try{ String name = request.getParameter("name"); if ("hello".equals(name)){ out.println("用户名" + name + "已经被使用,请重新填写!<a href='javascript:history.go(-1)'>返回</a>"); }else{ out.println("用户名" + name + "可以使用!<a href='javascript:history.go(-1)'>返回</a>"); } }finally{ out.close(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
AjaxServlet.java
package com.dcr.javascript.ajax01; 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; public class AjaxServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try{ String name = request.getParameter("name"); if ("hello".equals(name)){ out.println("用户名" + name + "已经被使用,请重新填写!"); }else{ out.println("用户名" + name + "可以使用!"); } }finally{ out.close(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
这两个servlet对应的html为classic.html,和ajax.html:
classic.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>传统方式</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <form action="Classic" method="get"> <input id="name" name="name" /> <input type="submit" value="验证用户名"> </form> </body> </html>
ajax.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Ajax方式</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <script type="text/javascript" src="../js/ajax01/ajax.js"></script> </head> <body> <input id="name" /> <input type="button" value="验证用户名" onclick="check()" /> <div id="message"></div> </body> </html>
ajax.js:
var XmlHttpResquest; function check(){ if(window.XMLHttpRequest){ XmlHttpResquest = new XMLHttpRequest(); if(XmlHttpResquest.overrideMimeType){ XmlHttpResquest.overrideMimeType("text/xml"); } }else if(window.ActiveXObject){ var activexName = ["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Miscrosoft.XMLHTTP"]; for(var i = 0;i < activexName.length;i++){ try{ XmlHttpResquest = new ActiveXObject(activexName[i]); break; }catch(e){ continue; } } } if(XmlHttpResquest == undefined || XmlHttpResquest == null){ alert("当前浏览器不支持创建XMLHttpResuest对象,请更换浏览器!"); } XmlHttpResquest.onreadystatechange = callback; XmlHttpResquest.open("GET","../ajax01/Ajax?name=" + document.getElementById("name").value,true); XmlHttpResquest.send(null); } function callback() { if(XmlHttpResquest.readyState == 4){ if(XmlHttpResquest.status == 200){ document.getElementById("message").innerHTML = XmlHttpResquest.responseText; } } }
web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>javascript</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <servlet> <servlet-name>Classic</servlet-name> <servlet-class>com.dcr.javascript.ajax01.ClassicServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Classic</servlet-name> <url-pattern>/ajax01/Classic</url-pattern> </servlet-mapping> <servlet> <description></description> <display-name>Ajax</display-name> <servlet-name>Ajax</servlet-name> <servlet-class>com.dcr.javascript.ajax01.AjaxServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Ajax</servlet-name> <url-pattern>/ajax01/Ajax</url-pattern> </servlet-mapping> </web-app>
这样已经很全了,就不再上传源码了。
本来想截个项目结构图的,但是不知道怎么插入到文章内容中去,暂且作罢。