I would like to map a servlet to serve the requests that includes 'app' and ends with *.html in the following way
<url-pattern>/app/*.html</url-pattern>
but on running the application it gives me an error
java.lang.IllegalArgumentException: Invalid <url-pattern>
/app/*.html in servlet mapping
please help me to map that. And please provide me the links where I can learn about these url mapping rule and conventions.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With