Is there a way to define in web.xml
, that certain servlet Filter must be executed only once?
Asked
Active
Viewed 1,202 times
0
2 Answers
1
No. There the Filter
is not for. Whatever the Filter
is doing, should most likely be done by a ServletContextListener
instead.
@WebListener
public class Config implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
// Do stuff during server startup.
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// Do stuff during server shutdown.
}
}
When you're still not on Servlet 3.0 yet, remove @WebListener
and register it in web.xml
the old fasioned way as follows:
<listener>
<listener-class>com.example.Config</listener-class>
</listener>
If you really insist in using a Filter
, you could abuse the init()
method for the job and just call chain.doFilter(request, response)
in the doFilter()
method.

BalusC
- 1,082,665
- 372
- 3,610
- 3,555
0
A round about way would be to have class variable and set it on first access and have all functional code in an 'if' block that checks this variable..

Hareesh Narayanan
- 47
- 1
- 6