客户端HTML代码(ajax.html):
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title></title>
- <script type="text/javascript" src="jslib/verityown.js"></script>
- </head>
- <body>
- 用户名校验ajax实例,请输入用户名:<br/>
- <input type="text" id="userName" />
- <input type="button" value="校验" onclick="verity()" />
- <div id="result"></div>
- </body>
客户端JS代码(verityown.js):
- var xmlhttp;
- function verity() {
- var userName = document.getElementById("userName").value;
- if (window.XMLHttpRequest) {
- xmlhttp = new XMLHttpRequest();
- if (xmlhttp.overrideMimeType) {
- xmlhttp.overrideMimeType("text/xml");
- }
- } else if (window.ActiveXObject) {
- var activexName = ["MSXML2.XMLHTTP", "Microsoft.XMLHTTP"];
- for (var i = 0; i < activexName.length; i++) {
- try {
- xmlhttp = new ActiveXObject(activexName[i]);
- break;
- } catch (e) {
- }
- }
- }
- xmlhttp.onreadystatechange = callback;
- xmlhttp.open("POST", "AjaxXmlServer", true);
- xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
- xmlhttp.send("name=" + userName);
- }
- function callback(data) {
- if (xmlhttp.readyState == 4) {
- if (xmlhttp.status == 200) {
- var domObj = xmlhttp.responseXML;
- if (domObj) {
- var messageNodes = domObj.getElementsByTagName("message");
- if (messageNodes.length > 0) {
- var messageNode = messageNodes[0].firstChild;
- var responseMessgae = messageNode.nodeValue;
- document.getElementById("result").innerHTML = responseMessgae;
- } else {
- alert("Error");
- }
- } else {
- alert("Error");
- }
- }
- }
- }
服务器端代码(AjaxXmlServer.java):
- import com.sun.deploy.net.HttpRequest;
- import com.sun.deploy.net.HttpResponse;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.ServletException;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.net.URLDecoder;
- public class AjaxXmlServer extends HttpServlet {
- protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
- throws ServletException, IOException {
- try {
- httpServletResponse.setContentType("text/xml;charset=utf-8");
- PrintWriter out = httpServletResponse.getWriter();
- String old = httpServletRequest.getParameter("name");
- StringBuilder builder = new StringBuilder();
- builder.append("<message>");
- if (old == null || old.length() == 0) {
- builder.append("用户名不能为空");
- } else {
- String name = old;
- if (name.equals("alan")) {
- builder.append("用户名[" + name + "] 已经存在, 请使用其它用户名");
- } else {
- builder.append("用户名[" + name + "] 尚未存在, 可以使用该用户名");
- }
- builder.append("</message>");
- }
- out.println(builder.toString());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
- throws ServletException, IOException {
- doGet(httpServletRequest, httpServletResponse);
- }
- }
服务器端配置文件(web.xml):
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
- <servlet>
- <servlet-name>AjaxXmlServer</servlet-name>
- <servlet-class>AjaxXmlServer</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>AjaxXmlServer</servlet-name>
- <url-pattern>/AjaxXmlServer</url-pattern>
- </servlet-mapping>
- </web-app>