Ajax实现验证(3/4)

前端之家收集整理的这篇文章主要介绍了Ajax实现验证(3/4)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在这个例子中使用java servlet作为服务器端组件,完成验证日期功能
例子包括2个文件,分别是validation.html和ValidationServlet.java

HTML代码里,有一个输入框,相应的onchange事件会触发验证方法
首先调用createXMLHttpRequest()方法,然后把输入值发送到ValidationServlet.
callback()方法从服务器得到结果,然后委托给setMessage()方法,这个方法会检查值以确定使用什么颜色来显示消息。

以下是完整的源代码

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>validation.html</title>
  5. <Meta http-equiv="content-type" content="text/html; charset=UTF-8">
  6. <script language="javascript" type="text/javascript">
  7. var xmlHttp;

  8. function createXMLHttpRequest(){
  9. if(window.ActiveXObject){
  10. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  11. }
  12. else if(window.XMLHttpRequest){
  13. xmlHttp = new XMLHttpRequest();
  14. }
  15. }

  16. function validate(){

  17. createXMLHttpRequest();

  18. var date = document.getElementById("birthDate");
  19. var url = "servlet/ValidationServlet?birthDate=" + escape(date.value);
  20. xmlHttp.open("GET",url,true);
  21. xmlHttp.onreadystatechange = callback;
  22. xmlHttp.send(null);


  23. }

  24. function callback(){
  25. if(xmlHttp.readyState == 4){
  26. if(xmlHttp.status == 200){

  27. var mes = xmlHttp.respon***ML.getElementsByTagName("message")[0].firstChild.data;
  28. // alert(xmlHttp.respon***ML.getElementsByTagName("message")[0].firstChild.data);
  29. var val = xmlHttp.respon***ML.getElementsByTagName("passed")[0].firstChild.data;
  30. setMessage(mes,val);

  31. }
  32. }
  33. }


  34. function setMessage(message,isValid){
  35. var messageArea = document.getElementById("dateMessage");
  36. var fontColor = "red";
  37. if(isValid== "true"){
  38. fontColor = "green";
  39. }

  40. messageArea.innerHTML = "<p style=\'color:" + fontColor +"\'>" + message + "</p>";
  41. }
  42. </script>
  43. </head>

  44. <body>
  45. <h2>Ajax validation Example</h2>
  46. Birth Date(MM/dd/yyyy):<input type="text" size="10" id="birthDate" value="11/11/2011" onchange="validate();" />
  47. <div id="dateMessage"></div>
  48. </body>
  49. </html>

  1. package ajax.study;

  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;

  6. import javax.servlet.ServletException;
  7. import javax.servlet.http.HttpServlet;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;

  10. public class ValidationServlet extends HttpServlet {

  11. private static final long serialVersionUID = 1L;


  12. public void doGet(HttpServletRequest request, HttpServletResponse response)
  13. throws ServletException, IOException {

  14. PrintWriter out = response.getWriter();
  15. response.setContentType("text/xml");
  16. boolean passed = validateDate(request.getParameter("birthDate"));
  17. response.setHeader("Cache-Control", "no-cache");
  18. String message = "You have entered an invalid date";
  19. if(passed){

  20. message = "You have entered a valid date";
  21. }
  22. out.println("<response>");
  23. out.println("<passed>" + Boolean.toString(passed) + "</passed>");
  24. out.println("<message>" + message +"</message>");
  25. out.println("</response>");

  26. out.close();
  27. }


  28. private boolean validateDate(String date) {

  29. boolean isValid = true;
  30. if(date != null){
  31. SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
  32. try{
  33. formatter.parse(date);
  34. }catch(ParseException pe){
  35. System.out.println(pe.toString());
  36. isValid = false;
  37. }
  38. }
  39. else{
  40. isValid = false;
  41. }
  42. return isValid;
  43. }


  44. public void doPost(HttpServletRequest request, IOException {

  45. doGet(request,response);
  46. }


  47. }

猜你在找的Ajax相关文章