blob: 20107676dd879f13e937b31cdd50bb69d37abbae [file] [log] [blame]
Hongqing Liufd5ee812014-05-10 16:32:51 +08001/*
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
刘洪青6266f992017-05-15 21:21:03 +080018import java.io.IOException;
19import java.io.PrintWriter;
20import java.util.Enumeration;
21import java.util.Locale;
22import java.util.ResourceBundle;
Hongqing Liufd5ee812014-05-10 16:32:51 +080023
刘洪青6266f992017-05-15 21:21:03 +080024import javax.servlet.ServletException;
25import javax.servlet.http.HttpServlet;
26import javax.servlet.http.HttpServletRequest;
27import javax.servlet.http.HttpServletResponse;
28import javax.servlet.http.HttpSession;
29
30import util.CookieFilter;
Hongqing Liufd5ee812014-05-10 16:32:51 +080031import util.HTMLFilter;
32
33/**
34 * Example servlet showing request headers
35 *
36 * @author James Duncan Davidson <duncan@eng.sun.com>
37 */
38
39public class RequestHeaderExample extends HttpServlet {
40
刘洪青6266f992017-05-15 21:21:03 +080041 private static final long serialVersionUID = 1L;
42
43 private static final ResourceBundle RB = ResourceBundle.getBundle("LocalStrings");
44
45 @Override
Hongqing Liufd5ee812014-05-10 16:32:51 +080046 public void doGet(HttpServletRequest request,
47 HttpServletResponse response)
48 throws IOException, ServletException
49 {
50 response.setContentType("text/html");
51
52 PrintWriter out = response.getWriter();
53 out.println("<html>");
54 out.println("<head>");
55
刘洪青6266f992017-05-15 21:21:03 +080056 String title = RB.getString("requestheader.title");
Hongqing Liufd5ee812014-05-10 16:32:51 +080057 out.println("<title>" + title + "</title>");
58 out.println("</head>");
59 out.println("<body bgcolor=\"white\">");
60
刘洪青6266f992017-05-15 21:21:03 +080061 // all links relative
Hongqing Liufd5ee812014-05-10 16:32:51 +080062
63 // XXX
64 // making these absolute till we work out the
刘洪青6266f992017-05-15 21:21:03 +080065 // addition of a PathInfo issue
66
Hongqing Liufd5ee812014-05-10 16:32:51 +080067 out.println("<a href=\"../reqheaders.html\">");
68 out.println("<img src=\"../images/code.gif\" height=24 " +
69 "width=24 align=right border=0 alt=\"view code\"></a>");
70 out.println("<a href=\"../index.html\">");
71 out.println("<img src=\"../images/return.gif\" height=24 " +
72 "width=24 align=right border=0 alt=\"return\"></a>");
73
74 out.println("<h3>" + title + "</h3>");
75 out.println("<table border=0>");
刘洪青6266f992017-05-15 21:21:03 +080076 Enumeration<String> e = request.getHeaderNames();
Hongqing Liufd5ee812014-05-10 16:32:51 +080077 while (e.hasMoreElements()) {
刘洪青6266f992017-05-15 21:21:03 +080078 String headerName = e.nextElement();
Hongqing Liufd5ee812014-05-10 16:32:51 +080079 String headerValue = request.getHeader(headerName);
80 out.println("<tr><td bgcolor=\"#CCCCCC\">");
81 out.println(HTMLFilter.filter(headerName));
82 out.println("</td><td>");
刘洪青6266f992017-05-15 21:21:03 +080083 if (headerName.toLowerCase(Locale.ENGLISH).contains("cookie")) {
84 HttpSession session = request.getSession(false);
85 String sessionId = null;
86 if (session != null) {
87 sessionId = session.getId();
88 }
89 out.println(HTMLFilter.filter(CookieFilter.filter(headerValue, sessionId)));
90 } else {
91 out.println(HTMLFilter.filter(headerValue));
92 }
Hongqing Liufd5ee812014-05-10 16:32:51 +080093 out.println("</td></tr>");
94 }
95 out.println("</table>");
96 }
97
刘洪青6266f992017-05-15 21:21:03 +080098 @Override
Hongqing Liufd5ee812014-05-10 16:32:51 +080099 public void doPost(HttpServletRequest request,
100 HttpServletResponse response)
101 throws IOException, ServletException
102 {
103 doGet(request, response);
104 }
105
106}
107