5.2 Making a Table of All Request Headers

Listing 5.1 shows a servlet that simply creates a table of all the headers it receives, along with their associated values. It accomplishes this task by calling request.getHeaderNames to obtain an Enumeration of headers in the current request. It then loops down the Enumeration , puts the header name in the left table cell , and puts the result of getHeader in the right table cell. Recall that Enumeration is a standard interface in Java; it is in the java.util package and contains just two methods : hasMoreElements and nextElement .

The servlet also prints three components of the main request line (method, URI, and protocol). Figures 5-1 and 5-2 show typical results with Netscape and Internet Explorer.

Listing 5.1 ShowRequestHeaders.java
 package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; /** Shows all the request headers sent on the current request. */ public class ShowRequestHeaders extends HttpServlet {   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";     String docType =       "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +       "Transitional//EN\">\n";     out.println(docType +                 "<HTML>\n" +                 "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +                 "<BODY BGCOLOR=\"#FDF5E6\">\n" +                 "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +                 "<B>Request Method: </B>" +  request.getMethod()  + "<BR>\n" +                 "<B>Request URI: </B>" +  request.getRequestURI()  + "<BR>\n" +                 "<B>Request Protocol: </B>" +  request.getProtocol()  + "<BR><BR>\n" +                 "<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +                 "<TR BGCOLOR=\"#FFAD00\">\n" +                 "<TH>Header Name<TH>Header Value");     Enumeration headerNames =  request.getHeaderNames();  while(headerNames.hasMoreElements()) {       String headerName = (String)headerNames.nextElement();       out.println("<TR><TD>" + headerName);       out.println("    <TD>" +  request.getHeader(headerName)  );     }     out.println("</TABLE>\n</BODY></HTML>");   }   /** Since this servlet is for debugging, have it    *  handle GET and POST identically.    */   public void doPost(HttpServletRequest request,                      HttpServletResponse response)       throws ServletException, IOException {     doGet(request, response);   } } 
Figure 5-1. Request headers sent by Netscape 7 on Windows 2000.

graphics/05fig01.jpg

Figure 5-2. Request headers sent by Internet Explorer 6 on Windows 2000.

graphics/05fig02.jpg



Core Servlets and JavaServer Pages (Vol. 1.Core Technologies)
Core Servlets and Javaserver Pages: Core Technologies, Vol. 1 (2nd Edition)
ISBN: 0130092290
EAN: 2147483647
Year: 2002
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net