用AJAX的Get和Post调用Servlet的简单示例。

***************************************AJAX网页代码****************





无标题文档



 



   

   


 


   


 


   
 


 

 


 


   
                      
 
 

 




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

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

你可能感兴趣的:(AJAX)