What Is the Significance of Url-Pattern in Web.Xml and How to Configure Servlet

Difference between / and /* in servlet mapping url pattern

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

The /* on a servlet overrides all other servlets, including all servlets provided by the servletcontainer such as the default servlet and the JSP servlet. Whatever request you fire, it will end up in that servlet. This is thus a bad URL pattern for servlets. Usually, you'd like to use /* on a Filter only. It is able to let the request continue to any of the servlets listening on a more specific URL pattern by calling FilterChain#doFilter().

<url-pattern>/</url-pattern>

The / doesn't override any other servlet. It only replaces the servletcontainer's built in default servlet for all requests which doesn't match any other registered servlet. This is normally only invoked on static resources (CSS/JS/image/etc) and directory listings. The servletcontainer's built in default servlet is also capable of dealing with HTTP cache requests, media (audio/video) streaming and file download resumes. Usually, you don't want to override the default servlet as you would otherwise have to take care of all its tasks, which is not exactly trivial (JSF utility library OmniFaces has an open source example). This is thus also a bad URL pattern for servlets. As to why JSP pages doesn't hit this servlet, it's because the servletcontainer's built in JSP servlet will be invoked, which is already by default mapped on the more specific URL pattern *.jsp.

<url-pattern></url-pattern>

Then there's also the empty string URL pattern . This will be invoked when the context root is requested. This is different from the <welcome-file> approach that it isn't invoked when any subfolder is requested. This is most likely the URL pattern you're actually looking for in case you want a "home page servlet". I only have to admit that I'd intuitively expect the empty string URL pattern and the slash URL pattern / be defined exactly the other way round, so I can understand that a lot of starters got confused on this. But it is what it is.

Front Controller

In case you actually intend to have a front controller servlet, then you'd best map it on a more specific URL pattern like *.html, *.do, /pages/*, /app/*, etc. You can hide away the front controller URL pattern and cover static resources on a common URL pattern like /resources/*, /static/*, etc with help of a servlet filter. See also How to prevent static resources from being handled by front controller servlet which is mapped on /*. Noted should be that Spring MVC has a built in static resource servlet, so that's why you could map its front controller on / if you configure a common URL pattern for static resources in Spring. See also How to handle static content in Spring MVC?

URL Pattern for servlet mapping in web.xml

Map a servlet to the containing directory. Inside that servlet, take apart the URL path and forward to the appropriate named servlet.

web.xml : URL mapping

The URL pattern of / has a special meaning. It namely indicates the "Default Servlet" URL pattern. So every request which does not match any of the other more specific URL patterns in web.xml will ultimately end up in this servlet. Note that this thus also covers static files like plain vanilla HTML/CSS/JS and image files! Normally, the "Default Servlet" is already provided by the servlet container itself (see e.g. Tomcat's DefaultServlet documentation). Overriding the "Default Servlet" in your own webapp should be done with extreme care and definitely not this way.

You need to give your index servlet a different URL pattern. It should be the same as the one you definied in <welcome-file>.

So in case of

<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>

you need to map the index servlet as follows

<servlet-mapping>
<servlet-name>Index</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>

Using an URL rewrite filter as suggested by other answer is unnecessary for the particular purpose you had in mind.

Configuring servlet url-patterns without web.xml

Since the web.xml and @Webservlet are both mechanisms for the server to know where to route requests by examining the war, you would have to be able to manipulate the server if you wanted to do it in code. This is at least theoretically possible, since the server could offer for example a JMX endpoint for configuration or you could go directly into the innards of the server.

However while it might be possible, it would be a non-standard way and you would have to write different tricks for all the servers you want to support. That's not something you want to do.

Finally, if you're creating a library, why does it have servlets in it? It shouldn't be the responsibility of the library to create servlets or decide which urls they're assigned to.

servlet mapping in web.xml (*.* vs *.any_pattern)

The *.* is not a valid URL pattern. The * can only be at start or end of the pattern. If you intend to cover every HTTP request, then you need to use /*.

<servlet-mapping>
<servlet-name>name_of_a_servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

I'd however question if that servlet couldn't better be a filter.



Related Topics



Leave a reply



Submit