View Javadoc

1   /*
2   * Licensed to the Apache Software Foundation (ASF) under one or more
3   * contributor license agreements.  See the NOTICE file distributed with
4   * this work for additional information regarding copyright ownership.
5   * The ASF licenses this file to You under the Apache License, Version 2.0
6   * (the "License"); you may not use this file except in compliance with
7   * the License.  You may obtain a copy of the License at
8   *
9   *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17  /* $Id: RequestHeaderExample.java 664175 2008-06-06 22:43:44Z djencks $
18   *
19   */
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.util.Enumeration;
24  import java.util.ResourceBundle;
25  
26  import javax.servlet.ServletException;
27  import javax.servlet.http.HttpServlet;
28  import javax.servlet.http.HttpServletRequest;
29  import javax.servlet.http.HttpServletResponse;
30  
31  import util.HTMLFilter;
32  
33  /**
34   * Example servlet showing request headers
35   *
36   * @author James Duncan Davidson <duncan@eng.sun.com>
37   */
38  
39  public class RequestHeaderExample extends HttpServlet {
40  
41      ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
42  
43      public void doGet(HttpServletRequest request,
44                        HttpServletResponse response)
45              throws IOException, ServletException {
46          response.setContentType("text/html");
47  
48          PrintWriter out = response.getWriter();
49          out.println("<html>");
50          out.println("<body bgcolor=\"white\">");
51          out.println("<head>");
52  
53          String title = rb.getString("requestheader.title");
54          out.println("<title>" + title + "</title>");
55          out.println("</head>");
56          out.println("<body>");
57  
58          // all links relative
59  
60          // XXX
61          // making these absolute till we work out the
62          // addition of a PathInfo issue 
63  
64          out.println("<a href=\"../reqheaders.html\">");
65          out.println("<img src=\"../images/code.gif\" height=24 " +
66                  "width=24 align=right border=0 alt=\"view code\"></a>");
67          out.println("<a href=\"../index.html\">");
68          out.println("<img src=\"../images/return.gif\" height=24 " +
69                  "width=24 align=right border=0 alt=\"return\"></a>");
70  
71          out.println("<h3>" + title + "</h3>");
72          out.println("<table border=0>");
73          Enumeration e = request.getHeaderNames();
74          while (e.hasMoreElements()) {
75              String headerName = (String) e.nextElement();
76              String headerValue = request.getHeader(headerName);
77              out.println("<tr><td bgcolor=\"#CCCCCC\">");
78              out.println(HTMLFilter.filter(headerName));
79              out.println("</td><td>");
80              out.println(HTMLFilter.filter(headerValue));
81              out.println("</td></tr>");
82          }
83          out.println("</table>");
84      }
85  
86      public void doPost(HttpServletRequest request,
87                         HttpServletResponse response)
88              throws IOException, ServletException {
89          doGet(request, response);
90      }
91  
92  }
93