httprequestresponseservlet.java

来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 105 行

JAVA
105
字号
package httpExamples;import javax.servlet.*;import javax.servlet.http.*;import java.io.IOException;import java.io.PrintWriter;import java.util.Enumeration;import java.util.Date;/** * Title:        Professional Java Servlet Programming - Chapter 2 * Description: * Copyright:    Copyright (c) 2001 * @author Andrew Harbourne-Thomas * @version 1.0 */public class HttpRequestResponseServlet extends HttpServlet{  private static int cookiesCreated = 0;  /**   * Output a web page with HTTP request information and response data.   *   * @param request The object containing the client request   * @param response The object used to send the response back   */  public void doGet(HttpServletRequest request, HttpServletResponse response)    throws ServletException, IOException  {    StringBuffer httpRequestTable = getHttpRequestTable(request);    StringBuffer httpResponseTable = getHttpResponseTable(response);    response.setContentType("text/html");    PrintWriter out = response.getWriter();    //HTML page    out.println("<html><head><title>RequestResponseServlet</title></head><body>");    out.println("<h1>Request Information</h1>" + httpRequestTable + "<hr>");    out.println("<h1>Response Information</h1>" + httpResponseTable);    out.println("</body></html>");    out.close();  }    /**   * Prepare a HTML table of information about the request made.   *   * @param request The object containing the client request   * @return String containing the table   */  private StringBuffer getHttpRequestTable(HttpServletRequest request)  {    HTMLTable table = new HTMLTable();    table.appendRow("HTTP Request Method", request.getMethod());    table.appendRow("Query String", request.getQueryString());    table.appendRow("Context Path", request.getContextPath());    table.appendRow("Servlet Path", request.getServletPath());    /*  //additional info if required    table.appendRow("Path Info", request.getPathInfo());    table.appendRow("Path Translated", request.getPathTranslated());    table.appendRow("Request URI", request.getRequestURI());    table.appendRow("Request URL", request.getRequestURL().toString());*/    Cookie[] ourCookies = request.getCookies();    if (ourCookies == null || ourCookies.length == 0)    {      table.appendRow("Cookies", "NONE");    }    else    {      for (int i = 0; i < ourCookies.length; i++)      {        String cookieName = ourCookies[i].getName();        String cookieValue = ourCookies[i].getValue();        table.appendRow("Cookie: <code>" + cookieName + "</code>", cookieValue);      }    }    Enumeration e = request.getHeaderNames();    while (e.hasMoreElements())    {      String headerName = (String)e.nextElement();      String headerValue = request.getHeader(headerName);      table.appendRow("Header: <code>" + headerName + "</code>", headerValue);    }    return table.toStringBuffer();  }  /**   * Prepare a HTML table of information about the response made.   *   * @param response Gives access to the response object   * @return String containing the table   */  private StringBuffer getHttpResponseTable(HttpServletResponse response)  {    HTMLTable table = new HTMLTable();    int cookieCount = cookiesCreated++;    String name = Integer.toString(cookieCount);    String value = new Date(System.currentTimeMillis()).toString();    Cookie cookie = new Cookie(name, value);    response.addCookie(cookie);    table.appendRow("Cookie Added:<code>" + name + "</code>", value);    return table.toStringBuffer();  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?