| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// |
| 5 |
// This program and the accompanying materials are made available under the |
| 6 |
// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 |
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 |
// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 |
// |
| 10 |
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 |
// ======================================================================== |
| 12 |
// |
| 13 |
|
| 14 |
package org.example; |
| 15 |
|
| 16 |
import java.io.IOException; |
| 17 |
|
| 18 |
import jakarta.servlet.ServletException; |
| 19 |
import jakarta.servlet.ServletOutputStream; |
| 20 |
import jakarta.servlet.http.Cookie; |
| 21 |
import jakarta.servlet.http.HttpServlet; |
| 22 |
import jakarta.servlet.http.HttpServletRequest; |
| 23 |
import jakarta.servlet.http.HttpServletResponse; |
| 24 |
|
| 25 |
/** |
| 26 |
* Test Servlet Rewrite |
| 27 |
*/ |
| 28 |
@SuppressWarnings("serial") |
| 29 |
public class RewriteServlet extends HttpServlet |
| 30 |
{ |
| 31 |
|
| 32 |
@Override |
| 33 |
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException |
| 34 |
{ |
| 35 |
doGet(req, res); |
| 36 |
} |
| 37 |
|
| 38 |
@Override |
| 39 |
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException |
| 40 |
{ |
| 41 |
ServletOutputStream out = res.getOutputStream(); |
| 42 |
out.println("<html><body><table>"); |
| 43 |
out.println("<tr><th>Original request URI: </th><td>" + req.getAttribute("requestedPath") + "</td></tr>"); |
| 44 |
out.println("<tr><th>Rewritten request URI: </th><td>" + req.getRequestURI() + "</td></tr>"); |
| 45 |
|
| 46 |
Cookie cookie = null; |
| 47 |
Cookie[] cookies = req.getCookies(); |
| 48 |
if (cookies != null) |
| 49 |
{ |
| 50 |
for (Cookie c : cookies) |
| 51 |
{ |
| 52 |
if (c.getName().equals("visited")) |
| 53 |
{ |
| 54 |
cookie = c; |
| 55 |
break; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
if (cookie != null) |
| 60 |
out.println("<tr><th>Previously visited: </th></td><td>" + cookie.getValue() + "</td></tr>"); |
| 61 |
|
| 62 |
out.println("</table></body></html>"); |
| 63 |
} |
| 64 |
|
| 65 |
@Override |
| 66 |
public String getServletInfo() |
| 67 |
{ |
| 68 |
return "Rewrite Servlet"; |
| 69 |
} |
| 70 |
} |