Professional Documents
Culture Documents
8
Title : Filters and Event Listener.
Requirements: JDK Setup, Glassfish Server and Netbeans supporting WEB edition.
Theory:
What is a Filter ? : A filter is an object that can transform the header or content or both of a request or
response. Filters differ from Web components in that they usually do not themselves create a response.
Instead, a filter provides functionality that can be "attached" to any kind of Web resource.
Properties of Filter
Filters are deployed in the deployment descriptor file web.xml and then map to either servlet or
JSP names or URL patterns in your application's deployment descriptor. The deployment
descriptor file web.xml can be found in <Tomcat-installation-directory>\conf directory.
When the JSP container starts up your web application, it creates an instance of each filter that
you have declared in the deployment descriptor. The filters execute in the order that they are
declared in the deployment descriptor.
Methods used with Filter: Following methods are used with the Filter:
1. public void init(FilterConfig filterConfig): This method is called by the web container to indicate
to a filter that it is being placed into service.
2. public void destroy() :This method is called by the web container to indicate to a filter
that it is being taken out of service
Using HTTPSession Listener : HTTP session listener observes when an HTTP session is created or is about
to shut down. Methods used with it are:
public void sessionCreated(HttpSessionEvent se) :This function gives a notification that a
session was created. The notification event is passed as a parameter to this function.
public void sessionDestroyed(HttpSessionEvent se): This function gives a notification that a
session is about to be invalidated. The notification event is passed as a parameter to this
function.
1. In the Projects or Files window, right-click the project and choose New > Other from the pop-up
menu.
2. Under Categories, select Web. Under File Types, select Web Application Listener.
3. Follow the instructions in the New Web Application Listener wizard and click Finish. The source
code appears in the Source Editor.
Conclusion: We studied the concept of filters and event listeners provided by JSP and sample
programs of how to use them.
Assignments:
1. Write a JSP program to implement a filter which checks the password entered by the user. If the
password entered is “admin” then the request would pass further or else it must display the
login page again with an error as “Invalid user”.
2. Write a JSP program to implement a HTTP session listener which displays a message session
created and session destroyed whenever the HTTP session is created/destroyed.