URL mapping in Tomcat to FrontController servlet

后端 未结 4 1810
太阳男子
太阳男子 2021-01-04 16:47

I\'m trying to follow the pattern at Design Patterns web based applications. It all works fine part from when it comes to maping \"root\" URLs.

I\'d like to put all

4条回答
  •  死守一世寂寞
    2021-01-04 17:12

    The /* URL pattern covers everything, also the forwarded JSP files and static resources like CSS/JS/images. You don't want to have that on a front controller servlet.

    Keep your controller servlet on a more specific URL pattern like /pages/*. You can achieve the functional requirement of getting rid of "/pages" in the URL by grouping the static resources in a common folder like /resources and creating a Filter which is mapped on /* and does the following job in the doFilter() method:

    HttpServletRequest req = (HttpServletRequest) request;
    String path = req.getRequestURI().substring(req.getContextPath().length());
    
    if (path.startsWith("/resources/")) {
        // Just let container's default servlet do its job.
        chain.doFilter(request, response);
    } else {
        // Delegate to your front controller.
        request.getRequestDispatcher("/pages" + path).forward(request, response);
    }
    

    A forwarded JSP resource will by default not match this filter, so it will properly be picked up by the container's own JspServlet.

提交回复
热议问题