DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> 用AJAX的Get和Post調用Servlet的簡單示例
用AJAX的Get和Post調用Servlet的簡單示例
編輯:AJAX詳解     

***************************************AJax網頁代碼****************

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xHtml1-transitional.dtd">
<html XMLns="http://www.w3.org/1999/xHtml">
<head>
<meta http-equiv="Content-Type" content="text/Html; charset=gb2312" />
<title>無標題文檔</title>
<script language="Javascript">
var XMLHttp;
function createXMLHttpRequest()
{
 if(window.ActiveXObject)
 {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
 }
 else if(window.XMLHttpRequest)
 {
  xmlHttp=new XMLHttpRequest();
 }
}
function createQueryString()
{
 var firstName=document.getElementById("firstname").value;
 var middleName=document.getElementById("middleName").value;
 var birthday=document.getElementById("birthday").value;
 var queryString="firstName=" + firstName + "&middleName=" + middleName + "&birthday=" + birthday;
 return queryString;
}
function doRequestUsingGET()
{
 createXMLHttpRequest();
 var queryString="../GetAndPost?";
 queryString=queryString+createQueryString() + "&timeStamp=" + new Date().getTime();
 XMLHttp.onreadystatechange=handleStateChange;
 XMLHttp.open("GET",queryString,true);
 XMLHttp.send(null);
}
function doRequestUsingPost()
{
 createXMLHttpRequest();
 var url="../GetAndPost?timeStamp=" + new Date().getTime();
 var queryString=createQueryString();
 XMLHttp.open("POST",url,true);
 XMLHttp.onreadystatechange=handleStateChange;
 XMLHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 XMLHttp.send(queryString);
}
function handleStateChange()
{
 if(XMLHttp.readyState==4)
 {
  if(XMLHttp.status==200)
  {
   parseResults();
  }
 }
}
function parseResults()
{
 var responseDiv=document.getElementById("serverResponse");
 if(responseDiv.hasChildNodes())
 {
  responseDiv.removeChild(responseDiv.childNodes[0]);
 }
 var responseText=document.createTextNode(XMLHttp.responseText);
 responseDiv.appendChild(responseText);
}
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="#">
  <p><br />
    <br />
    <input name="firstName" type="text" id="firstName" />
</p>
  <p>
    <label>
    <input type="text" name="middleName" id="middleName"  />
    </label>
</p>
  <p>
    <input name="birthday" type="text" id="birthday" />
  </p>
  <p> </p>
  <p>
    <input type="button" name="Submit" value="GET"  />
                      
 <input type="button" name="Submit2" value="POST"  />
  </p>

  <div id="serverResponse"></div>
</form>

</body>
</Html>

***********************************Servlet代碼*****************

package temp;

import Java.io.*;
import Java.Net.*;
import Javax.servlet.*;
import Javax.servlet.http.*;

public class GetAndPostExample extends HttpServlet{
  protected void processRequest(HttpServletRequest request,HttpServletResponse response,String method) throws ServletException,IOException
  {
    response.setContentType("text/XML");

    String firstName=request.getParameter("firstName");
    String middleName=request.getParameter("middleName");
    String birthday=request.getParameter("birthday");

    String responseText="hello:" + firstName + "-" + middleName + "。your birthday is " + birthday + "。" + "[method:" + method + "]";

    PrintWriter out=response.getWriter();
    out.println(responseText);

    out.close();
  }

  protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
  {
    processRequest(request,response,"GET");
  }

  protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
  {
    processRequest(request,response,"POST");
  }

}
**************************

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved