Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
96
Chapter 4 Handling the Client Request: HTTP Request Headers
should usually check
getProtocol
before specifying response
headers (Chapter 7) that are specific to HTTP 1.1.
4.2 Printing All Headers
Listing 4.1 shows a servlet that simply creates a table of all the headers it
receives, along with their associated values. It also prints out the three com
ponents of the main request line (method, URI, and protocol). Figures 4 1
and 4 2 show typical results with Netscape and Internet Explorer.
Listing 4.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 this
* particular 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";
out.println(ServletUtilities.headWithTitle(title) +
"\n" +
"" + title + "
\n" +
"Request Method: " +
request.getMethod() + "
\n" +
"Request URI: " +
request.getRequestURI() + "
\n" +
"Request Protocol: " +
request.getProtocol() + "
\n" +
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved