|
1. An Overview of Request HeadersWhen an HTTP client (e.g. a browser) sends a request, it is required to supply a request line (usuallyGET or POST ). If it wants to, it can also
send a number of headers, all of which are optional except for
Content-Length , which is required only for POST
requests. Here are the most common headers:
2. Reading Request Headers from ServletsReading headers is very straightforward; just call thegetHeader method of
the HttpServletRequest , which returns a String if the
header was supplied on this request, null otherwise. However, there
are a couple of headers that are so commonly used that they have special access
methods. The getCookies method returns the contents of the
Cookie header, parsed and stored in an array of Cookie
objects. See the separate section of this tutorial on cookies. The
getAuthType and getRemoteUser methods break the
Authorization header into its component pieces. The
getDateHeader and getIntHeader methods read the
specified header and then convert them to Date and int
values, respectively.
Rather than looking up one particular header, you can use the
Finally, in addition to looking up the request headers, you can get
information on the main request line itself. The 3. Example: Printing all HeadersHere's a servlet that simply creates a table of all the headers it receives, along with their associated values. It also prints out the three components of the main request line (method, URI, and protocol).3.1 ShowRequestHeaders.javaYou can also download the source or try it on-line.package hall; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; 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"; out.println(ServletUtilities.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</BODY></HTML>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } 3.2 ShowRequestHeaders OutputHere are the results of two typical requests, one from Netscape and one from Internet Explorer. You'll see the reason Netscape shows aCookie header when you get to the tutorial
section on cookies.
|