public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Shared Info"; out.println( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\">" + "<HTML>\n" + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" + "<UL>\n" + " <LI>Session:"); HttpSession session = request.getSession(true); Enumeration attributes = session.getAttributeNames(); out.println(getAttributeList(attributes)); out.println(" <LI>Current Servlet Context:"); ServletContext application = getServletContext(); attributes = application.getAttributeNames(); out.println(getAttributeList(attributes)); out.println(" <LI>Servlet Context of /shareTest1:"); application = application.getContext("/shareTest1"); if (application == null) { out.println("Context sharing disabled"); } else { attributes = application.getAttributeNames(); out.println(getAttributeList(attributes)); } out.println(" <LI>Cookies:<UL>"); Cookie[] cookies = request.getCookies(); if ((cookies == null) || (cookies.length == 0)) { out.println(" <LI>No cookies found."); } else { Cookie cookie; for (int i = 0; i < cookies.length; i++) { cookie = cookies[i]; out.println(" <LI>" + cookie.getName()); } } out.println(" </UL>\n" + "</UL>\n" + "</BODY></HTML>"); }
public static void showServletInfo(HttpServlet servlet, PrintStream out) { out.println("Servlet Info"); out.println(" getServletName(): " + servlet.getServletName()); out.println(" getRootPath(): " + getRootPath()); out.println(" Init Parameters:"); Enumeration params = servlet.getInitParameterNames(); while (params.hasMoreElements()) { String name = (String) params.nextElement(); out.println(" " + name + ": " + servlet.getInitParameter(name)); } out.println(); ServletContext context = servlet.getServletContext(); out.println("Context Info"); try { out.println(" context.getResource('/'): " + context.getResource("/")); } catch (java.net.MalformedURLException e) { } // cant happen out.println(" context.getServerInfo(): " + context.getServerInfo()); out.println(" name: " + getServerInfoName(context.getServerInfo())); out.println(" version: " + getServerInfoVersion(context.getServerInfo())); out.println(" context.getInitParameterNames():"); params = context.getInitParameterNames(); while (params.hasMoreElements()) { String name = (String) params.nextElement(); out.println(" " + name + ": " + context.getInitParameter(name)); } out.println(" context.getAttributeNames():"); params = context.getAttributeNames(); while (params.hasMoreElements()) { String name = (String) params.nextElement(); out.println(" context.getAttribute(\"" + name + "\"): " + context.getAttribute(name)); } out.println(); }