J. Blustein
Source Code
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 =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">";
private static String headWithTitle(String title) {
return(DOCTYPE + "\n" +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\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) +
"<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\n" +
"<p style='clear:both'>\n" +
" Adapted from Listing 19.12 <q><code>ShowRequestHeaders.java</code></q> " +
" in <cite>Core Web Programming Java</cite>\n" +
" (2nd ed.)" +
" By Marty Hall and Larry Brown\n" +
" Published by Prentice Hall and Sun Microsystems Press.\n" +
"</p> \n\n");
out.println("<p>" +
" Compare these headers (in servlets) to <a" +
" href='/~jamie/cgi-bin/4173/about/env.cgi' " +
" >headers in the <acronym" +
" title='common gateway interface' >CGI</acronym></a>" +
" with <kbd>env.cgi</kbd>." +
"<p>");
out.println("</BODY></HTML>");
}
/** Let the same servlet handle both GET and POST. */
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
- Version:
- 07 Feb. 2007
- CS 4173 Prof:
- J. Blustein
<jamie@cs.dal.ca>