1

This is almost identical to this question but the solution doesn't work for my servlet mapping.

Say I have a servlet which is mapped to /*, therefore all requests are dispatched to that servlet. Now for certain requests I want to include a JSP page WEB-INF/mypage.jsp.
The canonical solution is to use a RequestDispatcher and include the JSP page:

@WebServlet(value = "/*")
public class MyServlet extends HttpServlet
{
    private RequestDispatcher dispatcher_;

    public void init() throws ServletException {
        dispatcher_ = getServletContext().getRequestDispatcher("/WEB-INF/mypage.jsp");
    }
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        // include the jsp page for some requests
        dispatcher_.include(request, response);
    }
}

Unfortunately since the mapping /* has precedence over the mapping *.jsp of the JSP servlet, the dispatch is routed to MyServlet leading to an infinite recursion.

Note that this dispatch problem does not occur if the servlet would be mapped as default servlet (using mapping /). Unfortunately I can't use the default servlet mapping in my special case.

So is there any other way to include a JSP page in a servlet mapped with /*?

Community
  • 1
  • 1
wero
  • 32,544
  • 3
  • 59
  • 84
  • Food for read: http://stackoverflow.com/q/4140448 Note particularly the front controller section and links therein. – BalusC Jan 11 '16 at 12:00

1 Answers1

0
  1. A string beginning with a '/' character and ending with a '/*' suffix is used for path mapping.
  2. A string beginning with a '*.' prefix is used as an extension mapping.
  3. A string containing only the '/' character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
  4. All other strings are used for exact matches only.
Sangram Badi
  • 4,054
  • 9
  • 45
  • 78