Difference between / and /* in servlet mapping configuration

When we develop a Java Web application, one thing that we must do first is configuring the web.xml file. One thing that need to be configured is the servlet mapping. Here is the snippet of the config:

<servlet-mapping>
    <servlet-name>myServlet</servlet>
    <url-pattern>/</url-pattern>
</servlet-mapping>

The configuration above will make myServlet servlet as the default servlet. The default servlet will be the one who handle the request in case there are no other servlet that suit the path. This is a usual setting for your front controller or DispatcherServlet if you are using Spring MVC. With this config, you can still use another pattern for another purpose, such as for serving static files.

The most common mistakes in this config is setting the url-pattern using /*. This pattern will make every request fall through this particular servlet.

Leave a comment