001 /*
002 * Copyright 2004 The Apache Software Foundation
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 /* $Id: RequestHeaderExample.java 434248 2006-08-23 23:27:37Z jdillon $
017 *
018 */
019
020 import java.io.*;
021 import java.text.*;
022 import java.util.*;
023 import javax.servlet.*;
024 import javax.servlet.http.*;
025
026 import util.HTMLFilter;
027
028 /**
029 * Example servlet showing request headers
030 *
031 * @author James Duncan Davidson <duncan@eng.sun.com>
032 */
033
034 public class RequestHeaderExample extends HttpServlet {
035
036 ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
037
038 public void doGet(HttpServletRequest request,
039 HttpServletResponse response)
040 throws IOException, ServletException
041 {
042 response.setContentType("text/html");
043
044 PrintWriter out = response.getWriter();
045 out.println("<html>");
046 out.println("<body bgcolor=\"white\">");
047 out.println("<head>");
048
049 String title = rb.getString("requestheader.title");
050 out.println("<title>" + title + "</title>");
051 out.println("</head>");
052 out.println("<body>");
053
054 // all links relative
055
056 // XXX
057 // making these absolute till we work out the
058 // addition of a PathInfo issue
059
060 out.println("<a href=\"../reqheaders.html\">");
061 out.println("<img src=\"../images/code.gif\" height=24 " +
062 "width=24 align=right border=0 alt=\"view code\"></a>");
063 out.println("<a href=\"../index.html\">");
064 out.println("<img src=\"../images/return.gif\" height=24 " +
065 "width=24 align=right border=0 alt=\"return\"></a>");
066
067 out.println("<h3>" + title + "</h3>");
068 out.println("<table border=0>");
069 Enumeration e = request.getHeaderNames();
070 while (e.hasMoreElements()) {
071 String headerName = (String)e.nextElement();
072 String headerValue = request.getHeader(headerName);
073 out.println("<tr><td bgcolor=\"#CCCCCC\">");
074 out.println(HTMLFilter.filter(headerName));
075 out.println("</td><td>");
076 out.println(HTMLFilter.filter(headerValue));
077 out.println("</td></tr>");
078 }
079 out.println("</table>");
080 }
081
082 public void doPost(HttpServletRequest request,
083 HttpServletResponse response)
084 throws IOException, ServletException
085 {
086 doGet(request, response);
087 }
088
089 }
090