I would like to map a servlet to serve the requests that includes \'app\' and ends with *.html in the following way
/app/*.html
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 formhttp://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:
Use the /app/*
pattern and do not put non-HTML files in /app
. Put them elsewhere.
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);
}
When do you get this error? What app-server/web-server are you using? Also, please check if /app is being mapped somewhere else.