I have Spring web application. I would like to put some common piece of code which will be executed at the beginning of each HTTP request so that I can check for spams. I have configured DispatcherServlet
in my web.xml
which means DispatcherServlet
is the first entry point for every HTTP request. My question is does DispatcherServlet
provide any method which will be executed first and then the control passes onto the requested annotation controller?

- 965
- 1
- 9
- 21

- 1,533
- 4
- 17
- 24
3 Answers
I would agree to Dave. What you are looking for is a filter/interceptor for all the requests at mapped url. Traditionally this has been done using ServletFilter. This is where you put your custom code. For example.
public FooFilter implements ServletFilter {
@Override
void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException,
ServletException {
// My Custom check for spam.
}
}
Once you have implemented your custom code in ServletFilter all that you need is configure it in web.xml.
<filter>
<filter-name>FooFilter</filter-name>
<filter-class>com.foo.servlet.filters.FooFilter</filter-class>
<init-param>
<param-name>test-param</param-name>
<param-value>Test parameter.</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>FooFilter</filter-name>
<url-pattern>/*</url-pattern> <!-- The URL to be filtered. -->
</filter-mapping>
Its the easiest way to configure a filter and intercept your web requests.
When using Spring framework you would want to use the Sping's HandlerInterceptor. A very good post surrounding when to use what can be found here.
Hope this helps.

- 1
- 1

- 1,592
- 13
- 25
-
ServletFilter? Do you mean [javax.servlet.Filter](https://docs.oracle.com/javaee/7/api/javax/servlet/Filter.html)? By the way, the link is broken. And remember to call [`chain.doFilter(request), response);`](https://docs.oracle.com/javaee/7/api/javax/servlet/FilterChain.html#doFilter-javax.servlet.ServletRequest-javax.servlet.ServletResponse-) – Jason Law Mar 22 '20 at 02:14
IMO this kind of functionality would belong in a HandlerInterceptor
(ref docs).

- 158,873
- 26
- 254
- 302
Servlet Filters will work, because filters are always executed before than any servlet. Filters will be executed before Dispatcher servlet but interceptors will executed after Dispatcher servlet and Before actual handler !

- 350
- 1
- 10
-
This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). - [From Review](/review/low-quality-posts/11304032) – semirturgay Feb 19 '16 at 12:15
-
As per me, I'v concluded two points which answers question, one is before dispatcher servlet one can use filters and after dispatcher and before controllers one can use interceptors of spring, Isn't that sufficient ? – sandeep pandey Jun 08 '16 at 11:34