Get used AJAX and the Post calls a simple Servlet examples.

  *************************************** AJAX page code ******** ******** 

  <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> Untitled document </ 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 = createQueryString queryString + () + "& 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" onclick="doRequestUsingGET();"/> 
  
  <input Type="button" name="Submit2" value="POST" onclick="doRequestUsingPost();"/> 
  </ P> 

  <div Id="serverResponse"> </ div> 
  </ Form> 

  </ Body> 
  </ Html> 

  *********************************** Servlet code ************* **** 

  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"); 
  ) 

  ) 
  The following 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply