[ Back | Previous | Next ]

What am I sending to my servlet?

Package:
javax.servlet.*
Product:
JSDK
Release:
1.1.x
Related Links:
General
General
General
General
General
Comment:

This is one of my favourity examples to view whats being parsed from the client to the servlet. I think I snatched it for the JSDK and modified it somewhat. It's just a handy tool.

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import sun.security.x509.*;


/**
 * This servlet simply echos back the request line and
 * headers that were sent by the client, plus any HTTPS information
 * which is accessible.
 *
 */
public
class CGIPropertiesServlet extends HttpServlet {


  public void doPost (HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {
        //value chosen to limit denial of service
        if (req.getContentLength() > 8*1024) {  
            res.setContentType("text/html");
            ServletOutputStream out = res.getOutputStream();
            out.println("[title]Too big");
            out.println("[body][h1]Error - content length >8k not ");
            out.println("[/h1][/body][/html]");
        } else {
            doGet(req, res);
        }
    }
    
  public void doGet (HttpServletRequest req, HttpServletResponse res)
        throws ServletException, IOException
    {
        res.setContentType("text/html");
        ServletOutputStream out = res.getOutputStream();
        out.println("");
        out.println("CGIProperties Servlet");
        out.println("");
    
        out.println("

Requested URL:

"); out.println("
");
        out.println (HttpUtils.getRequestURL (req).toString ());
        out.println("
"); Enumeration enum = getServletConfig().getInitParameterNames(); if (enum != null) { boolean first = true; while (enum.hasMoreElements()) { if (first) { out.println("

Init Parameters

"); out.println("
");
                    first = false;
                    }
                String param = (String) enum.nextElement();
                    out.println(" "+param+": "+getInitParameter(param));
            }
            out.println("
"); } out.println("

Request information:

"); out.println("
");
        print(out, "Request method (getMethod)", req.getMethod());
        print(out, "Request URI (getRequestURI)", req.getRequestURI());
        print(out, "Request protocol (getProtocol)", req.getProtocol());
        print(out, "Servlet path (getServletPath)", req.getServletPath());
        print(out, "Path info (getPathInfo)", req.getPathInfo());
        print(out, "Path translated (getPathTranslated)", req.getPathTranslated());
        print(out, "Query string (getQueryString)", req.getQueryString());
        print(out, "Content length (getContentLength)", req.getContentLength());
        print(out, "Content type (getContentType)", req.getContentType());
        print(out, "Server name (getServerName)", req.getServerName());
        print(out, "Server port (getServerPort)", req.getServerPort());
        print(out, "Remote user (getRemoteUser)", req.getRemoteUser());
        print(out, "Remote address (getRemoteAddr)", req.getRemoteAddr());
        print(out, "Remote host (getRemoteHost)", req.getRemoteHost());
        print(out, "Authorization scheme (getAuthType)", req.getAuthType());
        out.println("
"); Enumeration e = req.getHeaderNames(); if (e.hasMoreElements()) { out.println("

Request headers:

"); out.println("
");
            while (e.hasMoreElements()) {
                String name = (String)e.nextElement();
                out.println(" " + name + ": " + req.getHeader(name));
            }
            out.println("
"); } e = req.getParameterNames(); if (e.hasMoreElements()) { out.println("

Servlet parameters (Single Value style):

"); out.println("
");
            while (e.hasMoreElements()) {
                String name = (String)e.nextElement();
                out.println(" " + name + " = " + req.getParameter(name));
            }
            out.println("
"); } e = req.getParameterNames(); if (e.hasMoreElements()) { out.println("

Servlet parameters (Multiple Value style):

"); out.println("
");
            while (e.hasMoreElements()) {
                String name = (String)e.nextElement();
                String vals[] = (String []) req.getParameterValues(name);
                if (vals != null) {
                    out.print(" " + name + " = "); 
                    out.println(vals[0]);
                    for (int i = 1; i");
            }
            out.println("
"); } String cipherSuite = (String) req.getAttribute ("javax.net.ssl.cipher_suite"); if (cipherSuite != null) { X509Cert certChain [] = (X509Cert []) req.getAttribute ("javax.net.ssl.peer_certificates"); out.println ("

HTTPS Information:

"); out.println("
");
    
            out.println ("Cipher Suite:  " + cipherSuite);
    
            if (certChain != null) {
                for (int i = 0; i < certChain.length; i++) {
                    out.println ("client cert chain [" + i + "] = "
                        + certChain [i].toString ());
                }
            }
    
            // javax.net.ssl.session --> ssl.Session object
            // ... has above data plus creation and last used dates
    
            out.println("
"); } out.println(""); } private void print(ServletOutputStream out, String name, String value) throws IOException { out.print(" " + name + ": "); out.println(value == null ? "" : value); } private void print(ServletOutputStream out, String name, int value) throws IOException { out.print(" " + name + ": "); if (value == -1) { out.println(""); } else { out.println(value); } } private static final String UNKNOWN = ""; public String getServletInfo() { return "A servlet that shows the request headers sent by the client"; } }
1