What is the need of <mvc:default-servlet-handler />
in Spring MVC . When should we use it. When exactly is it needed. Why should we use it. I gone through few links in stackoverflow, but could not get clear picture or understanding. Can someone explain ?

- 2,204
- 3
- 25
- 44
3 Answers
What is the need of <mvc:default-servlet-handler />
in Spring MVC
?
Using this handler
spring dispatcher will forward all requests to the default Servlet
. To enable the feature either you can use annotations or xml based configuration as below:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
Or in XML:
<mvc:default-servlet-handler/>
What it will do?
The DefaultServletHttpRequestHandler
will attempt to auto-detect
the default Servlet
for the container
at startup time, using a list of known names for most of the major Servlet containers (including Tomcat, Jetty, GlassFish, JBoss, Resin, WebLogic, and WebSphere). If the default Servlet has been custom configured with a different name, or if a different Servlet container is being used where the default Servlet name is unknown, then the default Servlet’s name must be explicitly provided as in the following example:
@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable("myCustomDefaultServlet");
}
}
Or in XML:
<mvc:default-servlet-handler default-servlet-name="myCustomDefaultServlet"/>
When should we use it? When exactly is it needed? Why should we use it?
When you want spring dispatcher to serve static resources
under the web root
using default servlet.
If we are using DefaultServletHttpRequestHandler
, then we can replace :
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/images/**" location="/images/" />
with :
<mvc:default-servlet-handler />
More you can explore here.

- 27,626
- 16
- 90
- 108
Because we usually config the DispatcherServlet with the mapping "/*",but all requests such as .js/.css will also be mapped to DispatcherServlet,so we need a HttpRequestHandler to dispatch these static resources requests to DefaultServlet.

- 69
- 1
- 1
<mvc:default-servlet-handler />
This tag usually use for getting resourses like *.js, *.css, *.jpg and etc.
Tag mvc:default-servlet-handler use DefaultServletHttpRequestHandler which has low property (Integer.MAX_VALUE) than handlers in mvc:annotation-driven:
- DefaultAnnotationHandlerMapping
- AnnotationMethodHandlerAdapter
- AnnotationMethodHandlerExceptionResolve
- see here
For example
Your request like ( http://localhost:8080/jquery.js ) first will be search Controller mapping @RequsetMapping("jquery.js"), and when request don't find any Controller mapping try to get resourse in your folder web or webapp.
If you have webapp/jquery.js, you get this file.

- 493
- 5
- 14