001package edu.pdx.cs410J.servlets; 002 003import java.io.*; 004import java.util.*; 005import javax.servlet.*; 006import javax.servlet.http.*; 007 008/** 009 * A servlet that returns all sorts of information available from the 010 * servlet API. 011 * 012 * @author David Whitlock 013 * @since Summer 2005 014 */ 015public class ServletInfoServlet extends HttpServlet { 016 protected void doPost(HttpServletRequest request, HttpServletResponse response) 017 throws ServletException, IOException { 018 019 doGet(request, response); 020 } 021 022 protected void doPut(HttpServletRequest request, HttpServletResponse response) 023 throws ServletException, IOException { 024 025 doGet(request, response); 026 } 027 028 /** 029 * Display information from the {@link HttpServletRequest}, {@link 030 * HttpServletResponse} 031 */ 032 protected void doGet(HttpServletRequest request, HttpServletResponse response) 033 throws ServletException, IOException { 034 035 response.setContentType("text/html"); 036 response.setBufferSize(8192); 037 PrintWriter out = response.getWriter(); 038 039 out.println("<html>"); 040 out.println("<title>Servlet Information</title>"); 041 out.println("<body>"); 042 043 out.println("<h1>Servlet Information " + (new Date()) + "</h1>"); 044 045 dump(this.getServletConfig(), out); 046 dump(this.getServletConfig().getServletContext(), out); 047 048 dump(request, out); 049 050 out.println("</body>"); 051 out.println("</html>"); 052 } 053 054 /** 055 * Starts a new HTML table with the given title 056 */ 057 private static void startTable(String title, PrintWriter out) { 058 out.println("<table border=\"1\">"); 059 out.print("<caption align=\"top\">"); 060 out.print(title); 061 out.println("</caption>"); 062 } 063 064 /** 065 * Inserts a row into an HTML table 066 */ 067 private static void tableRow(String key, Object value, PrintWriter out) { 068 out.print(" <tr><td>"); 069 out.print(key); 070 out.print("</td><td>"); 071 out.print(value); 072 out.println("</td></tr>"); 073 } 074 075 /** 076 * Ends an HTML table 077 */ 078 private static void endTable(PrintWriter out) { 079 out.println("</table>"); 080 } 081 082 /** 083 * Dumps information about a <code>HttpServletRequest</code> to the 084 * given <code>PrintWriter</code>. 085 */ 086 static void dump(HttpServletRequest request, PrintWriter out) { 087 startTable("HttpServletRequest", out); 088 089 for (Enumeration attrs = request.getAttributeNames(); attrs.hasMoreElements();) { 090 String name = (String) attrs.nextElement(); 091 Object attr = request.getAttribute(name); 092 tableRow("Attribute \"" + name + "\"", attr, out); 093 } 094 095 tableRow("Character encoding", request.getCharacterEncoding(), out); 096 tableRow("Context length", String.valueOf(request.getContentLength()), out); 097 tableRow("Content type", request.getContentType(), out); 098 tableRow("Local address", request.getLocalAddr(), out); 099 tableRow("Local port", String.valueOf(request.getLocalPort()), out); 100 101 tableRow("Remote address", request.getRemoteAddr(), out); 102 tableRow("Remote host", request.getRemoteHost(), out); 103 tableRow("Remote port", String.valueOf(request.getRemotePort()), out); 104 105 tableRow("Server name", request.getServerName(), out); 106 tableRow("Server port", String.valueOf(request.getServerPort()), out); 107 108 tableRow("Locale", request.getLocale(), out); 109 110 { 111 StringBuffer sb = new StringBuffer(); 112 for (Enumeration locales = request.getLocales(); locales.hasMoreElements();) { 113 sb.append(locales.nextElement()); 114 sb.append(" "); 115 } 116 tableRow("Locales", sb.toString().trim(), out); 117 } 118 119 for (Iterator iter = request.getParameterMap().entrySet().iterator(); iter.hasNext();) { 120 Map.Entry entry = (Map.Entry) iter.next(); 121 Object param = entry.getValue(); 122 if (param instanceof String[]) { 123 String[] array = (String[]) param; 124 StringBuffer sb = new StringBuffer(); 125 for (int i = 0; i < array.length; i++) { 126 sb.append(array[i]); 127 if (i < array.length - 1) { 128 sb.append(", "); 129 } 130 } 131 param = sb; 132 } 133 tableRow("<b>Parameter</b> \"" + entry.getKey() + "\"", String.valueOf(param), out); 134 } 135 136 tableRow("Protocol", request.getProtocol(), out); 137 tableRow("Scheme", request.getScheme(), out); 138 tableRow("Is secure?", String.valueOf(request.isSecure()), out); 139 140 // HTTP 141 142 tableRow("<b>Method</b>", request.getMethod(), out); 143 tableRow("Auth Type", request.getAuthType(), out); 144 145 for (Enumeration names = request.getHeaderNames(); names.hasMoreElements();) { 146 String name = (String) names.nextElement(); 147 String value = request.getHeader(name); 148 tableRow("Header \"" + name + "\"", value, out); 149 } 150 151 tableRow("Path Info", request.getPathInfo(), out); 152 tableRow("Path Translated", request.getPathTranslated(), out); 153 tableRow("Query String", request.getQueryString(), out); 154 tableRow("Remote User", request.getRemoteUser(), out); 155 tableRow("Requested Session ID", request.getRequestedSessionId(), out); 156 tableRow("Request URI", request.getRequestURI(), out); 157 tableRow("Request URL", request.getRequestURL(), out); 158 tableRow("Servlet Path", request.getServletPath(), out); 159 tableRow("User Principal", request.getUserPrincipal(), out); 160 161 endTable(out); 162 } 163 164 /** 165 * Dumps information about a <code>ServletContext</code> to the 166 * given <code>PrintWriter</code> 167 */ 168 private static void dump(ServletContext context, PrintWriter out) { 169 startTable("ServletContext", out); 170 171 tableRow("Context name", context.getServletContextName(), out); 172 tableRow("Server Info", context.getServerInfo(), out); 173 174 for (Enumeration attrs = context.getAttributeNames(); attrs.hasMoreElements();) { 175 String name = (String) attrs.nextElement(); 176 Object value = context.getAttribute(name); 177 tableRow("Attribute \"" + name + "\"", String.valueOf(value), out); 178 } 179 180 for (Enumeration initParams = context.getInitParameterNames(); initParams.hasMoreElements();) { 181 String name = (String) initParams.nextElement(); 182 String param = context.getInitParameter(name); 183 tableRow("Init parameter \"" + name + "\"", param, out); 184 } 185 186 tableRow("Major version", String.valueOf(context.getMajorVersion()), out); 187 tableRow("Minor version", String.valueOf(context.getMinorVersion()), out); 188 189 190 endTable(out); 191 } 192 193 private static void dump(ServletConfig config, PrintWriter out) { 194 startTable("ServletConfig", out); 195 196 tableRow("Name", config.getServletName(), out); 197 198 for (Enumeration initParams = config.getInitParameterNames(); initParams.hasMoreElements();) { 199 String name = (String) initParams.nextElement(); 200 String param = config.getInitParameter(name); 201 tableRow("Init parameter \"" + name + "\"", param, out); 202 } 203 204 endTable(out); 205 } 206 207}