dev@glassfish.java.net

Servlet request dispatching

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 02 Sep 2008 08:36:21 +0200

Hi,

Is the following a bug or expected behavior for Servlet (using GF v2
as shipped with NetBeans 6.5 beta).

I have a servlet with the following web.xml:

   <web-app version="2.5">
     <servlet>
         <servlet-name>NewServlet</servlet-name>
         <servlet-class>NewServlet</servlet-class>
     </servlet>
     <servlet-mapping>
         <servlet-name>NewServlet</servlet-name>
         <url-pattern>/*</url-pattern>
     </servlet-mapping>
   </web-app>

And the servlet is implemented as follows:

   public class NewServlet extends HttpServlet {

     ServletContext sc;

     @Override
     public final void init(ServletConfig servletConfig) throws
ServletException {
         sc = servletConfig.getServletContext();
     }

     @Override
     public void service(HttpServletRequest req, HttpServletResponse
resp)
     throws ServletException, IOException {
         RequestDispatcher rd = sc.getRequestDispatcher("/jersey.png");
         rd.forward(req, resp);
     }
   }

There is an image in the Web pages called "jersey.png". So basically
this servlet forward any requests to the resource "/jersey.png". See
end of email for NetBeans project containing the above.

I am getting:

   javax.servlet.ServletException: PWC1232: Exceeded maximum depth
for nested request dispatches: 20

which is not entirely surprising but is this the expected behavior?
Namely if there is a specific resource in the web location and the
RequestDispatcher for that resource returns a non-null value then
should forward result in the processing of that resource and not a
recursive invocation of the servlet.

If this is the expected behavior then is the only way for a servlet
to support the returning of static content to ensure that the path to
the static content does not match the URL pattern of the servlet path?

My questioning behind this is i want to support static content for
Jersey's MVC approach. But i want to avoid restrictions in the naming
of the Servlet path as it mandates a particular URL structure, and
one ends up with a base URL like:

   http://localhost:8080/<deployment base>/<jersey base>/

(with static content also exposed directly, which may not be what one
wants) rather than:

   http://localhost:8080/<deployment base>/

It also means that the Jersey servlet might have to write the content
out itself but then it cannot get access to the meta-data like
content type, etag, last modified etc. which is all very useful stuff
for static content and it is not something i want to have to re-
implement.

Paul.