BambooleanLogic
BambooleanLogic

Reputation: 8171

404 when dispatching from servlet served with a wildcarded path

tl;dr: Dispatching to an absolute path from a servlet fails when it (the servlet) is bound with a wildcard (/a/*) but not when bound explicitly (/b/b)


I have a Guice webapp with a servlet that dispatches traffic to an HTML file stored in WEB-INF:

@Singleton
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/hello.html").forward(request, response);
    }
}

It is configured with a GuiceServletContextListener...

public class MyApp extends GuiceServletContextListener {
    @Override
    protected Injector getInjector() {
        return Guice.createInjector(new MyModule());
    }

    private static final class MyModule extends ServletModule {
        @Override
        protected void configureServlets() {
            serve("/a/*").with(MyServlet.class);
            serve("/b/b").with(MyServlet.class);
        }
    }
}

...and a web.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <display-name>MyApp</display-name>

    <filter>
        <filter-name>guiceFilter</filter-name>
        <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>guiceFilter</filter-name>
        <url-pattern>/*</url-pattern>                                                
    </filter-mapping>

    <listener>
        <listener-class>test.MyApp</listener-class>
    </listener>
</web-app>

I launch it in Tomcat and go to localhost/b/b and I see my hello.html page. However, when I go to localhost/a/a, I get a HTTP Status 404 - /WEB-INF/hello.html. This seems very strange to me as the two paths, wildcarded or not, are bound to the same servlet, as well as the HTML path being absolute and not really up for interpretation.

Is this the result of a bug, some (to me unknown) behavior I haven't accounted for, or sheer misconfiguration on my part?

Edit:

Based on some further experimentation, include seems to work as expected, which suggests to me that forward has some additional behavior I've missed. Using include will probably hold me over for now, but I'd still like to know what I'm doing wrong.

Upvotes: 1

Views: 270

Answers (1)

BambooleanLogic
BambooleanLogic

Reputation: 8171

I found a GitHub issue which seems to match the problems I've encountered, meaning it's a bug in Guice itself.

The bug was originally reported in 2010 and has been treated with what seems like minor interest from contributors and none whatsoever from those in charge, so it'll probably never be fixed.

For future reference, I'm going to attempt to make do with include for now and consider switching to another framework at some point in the future.

Upvotes: 1

Related Questions