GetAndPostExample.java
上传用户:shen332233
上传日期:2021-09-03
资源大小:7478k
文件大小:2k
源码类别:

Ajax

开发平台:

Java

  1. package ajaxbook.chap3;
  2. import java.io.*;
  3. import java.net.*;
  4. import javax.servlet.*;
  5. import javax.servlet.http.*;
  6. public class GetAndPostExample extends HttpServlet {
  7.     
  8.     protected void processRequest(HttpServletRequest request, 
  9.             HttpServletResponse response, String method)
  10.     throws ServletException, IOException {
  11.         
  12.         //Set content type of the response to text/xml
  13.         response.setContentType("text/xml");
  14.         
  15.         //Get the user's input
  16.         String firstName = request.getParameter("firstName");
  17.         String middleName = request.getParameter("middleName");
  18.         String birthday = request.getParameter("birthday");
  19.         
  20.         //Create the response text
  21.         String responseText = "Hello " + firstName + " " + middleName
  22.                 + ". Your birthday is " + birthday + "."
  23.                 + " [Method: " + method + "]";
  24.         
  25.         //Write the response back to the browser
  26.         PrintWriter out = response.getWriter();
  27.         out.println(responseText);
  28.         //Close the writer
  29.         out.close();
  30.     }
  31.     
  32.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  33.     throws ServletException, IOException {
  34.         //Process the request in method processRequest
  35.         processRequest(request, response, "GET");
  36.     }
  37.     
  38.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  39.     throws ServletException, IOException {
  40.         //Process the request in method processRequest
  41.         processRequest(request, response, "POST");
  42.     }
  43. }