// ======================================================================== // Copyright 1996-2005 Mort Bay Consulting Pty. Ltd. // ------------------------------------------------------------------------ // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // http://www.apache.org/licenses/LICENSE-2.0 // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // ======================================================================== package com.acme; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; /* ------------------------------------------------------------ */ /** Test Servlet RequestDispatcher. * * @author Greg Wilkins (gregw) */ public class DispatchServlet extends HttpServlet { /* ------------------------------------------------------------ */ String pageType; /* ------------------------------------------------------------ */ public void init(ServletConfig config) throws ServletException { super.init(config); } /* ------------------------------------------------------------ */ public void doPost(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException { doGet(sreq, sres); } /* ------------------------------------------------------------ */ public void doGet(HttpServletRequest sreq, HttpServletResponse sres) throws ServletException, IOException { if (sreq.getParameter("wrap") != null) { sreq= new HttpServletRequestWrapper(sreq); sres= new HttpServletResponseWrapper(sres); } if (sreq.getParameter("session") != null) sreq.getSession(true); String prefix= sreq.getContextPath() != null ? sreq.getContextPath() + sreq.getServletPath() : sreq.getServletPath(); String info; if (sreq.getAttribute("javax.servlet.include.servlet_path") != null) info= (String)sreq.getAttribute("javax.servlet.include.path_info"); else info= sreq.getPathInfo(); if (info == null) info= "NULL"; if (info.indexOf(sreq.getServletPath()) > 0) { sres.sendError(403,"Nested " + sreq.getServletPath() + " forbidden."); return; } if (info.indexOf(getServletName()) > 0) { sres.sendError(403,"Nested " + getServletName() + " forbidden."); return; } if (info.startsWith("/includeW/")) { sres.setContentType("text/html"); info= info.substring(9); if (info.indexOf('?') < 0) info += "?Dispatch=include"; else info += "&Dispatch=include"; PrintWriter pout= null; pout= sres.getWriter(); pout.write("
" + prefix + "/includeW/path\n" + prefix + "/includeS/path\n" + prefix + "/forward/path\n" + prefix + "/includeN/name\n" + prefix + "/forwardC/_context/path\n"); } } /* ------------------------------------------------------------ */ public String getServletInfo() { return "Include Servlet"; } /* ------------------------------------------------------------ */ public synchronized void destroy() { } }