AJAX调用SERVLET例子

前端之家收集整理的这篇文章主要介绍了AJAX调用SERVLET例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

工作需要自己写了个例子调用SERVLET的,可以运行,

很简单就是一个index.jsp页面,一个GetAndPostExample servlet后台,和WEB.XML配置文件

index.jsp页面

-------------------------------------------------------------------------------------------------------

[java] view plain copy
  1. <%@pagelanguage="java"import="java.util.*"pageEncoding="gb2312"%>
  2. <%request.setCharacterEncoding("GB2312");%>
  3. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <htmlxmlns="http://www.w3.org/1999/xhtml">
  5. <head>
  6. <Metahttp-equiv="Content-Type"content="text/html;charset=gb2312"/>
  7. <title>AJAX测试</title>
  8. <mce:scriptlanguage="javascript"><!--
  9. varxmlHttp;
  10. //创建xmlHttp
  11. functioncreateXMLHttpRequest()
  12. {
  13. if(window.ActiveXObject)
  14. xmlHttp=newActiveXObject("Microsoft.XMLHTTP");
  15. }
  16. elseif(window.XMLHttpRequest)
  17. newXMLHttpRequest();
  18. }
  19. //拼出要发送的姓名数据
  20. functioncreateQueryString()
  21. {
  22. varfirstName=document.getElementById("firstname").value;
  23. varmiddleName=document.getElementById("middleName").value;
  24. varbirthday=document.getElementById("birthday").value;
  25. varqueryString="firstName="+firstName+"&middleName="+middleName+"&birthday="+birthday;
  26. returnqueryString;
  27. //使用get方式发送
  28. functiondoRequestUsingGET()
  29. createXMLHttpRequest();
  30. varqueryString="./GetAndPostExample?";
  31. queryString=queryString+createQueryString()+"&timeStamp="+newDate().getTime();
  32. xmlHttp.onreadystatechange=handleStateChange;
  33. xmlHttp.open("GET",queryString,true);
  34. xmlHttp.send(null);
  35. //使用post方式发送
  36. functiondoRequestUsingPost()
  37. createXMLHttpRequest();
  38. varurl="./GetAndPostExample?timeStamp="+ varqueryString=createQueryString();
  39. xmlHttp.open("POST",url,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  40. xmlHttp.send(queryString);
  41. functionhandleStateChange()
  42. if(xmlHttp.readyState==4)
  43. if(xmlHttp.status==200)
  44. parseResults();
  45. //解析返回值
  46. functionparseResults()
  47. varresponseDiv=document.getElementById("serverResponse");
  48. if(responseDiv.hasChildNodes())
  49. responseDiv.removeChild(responseDiv.childNodes[0]);
  50. varresponseText=document.createTextNode(xmlHttp.responseText);
  51. alert("后台返回的返回值:"+xmlHttp.responseText);
  52. responseDiv.appendChild(responseText);
  53. //--></mce:script>
  54. </head>
  55. <body>
  56. <formid="form1"name="form1"method="post"action="#">
  57. <p><br/>
  58. <br/>
  59. 姓:<inputname="firstName"type="text"id="firstName"/>
  60. </p>
  61. <p>
  62. <label>
  63. 名:<inputtype="text"name="middleName"id="middleName"/>
  64. </label>
  65. </p>
  66. <p>
  67. 生日:<inputname="birthday"type="text"id="birthday"/>
  68. <p></p>
  69. <inputtype="button"name="Submit"value="GET"onclick="doRequestUsingGET();"/>
  70. <inputtype="button"name="Submit2"value="POST"onclick="doRequestUsingPost();"/>
  71. <divid="serverResponse"></div>
  72. </form>
  73. </body>
  74. </html>

GetAndPostExample

copy

    packagetemp;
  1. importjava.io.IOException;
  2. importjava.io.PrintWriter;
  3. importjavax.servlet.ServletException;
  4. importjavax.servlet.http.HttpServlet;
  5. importjavax.servlet.http.HttpServletRequest;
  6. importjavax.servlet.http.HttpServletResponse;
  7. publicclassGetAndPostExampleextendsHttpServlet{
  8. /**
  9. *Constructoroftheobject.
  10. */
  11. publicGetAndPostExample(){
  12. super();
  13. /**
  14. *Destructionoftheservlet.<br>
  15. */
  16. voiddestroy(){
  17. super.destroy();//Justputs"destroy"stringinlog
  18. //Putyourcodehere
  19. *ThedoGetmethodoftheservlet.<br>
  20. *
  21. *Thismethodiscalledwhenaformhasitstagvaluemethodequalstoget.
  22. *@paramrequest
  23. *therequestsendbytheclienttotheserver
  24. *@paramresponse
  25. *theresponsesendbytheservertotheclient
  26. *@throwsServletException
  27. *ifanerroroccurred
  28. *@throwsIOException
  29. voiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
  30. throwsServletException,IOException{
  31. doPost(request,response);
  32. *ThedoPostmethodoftheservlet.<br>
  33. *Thismethodiscalledwhenaformhasitstagvaluemethodequalsto
  34. *post.
  35. *
  36. *@paramrequest
  37. *therequestsendbytheclienttotheserver
  38. *@paramresponse
  39. *theresponsesendbytheservertotheclient
  40. *@throwsServletException
  41. *ifanerroroccurred
  42. *@throwsIOException
  43. voiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
  44. Stringdata="";
  45. Stringtemp="";
  46. temp=(String)request.getParameter("firstName");
  47. data=data+"第一个名字"+temp;
  48. temp=(String)request.getParameter("middleName");
  49. data=data+"中间的名字"+temp;
  50. temp=(String)request.getParameter("birthday");
  51. data=data+"生日"+temp;
  52. temp=(String)request.getParameter("timeStamp");
  53. data=data+"调用时间"+temp;
  54. System.out.println("获得的数据"+data);
  55. response.setContentType("text/html;charset=gb2312");
  56. PrintWriterout=response.getWriter();
  57. out.println(data);
  58. out.flush();
  59. out.close();
  60. *Initializationoftheservlet.<br>
  61. *ifanerroroccurs
  62. voidinit()throwsServletException{
  63. //Putyourcodehere
  64. }

web.xml

copy

    <?xmlversion="1.0"encoding="UTF-8"?>
  1. <web-appversion="2.4"
  2. xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6. <servlet>
  7. <description>ThisisthedescriptionofmyJ2EEcomponent</description>
  8. <display-name>ThisisthedisplaynameofmyJ2EEcomponent</display-name>
  9. <servlet-name>GetAndPostExample</servlet-name>
  10. <servlet-class>temp.GetAndPostExample</servlet-class>
  11. </servlet>
  12. <servlet-mapping>
  13. <servlet-name>GetAndPostExample</servlet-name>
  14. <url-pattern>/GetAndPostExample</url-pattern>
  15. </servlet-mapping>
  16. <welcome-file-list>
  17. <welcome-file>index.jsp</welcome-file>
  18. </welcome-file-list>
  19. </web-app>

-------------------------------------------------------------------------------------------------------

原文链接:https://www.f2er.com/ajax/164521.html

猜你在找的Ajax相关文章