import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; /** Shows all the request headers sent on this request. * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * http://www.corewebprogramming.com/. * May be freely used or adapted. */ public class ShowRequestHeaders extends HttpServlet { private static final String DOCTYPE = ""; private static String headWithTitle(String title) { return(DOCTYPE + "\n" + "\n" + "" + title + "\n"); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Servlet Example: Showing Request Headers"; out.println(headWithTitle(title) + "\n" + "

" + title + "

\n" + "Request Method: " + request.getMethod() + "
\n" + "Request URI: " + request.getRequestURI() + "
\n" + "Request Protocol: " + request.getProtocol() + "

\n" + "\n" + "\n" + "
Header NameHeader Value"); Enumeration headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()) { String headerName = (String)headerNames.nextElement(); out.println("
" + headerName); out.println(" " + request.getHeader(headerName)); } out.println("
\n\n" + "

\n" + " Adapted from Listing 19.12 ShowRequestHeaders.java " + " in Core Web Programming Java\n" + " (2nd ed.)" + " By Marty Hall and Larry Brown\n" + " Published by Prentice Hall and Sun Microsystems Press.\n" + "

\n\n"); out.println("

" + " Compare these headers (in servlets) to headers in the CGI" + " with env.cgi." + "

"); out.println(""); } /** Let the same servlet handle both GET and POST. */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }