mapping servlet to serve my requests

前端 未结 2 477
日久生厌
日久生厌 2021-01-03 04:39

I would like to map a servlet to serve the requests that includes \'app\' and ends with *.html in the following way

/app/*.html

        
相关标签:
2条回答
  • 2021-01-03 05:25

    This is indeed invalid. The wildcard has to be the first or the last character to indicate a suffix or prefix pattern respectively.

    <url-pattern>*.html</url-pattern>
    

    or

    <url-pattern>/app/*</url-pattern>
    

    This is all clearly specified in Section 12.2 of Servlet API specification. Here's an extract of relevance:

    12.2 Specification of Mappings

    In the Web application deployment descriptor, the following syntax is used to define mappings:

    • A string beginning with a ‘/’ character and ending with a ‘/*’ suffix is used for path mapping.
    • A string beginning with a ‘*.’ prefix is used as an extension mapping.
    • The empty string ("") is a special URL pattern that exactly maps to the application's context root, i.e., requests of the form http://host:port/<contextroot>/. In this case the path info is ’/’ and the servlet path and context path is empty string (““).
    • 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.
    • All other strings are used for exact matches only.

    To fix this, you have 2 options:

    1. Use the /app/* pattern and do not put non-HTML files in /app. Put them elsewhere.

    2. Use a different prefix pattern like /controller/* and create a Filter which is mapped on /app/* and does the following in doFilter() method:

      String uri = ((HttpServletRequest) request).getRequestURI();
      if (uri.endsWith(".html")) {
          request.getRequestDispatcher("/controller" + uri).forward(request, response);
      } else {
          chain.doFilter(request, response);
      }
      

    Related:

    • Design patterns in Java web applications - contains simple MVC example
    0 讨论(0)
  • 2021-01-03 05:28

    When do you get this error? What app-server/web-server are you using? Also, please check if /app is being mapped somewhere else.

    0 讨论(0)
提交回复
热议问题