javax.servlet.forward.mapping request attribute is not available in forwarded servlets

Solution Verified - Updated -

Issue

From the Servlet 4.0 specification, we expect the request attribute javax.servlet.forward.mapping to be available in forwarded servlets with RequestDispatcher. However, it is not set in JBoss EAP 7.4.

9.4.2 Forwarded Request Parameters
...
The following request attributes must be set:

javax.servlet.forward.mapping <<<=== added from servlet 4.0
javax.servlet.forward.request_uri
javax.servlet.forward.context_path
javax.servlet.forward.servlet_path
javax.servlet.forward.path_info
javax.servlet.forward.query_string

For example, the following code behaves differently in JBoss EAP 7.4 and Tomcat 9:

@WebServlet("/dispatch")
public class DispatcherServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException, ServletException {
        req.getRequestDispatcher("/next").forward(req, resp);
    }
}

@WebServlet("/next")
public class NextServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {
        resp.setContentType("text/plain; charset=utf-8");
        resp.getWriter().println("request attributes:");
        Enumeration<String> attributeNames = req.getAttributeNames();
        while (attributeNames.hasMoreElements()) {
            String attributeName = attributeNames.nextElement();
            resp.getWriter().println("key: " + attributeName + ", value: " + req.getAttribute(attributeName));
        }
    }
}

JBoss EAP 7.4.9:

$ curl http://127.0.0.1:8080/dispatch
request attributes:
key: javax.servlet.forward.context_path, value: 
key: javax.servlet.forward.servlet_path, value: /dispatch
key: javax.servlet.forward.request_uri, value: /dispatch

Tomcat 9.0.72 (expected):

$ curl http://127.0.0.1:8080/dispatch
request attributes:
key: javax.servlet.forward.request_uri, value: /dispatch
key: javax.servlet.forward.context_path, value: 
key: javax.servlet.forward.servlet_path, value: /dispatch
key: javax.servlet.forward.mapping, value: org.apache.catalina.core.ApplicationMapping$MappingImpl@2248cb0c  <<<===

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP)
    • 7.4

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content