You are on page 1of 3

Experiment No.

8
Title : Filters and Event Listener.

Aim : Study of filters and event listeners in JSP.

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.

Filter can be used for ?


 Query the request and act accordingly.
 Block the request-and-response pair from passing any further.
 Modify the request headers and data. You do this by providing a customized version of the
request.
 Modify the response headers and data. You do this by providing a customized version of the
response.
 Interact with external resources.
 Applications of filters include authentication, logging, image conversion, data compression,
encryption, tokenizing streams, and XML transformations

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

3. public void doFilter (ServletRequest, ServletResponse, FilterChain): This method is called by


the container each time a request/response pair is passed through the chain due to a
client request for a resource at the end of the chain.

Steps to add Filter: Filter can be added in netbeans by following steps


1. Write click the project and select add filter
2. Give a name to the filter
3. Select Add the information to deployment descriptor.
4. Select the page/servlet on which the filter should be applied
5. Click on finish and now write the required code in doFilter() method.
What is an Event Listener ? : Event listeners are the objects which respond whenever some event
occurs.

What is the need of an Event Listener ? :


Application events provide notifications of a change in state of the servlet context (each Web application
uses its own servlet context) or of an HTTP session object. You write event listener classes that respond
to these changes in state, and you configure and deploy them in a Web application. The servlet
container generates events that cause the event listener classes to do something.

Types of Event Listeners supported:


Following types of event listeners are supported by JSP:
1. Session Event Listeners: Use HTTP session listeners to manage state or resources associated with
a series of requests made to a web application from the same client or users. It consists of
 HttpSessionListener: which notifies you that an HttpSession has been created, invalidated,
or timed out.
 HttpSessionAttributesListener: which notifies you that attributes have been added,
removed, or replaced on an HttpSession?
2. Context event listeners: Use Servlet context listeners to manage resources or state held at a
virtual machine level for the web application.It consists of
 ServletContextListener: which notifies you that the servlet context has just been created
and is available to service its first request, or that the servlet context is about to be shut
down.
 ServletContextAttributesListener: which notifies you that attributes on the servlet context
have been added, removed, or replaced.
3. Request Event Listeners: Use Servlet request listeners to observe as requests are created and
destroyed, and as attributes are added and removed from a request. It consists of
 ServletRequestListener,: which notifies you that the request is coming in or out of a web
application's scope.
 ServletRequestAttributesListener: which notifies you that request attributes have been
added, removed, or replaced

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.

Steps to To create a listener:

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.

You might also like