Hakanai
Hakanai

Reputation: 12680

How do I forward to another URL from a Jetty handler?

Somewhere in our chain of servlet filters there is a filter which forwards the request to the sign-in page when a 401 error is sent, as a usability tweak.

I'm trying to convert this to a Jetty handler because someone wants all web applications to be authenticated by the same logic instead of every webapp having to implement their own authentication.

(The main reason we're using a filter approach in the first place is that nobody was able to get Jetty's container-level authentication to work at all - we have the ability to choose Windows auth or built-in auth and want to be able to switch between these at runtime and were never able to figure out how to make that work with Jetty.)

Out in the Jetty handler, there is some logic like this:

private void handleErrorBetter(HttpServletRequest servletRequest,
                               HttpServletResponse servletResponse)
        throws ServletException, IOException {
    if (isPageRequest(servletRequest)) {
        ServletContext rootContext = servletRequest.getServletContext().getContext("/");
        RequestDispatcher dispatcher = rootContext.getRequestDispatcher("/sign_in");
        dispatcher.forward(servletRequest, servletResponse);
    } else {
        // ...
    }
}

servletRequest.getServletContext() appears to correctly return the context for /. Interestingly it appears to do this even if I make a request for a different webapp, but according to the Javadoc I have to use getContext("/") to be sure that I get the root context, so I'm doing that.

Getting the dispatcher succeeds too.

Then I call forward() and this always returns a 404 response to the client.

If I go to /sign_in directly from a web browser, the form loads.

There are only two contexts on the server: the root context /, and a /sample/ context which I'm using to test the second webapp. So I know that /sign_in will be in the root context, but why does forward() give a 404 when forwarding to it?

Upvotes: 2

Views: 2347

Answers (1)

Hakanai
Hakanai

Reputation: 12680

It turned out to be a bug.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=386359

Upvotes: 3

Related Questions