KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > RequestHeaderExample


1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */

16 /* $Id: RequestHeaderExample.java,v 1.3 2004/03/18 16:40:33 jfarcand Exp $
17  *
18  */

19
20 import java.io.*;
21 import java.text.*;
22 import java.util.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26 import util.HTMLFilter;
27
28 /**
29  * Example servlet showing request headers
30  *
31  * @author James Duncan Davidson <duncan@eng.sun.com>
32  */

33
34 public class RequestHeaderExample extends HttpServlet {
35
36     ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
37     
38     public void doGet(HttpServletRequest request,
39                       HttpServletResponse response)
40         throws IOException, ServletException
41     {
42         response.setContentType("text/html");
43
44         PrintWriter out = response.getWriter();
45         out.println("<html>");
46         out.println("<body bgcolor=\"white\">");
47         out.println("<head>");
48
49         String JavaDoc title = rb.getString("requestheader.title");
50         out.println("<title>" + title + "</title>");
51         out.println("</head>");
52         out.println("<body>");
53
54     // all links relative
55

56         // XXX
57
// making these absolute till we work out the
58
// addition of a PathInfo issue
59

60         out.println("<a HREF=\"../reqheaders.html\">");
61         out.println("<img SRC=\"../images/code.gif\" height=24 " +
62                     "width=24 align=right border=0 alt=\"view code\"></a>");
63         out.println("<a HREF=\"../index.html\">");
64         out.println("<img SRC=\"../images/return.gif\" height=24 " +
65                     "width=24 align=right border=0 alt=\"return\"></a>");
66
67         out.println("<h3>" + title + "</h3>");
68         out.println("<table border=0>");
69         Enumeration e = request.getHeaderNames();
70         while (e.hasMoreElements()) {
71             String JavaDoc headerName = (String JavaDoc)e.nextElement();
72             String JavaDoc headerValue = request.getHeader(headerName);
73             out.println("<tr><td bgcolor=\"#CCCCCC\">");
74             out.println(HTMLFilter.filter(headerName));
75             out.println("</td><td>");
76             out.println(HTMLFilter.filter(headerValue));
77             out.println("</td></tr>");
78         }
79         out.println("</table>");
80     }
81
82     public void doPost(HttpServletRequest request,
83                       HttpServletResponse response)
84         throws IOException, ServletException
85     {
86         doGet(request, response);
87     }
88
89 }
90
91
Popular Tags