Professional Documents
Culture Documents
Group 5
Thành viên
Võ Hữu Tiến
Hà Quang Hưng
Dương Văn Việt Anh
ServletRequestListener
void requestDestroyed(ServletRequestEvent sre)
Receives notification that a ServletRequest is about
to go out of scope of the web application.
nvoid requestInitialized(ServletRequestEvent sre)
Receives notification that a ServletRequest is about
to come into scope of the web application
When a servlet request listener is installed, the user request not only
calls the requested Servlet but also calls the listener class
ServletRequestAttributeListener
ServletRequestAttributeListener can listen to these events:
The servlet container calls this method to notify the listener that an
attribute was added to the servlet context.
The servlet container calls this method to notify the listener that an
attribute was removed from the servlet context.
The servlet container calls this method to notify the listener that an
attribute was replaced in the servlet context.
Event Listener Declaration
Event listeners are declared in the application web.xml deployment
descriptor through <listener> elements under the top-level <web-app>
element.
Each listener has its own <listener> element, with a <listener-class> sub-
element specifying the class name.
Within each event category, event listeners should be specified in the order
in which you would like them to be invoked when the application runs.
Event Listener in web.xml
Here is an example of event listener declarations in web.xml:
<web-app>
<listener>
<description>ServletContextListener</description>
<listener-class>controller.AppListener</listener-class>
</listener>
<listener>
<description>HttpSessionListener</description>
<listener-class>controller.SessionListener</listener-class>
</listener>
</web-app>
Coding and Deployment Guidelines
•In a multithreaded application, attribute changes may occur simultaneously.
There is no requirement for the servlet container to synchronize the resulting
notifications; the listener classes themselves are responsible for maintaining
data integrity in such a situation.
•Each listener class file must be packaged in the application WAR file, either
under /WEB-INF/classes or in a JAR file in /WEB-INF/lib.
SUMARY
Is it possible to install multiple listeners of the same type for a web application? What is going
to happen?
Where should the hit counter of a web application be installed? Provide a solution so that when
the web application is shutdown and restarted, the counter will continue to count.
THANK
YOU