containerservlet.java
来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 144 行
JAVA
144 行
package basicServlets;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import java.util.*;import java.net.*;/** * Title: Professional Java Servlet Programming - Chapter 2 * Description: servlet to demonstrate the use of ServletConfig * and ServletContext interfaces * Copyright: Copyright (c) 2001 * @author Andrew Harbourne-Thomas * @version 1.0 */public class ContainerServlet extends GenericServlet { /** * Output a web page with servlet context and config information. * * @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 configTable = getConfigTable(getServletConfig()); StringBuffer contextTable = getContextTable(getServletContext()); response.setContentType("text/html"); PrintWriter out = response.getWriter(); //HTML page out.println("<html><head><title>ContainerServlet</title></head><body>"); out.println("<h1>Config Information</h1>" + configTable + "<hr>"); out.println("<h1>Context Information</h1>" + contextTable); out.println("</body></html>"); out.close(); } /** * Prepare a HTML table of information about the ServletConfig. * * @param ServletConfig The ServletConfig object * @return String containing the table */ private StringBuffer getConfigTable(ServletConfig config) { HTMLTable table = new HTMLTable(); table.appendRow("Servlet Name", config.getServletName()); Enumeration e = config.getInitParameterNames(); while (e.hasMoreElements()) { String paramName = (String)e.nextElement(); String paramValue = config.getInitParameter(paramName); table.appendRow("Parameter: <code>" + paramName + "</code>", paramValue); } return table.toStringBuffer(); } /** * Prepare a HTML table of information about the ServletContext. * * @param context ServletContext * @return String containing the table */ private StringBuffer getContextTable(ServletContext context) { HTMLTable table = new HTMLTable(); table.appendTitleRow("Attributes"); Enumeration e = context.getAttributeNames(); while (e.hasMoreElements()) { String paramName = (String)e.nextElement(); Object paramObject = context.getAttribute(paramName); String paramValue = ""; if (paramObject instanceof String) { paramValue = (String)context.getAttribute(paramName); } else if (paramObject instanceof String[]) { String[] paramArray = (String[])context.getAttribute(paramName); for (int i = 0; i < paramArray.length; i++) { paramValue = paramValue + paramArray[i] + "<br>"; } } else { paramValue = context.getAttribute(paramName).toString(); } table.appendRow("Attribute: <code>" + paramName + "</code>", paramValue); } table.appendTitleRow("Servlet Infomation"); table.appendRow("Web App Name", context.getServletContextName()); Enumeration enum = context.getInitParameterNames(); while (enum.hasMoreElements()) { String paramName = (String)enum.nextElement(); String paramValue = context.getInitParameter(paramName); table.appendRow("Parameter: <code>" + paramName + "</code>", paramValue); } table.appendTitleRow("Server Information"); table.appendRow("Servlet API version", context.getMajorVersion() + "." + context.getMinorVersion()); table.appendRow("Server Version", context.getServerInfo()); table.appendTitleRow("Server Environment"); table.appendRow("Real Path (<code>\"/\"<code>)", context.getRealPath("/")); Set setPaths = context.getResourcePaths("/"); Object[] arrayPaths = setPaths.toArray(); StringBuffer bufferPaths = new StringBuffer(); for (int i = 0; i < arrayPaths.length; i++) { bufferPaths.append((String)arrayPaths[i]).append("<br>"); } table.appendRow("Paths", bufferPaths.toString()); try { URL url = context.getResource("/index.html"); if (url != null) { table.appendRow("URL (<code>\"/index.html\"<code>)", url.toString()); } else { table.appendRow("URL (<code>\"/index.html\"<code>)", "null"); } } catch (MalformedURLException mfe) { table.appendRow("MalformedURLException (<code>\"/index.html\"<code>)", mfe.getMessage() ); } table.appendRow("Mime Type (<code>\"index.html\"<code>)", context.getMimeType("index.html")); return table.toStringBuffer(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?