requestresponseservlet.java
来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 158 行
JAVA
158 行
package basicServlets;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
/**
* Title: Professional Java Servlet Programming - Chapter 2
* Description: Servlet to demonstrat the use of request and response objects
* Copyright: Copyright (c) 2001
* @author Andrew Harbourne-Thomas
* @version 1.0
*/
public class RequestResponseServlet extends GenericServlet
{
/**
* Output a web page with 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 service(ServletRequest request, ServletResponse response)
throws ServletException, IOException
{
StringBuffer requestTable = getRequestTable(request);
StringBuffer responseTable = getResponseTable(request, 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>" + requestTable + "<hr>");
out.println("<h1>Response Information</h1>" + responseTable);
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 getRequestTable(ServletRequest request)
{
HTMLTable table = new HTMLTable();
table.appendTitleRow("Parameters");
Enumeration e = request.getParameterNames();
while (e.hasMoreElements())
{
String paramName = (String)e.nextElement();
String[] paramValues = request.getParameterValues(paramName);
if (paramValues != null)
{
for (int i = 0; i < paramValues.length; i++)
{
table.appendRow("Parameter: <code>" + paramName + "</code>", paramValues[i]);
}
}
}
table.appendTitleRow("Headers");
int requestLength = request.getContentLength();
if (requestLength == -1)
{
table.appendRow("Request Length", "(Not Specified)");
}
else
{
table.appendRow("Request Length", requestLength);
}
table.appendRow("Content Type", request.getContentType());
table.appendRow("Request Protocol", request.getProtocol());
table.appendTitleRow("Attributes");
request.setAttribute("My Attribute", "My Attribute Value");
request.setAttribute("Another Attribute", "123");
Enumeration enum = request.getAttributeNames();
while (enum.hasMoreElements())
{
String attributeName = (String)enum.nextElement();
Object attributeValue = request.getAttribute(attributeName);
if (attributeValue != null)
{
table.appendRow("Attribute: <code>" + attributeName + "</code>",
attributeValue.toString());
}
}
table.appendTitleRow("Path Information");
table.appendRow("Request Scheme", request.getScheme());
table.appendRow("Request Server", request.getServerName());
table.appendRow("Request Port", request.getServerPort());
table.appendTitleRow("Security");
table.appendRow("Secure Request", request.isSecure());
table.appendTitleRow("Internationalization");
Locale locale = request.getLocale();
table.appendRow("Preferred Locale", locale.toString());
table.appendRow("Country (ISO Code)", locale.getCountry());
table.appendRow("Country", locale.getDisplayCountry());
table.appendRow("Language", locale.getDisplayLanguage());
table.appendRow("Locale Name", locale.getDisplayName());
table.appendRow("Character Encoding", request.getCharacterEncoding());
table.appendTitleRow("Client Information");
table.appendRow("Client IP Address", request.getRemoteAddr());
table.appendRow("Client Name", request.getRemoteHost());
return table.toStringBuffer();
}
/**
* Prepare a HTML table of information about the response made.
*
* @param request Gives access to the request object
* @param response Gives access to the response object
* @return String containing the table
*/
private StringBuffer getResponseTable(ServletRequest request,
ServletResponse response)
{
HTMLTable table = new HTMLTable();
table.appendTitleRow("Internationalization");
Locale locale = response.getLocale();
table.appendRow("Response Locale", locale.toString());
table.appendRow("Country (ISO Code)", locale.getCountry());
table.appendRow("Country", locale.getDisplayCountry());
table.appendRow("Language", locale.getDisplayLanguage());
table.appendRow("Locale Name", locale.getDisplayName());
response.setLocale(locale);
table.appendRow("Character Encoding", response.getCharacterEncoding());
table.appendTitleRow("Buffering");
int buffer = response.getBufferSize();
table.appendRow("Buffer Size", buffer);
//we need this for our dispatcher servlet to indicate that the
//response is already committed
String written = (String) request.getAttribute("buffer");
if (!response.isCommitted() && !"written".equalsIgnoreCase(written))
{
response.setBufferSize(buffer + 1);
}
table.appendRow("Is Response Committed", new Boolean(response.isCommitted()).toString());
return table.toStringBuffer();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?