// // ======================================================================== // Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 // which is available at https://www.apache.org/licenses/LICENSE-2.0. // // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 // ======================================================================== // package org.example; import java.io.IOException; import jakarta.servlet.ServletException; import jakarta.servlet.ServletOutputStream; import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; /** * Test Servlet Rewrite */ @SuppressWarnings("serial") public class RewriteServlet extends HttpServlet { @Override public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doGet(req, res); } @Override public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { ServletOutputStream out = res.getOutputStream(); out.println(""); out.println(""); out.println(""); Cookie cookie = null; Cookie[] cookies = req.getCookies(); if (cookies != null) { for (Cookie c : cookies) { if (c.getName().equals("visited")) { cookie = c; break; } } } if (cookie != null) out.println(""); out.println("
Original request URI: " + req.getAttribute("requestedPath") + "
Rewritten request URI: " + req.getRequestURI() + "
Previously visited: " + cookie.getValue() + "
"); } @Override public String getServletInfo() { return "Rewrite Servlet"; } }