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