View Javadoc

1   // ========================================================================
2   // Copyright (c) 2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package com.acme;
15  import java.io.IOException;
16  
17  import javax.servlet.ServletException;
18  import javax.servlet.ServletOutputStream;
19  import javax.servlet.http.Cookie;
20  import javax.servlet.http.HttpServlet;
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.http.HttpServletResponse;
23  
24  
25  /* ------------------------------------------------------------ */
26  /** Test Servlet Rewrite
27   * 
28   * 
29   */
30  public class RewriteServlet extends HttpServlet
31  {
32      /* ------------------------------------------------------------ */
33      @Override
34      public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
35      {
36          doGet(req, res);
37      }
38  
39      /* ------------------------------------------------------------ */
40      @Override
41      public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
42      {
43          ServletOutputStream out = res.getOutputStream();
44          out.println("<html><body><table>");
45          out.println("<tr><th>Original request URI: </th><td>" + req.getAttribute("requestedPath") + "</td></tr>");
46          out.println("<tr><th>Rewritten request URI: </th><td>" + req.getRequestURI() + "</td></tr>");
47         
48          Cookie cookie = null;
49          for(Cookie c: req.getCookies())
50          {
51              if (c.getName().equals("visited"))
52              {
53                  cookie = c;
54                  break;
55              }
56          }
57          if (cookie!=null)
58              out.println("<tr><th>Previously visited: </th></td><td>" + cookie.getValue()+"</td></tr>");
59  
60          out.println("</table></body></html>");
61      }
62  
63      /* ------------------------------------------------------------ */
64      @Override
65      public String getServletInfo()
66      {
67          return "Rewrite sServlet";
68      }
69  }