ServletRequestEvent and ServletRequestListener

ServletRequestEvent class gives notification about lifecycle events for a ServletRequest. The source of the event is the ServletContext of the web application. ServletRequestListener receives the notifications generated by ServletRequestEvent and performs some specified tasks based on them.

Some Importants Methods of ServletRequestListener

void requestDestroyed(ServletRequestEvent e)is invoked when request is about to go out of scope of the web application..
void requestInitialized(ServletRequestEvent e)is invoked when request is about to come into scope of the web application.


ServletRequestListener example

Hence one can easily implement the ServletRequestListener Interface and provide definitions to the two methods provided by it, requestDestroyed() and requestInitialized().

As the name suggests these methods will be called when request is initialized and when it is destroyed, hence we can use them for various purposes, like variable cleanup when request is destroyed, or performing some initial checks when request is received etc.