Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 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 |
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 18 | import java.io.IOException;
|
| 19 | import java.io.PrintWriter;
|
| 20 | import java.util.Enumeration;
|
| 21 | import java.util.Locale;
|
| 22 | import java.util.ResourceBundle;
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 23 |
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 24 | import javax.servlet.ServletException;
|
| 25 | import javax.servlet.http.HttpServlet;
|
| 26 | import javax.servlet.http.HttpServletRequest;
|
| 27 | import javax.servlet.http.HttpServletResponse;
|
| 28 | import javax.servlet.http.HttpSession;
|
| 29 |
|
| 30 | import util.CookieFilter;
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 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 |
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 41 | private static final long serialVersionUID = 1L;
|
| 42 |
|
| 43 | private static final ResourceBundle RB = ResourceBundle.getBundle("LocalStrings");
|
| 44 |
|
| 45 | @Override
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 46 | 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 |
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 56 | String title = RB.getString("requestheader.title");
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 57 | out.println("<title>" + title + "</title>");
|
| 58 | out.println("</head>");
|
| 59 | out.println("<body bgcolor=\"white\">");
|
| 60 |
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 61 | // all links relative
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 62 |
|
| 63 | // XXX
|
| 64 | // making these absolute till we work out the
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 65 | // addition of a PathInfo issue
|
| 66 |
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 67 | 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>");
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 76 | Enumeration<String> e = request.getHeaderNames();
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 77 | while (e.hasMoreElements()) {
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 78 | String headerName = e.nextElement();
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 79 | String headerValue = request.getHeader(headerName);
|
| 80 | out.println("<tr><td bgcolor=\"#CCCCCC\">");
|
| 81 | out.println(HTMLFilter.filter(headerName));
|
| 82 | out.println("</td><td>");
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 83 | 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 Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 93 | out.println("</td></tr>");
|
| 94 | }
|
| 95 | out.println("</table>");
|
| 96 | }
|
| 97 |
|
刘洪青 | 6266f99 | 2017-05-15 21:21:03 +0800 | [diff] [blame^] | 98 | @Override
|
Hongqing Liu | fd5ee81 | 2014-05-10 16:32:51 +0800 | [diff] [blame] | 99 | public void doPost(HttpServletRequest request,
|
| 100 | HttpServletResponse response)
|
| 101 | throws IOException, ServletException
|
| 102 | {
|
| 103 | doGet(request, response);
|
| 104 | }
|
| 105 |
|
| 106 | }
|
| 107 |
|