/Users/lyon/j4p/src/net/server/servlets/RequestHeaderExample.java

1    package net.server.servlets; 
2     
3    /* $Id: RequestHeaderExample.java,v 1.2 1999/03/17 02:36:50 duncan Exp $ 
4     * 
5     */ 
6     
7    import javax.servlet.ServletException; 
8    import javax.servlet.http.HttpServlet; 
9    import javax.servlet.http.HttpServletRequest; 
10   import javax.servlet.http.HttpServletResponse; 
11   import java.io.IOException; 
12   import java.io.PrintWriter; 
13   import java.util.Enumeration; 
14   import java.util.ResourceBundle; 
15    
16   /** 
17    * Example servlet showing request headers 
18    * 
19    * @author James Duncan Davidson <duncan@eng.sun.com> 
20    */ 
21    
22   public class RequestHeaderExample extends HttpServlet { 
23    
24       ResourceBundle rb = ResourceBundle.getBundle("LocalStrings"); 
25    
26       public void doGet(HttpServletRequest request, 
27                         HttpServletResponse response) 
28               throws IOException, ServletException { 
29           response.setContentType("text/html"); 
30    
31           PrintWriter out = response.getWriter(); 
32           out.println("<html>"); 
33           out.println("<body bgcolor=\"white\">"); 
34           out.println("<head>"); 
35    
36           String title = rb.getString("requestheader.title"); 
37           out.println("<title>" + title + "</title>"); 
38           out.println("</head>"); 
39           out.println("<body>"); 
40    
41           // all links relative 
42    
43           out.println("<a href=\"../servlets/reqheaders.html\">"); 
44           out.println("<img src=\"../images/code.gif\" height=24 " + 
45                   "width=24 align=right border=0 alt=\"view code\"></a>"); 
46           out.println("<a href=\"../servlets/index.html\">"); 
47           out.println("<img src=\"../images/return.gif\" height=24 " + 
48                   "width=24 align=right border=0 alt=\"return\"></a>"); 
49    
50           out.println("<h3>" + title + "</h3>"); 
51           out.println("<table border=0>"); 
52           Enumeration e = request.getHeaderNames(); 
53           while (e.hasMoreElements()) { 
54               String headerName = (String) e.nextElement(); 
55               String headerValue = request.getHeader(headerName); 
56               out.println("<tr><td bgcolor=\"#CCCCCC\">" + headerName); 
57               out.println("</td><td>" + headerValue + "</td></tr>"); 
58           } 
59           out.println("</table>"); 
60       } 
61    
62       public void doPost(HttpServletRequest request, 
63                          HttpServletResponse response) 
64               throws IOException, ServletException { 
65           doGet(request, response); 
66       } 
67    
68   } 
69    
70