例子包括2个文件,分别是validation.html和ValidationServlet.java
在HTML代码里,有一个输入框,相应的onchange事件会触发验证方法。
首先调用createXMLHttpRequest()方法,然后把输入值发送到ValidationServlet.
callback()方法从服务器得到结果,然后委托给setMessage()方法,这个方法会检查值以确定使用什么颜色来显示消息。
以下是完整的源代码:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <title>validation.html</title>
- <Meta http-equiv="content-type" content="text/html; charset=UTF-8">
- <script language="javascript" type="text/javascript">
- var xmlHttp;
-
- function createXMLHttpRequest(){
- if(window.ActiveXObject){
- xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
- }
- else if(window.XMLHttpRequest){
- xmlHttp = new XMLHttpRequest();
- }
- }
-
- function validate(){
-
- createXMLHttpRequest();
-
- var date = document.getElementById("birthDate");
- var url = "servlet/ValidationServlet?birthDate=" + escape(date.value);
- xmlHttp.open("GET",url,true);
- xmlHttp.onreadystatechange = callback;
- xmlHttp.send(null);
-
-
- }
-
- function callback(){
- if(xmlHttp.readyState == 4){
- if(xmlHttp.status == 200){
-
- var mes = xmlHttp.respon***ML.getElementsByTagName("message")[0].firstChild.data;
- // alert(xmlHttp.respon***ML.getElementsByTagName("message")[0].firstChild.data);
- var val = xmlHttp.respon***ML.getElementsByTagName("passed")[0].firstChild.data;
- setMessage(mes,val);
-
- }
- }
- }
-
-
- function setMessage(message,isValid){
- var messageArea = document.getElementById("dateMessage");
- var fontColor = "red";
- if(isValid== "true"){
- fontColor = "green";
- }
-
- messageArea.innerHTML = "<p style=\'color:" + fontColor +"\'>" + message + "</p>";
- }
- </script>
- </head>
-
- <body>
- <h2>Ajax validation Example</h2>
- Birth Date(MM/dd/yyyy):<input type="text" size="10" id="birthDate" value="11/11/2011" onchange="validate();" />
- <div id="dateMessage"></div>
- </body>
- </html>
- package ajax.study;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- public class ValidationServlet extends HttpServlet {
-
- private static final long serialVersionUID = 1L;
-
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- PrintWriter out = response.getWriter();
- response.setContentType("text/xml");
- boolean passed = validateDate(request.getParameter("birthDate"));
- response.setHeader("Cache-Control", "no-cache");
- String message = "You have entered an invalid date";
- if(passed){
-
- message = "You have entered a valid date";
- }
- out.println("<response>");
- out.println("<passed>" + Boolean.toString(passed) + "</passed>");
- out.println("<message>" + message +"</message>");
- out.println("</response>");
-
- out.close();
- }
-
-
- private boolean validateDate(String date) {
-
- boolean isValid = true;
- if(date != null){
- SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
- try{
- formatter.parse(date);
- }catch(ParseException pe){
- System.out.println(pe.toString());
- isValid = false;
- }
- }
- else{
- isValid = false;
- }
- return isValid;
- }
-
-
- public void doPost(HttpServletRequest request, IOException {
-
- doGet(request,response);
- }
-
-
- }