image
Filters and Listeners

Contents

Java Servlet Technology
1
Servlet
PREVIEW13m 5s
2
Servlet Request
PREVIEW8m 54s
7
MVC
10m 27s

The course is part of this learning path

Start course
Difficulty
Intermediate
Duration
1h 15m
Students
13
Description

In this course, we will learn the concepts of Java EE 7 with a focus on Servlet Technology.

Learning Objectives

  • Java Servlet Technology with Request, Filter and Listeners

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to improve Java 7 EE knowledge
  • Java developers

Prerequisites

  • Have at least 2 years of Java development experience 
Transcript

Hello dear friends. In this video, we examine the filters and listeners in servlet. Let's start with filters. Servlet filters are Java classes designed to be able to manipulate a request or response objects which is being sent to a web application as servlet or JSP. A filter is used to perform certain logic before or after the functionality of a web application. Think of the scenario where you want to protect your servlet, which means the client must be able to access the servlet's do methods if and only if the client is eligible. To achieve this type of functionality, we can write a filter which gets executed first and determine the permission. If client is eligible, then parse the request to servlet, else return back from filter. Filters are configured in web.xml, so can be easily attached or detached from servlets. And to do so, no code changes are needed. This image depicts the flow between servlets and filters. There can be n numbers of filters configured for a servlet and all filters execute in a chain.

Filter 2 will be executed only if filter 1 parses the request and so on. Once the request parses all of the filters, it reaches servlets. One servlet completes, request goes back to filter n, and then filter n-1, and so on. Filter class has to implement javaX servlet filter interface. Filter interface defines three methods, which means classes that implement filter interface have to implement these three methods. Void void Init, DoFilter, voidDestroy. Init method is called only once after the instantiation to perform any initialization task. We can define initialization parameters in web xmL for filters similar to in INIT params of servlets. Do filter method is called after the Init method and is called each time a filter needs to perform any function. This method performs the actual work of a filter, either modifying the request or the response. Destroy; This method is used to perform any cleanup operation before the container removes a filter instance. I'm not going to describe filter settings right now. Perhaps we will create a project that includes filters in the following servlet videos. That should be enough filters for now.

The final servlet topic is event and listener. Let's continue. The term event refers to the occurrence of something. An event occurs when the state of an object changes. When these exceptions occur, we can conduct certain crucial actions such as collecting total and current logged in users, establishing database tables when deploying the project, building database connection objects and so on. The Java servlet and Java servlet http packages contain numerous event classes and listener interfaces. There are two levels of servlet events, application level and session level. Servlet context level - application level event; Concerns resources or states held at the appliance servlet context objects extent. The session level event involves resources or states associated with a sequence of requests from a single user session. In other words, it is associated with the http session object. Each of these two levels has two event categories; lifecycle changes, attribute changes. You can create one or more event listener classes for each of the four event categories. A single listener class can monitor multiple event categories.

Let's examine them. Servlet context lifecycle changes are servlet context creation, or imminent shutdown of the servlet context. ServletContextAttribute changes are in addition to ServletContexAttributes, removal of ServletContextAttributes, and replacement of ServletContextAttributes. Session lifecycle changes are session creation, session invalidation, and session time out. And the last one, session attribute changes are addition of session attributes, removal of session attributes, and replacement of session attributes. I would now like to focus on listeners. Listeners are the classes which listens to a particular type of event; and when that event occurs, triggers the functionality. Each type of listener is bind to a type of event. There are eight total types of listeners available in servlet framework which listens to a particular event. ServletContextListener, ServletContextAttributeListener, HttpSessionListener, HttpSessionAttributeListener, ServletRequestListener, ServletRequestAttributeListener, HttpSessionActivationListener, HttpSessionBindingListener. ServletContextListener listens to session context event event, which gives a notification when servlet context is initialized or destroyed.

And based on the events, servlet context listener executes the functionality. Servlet context listener is the interface and it defines two methods; contextDestroyed, contextInitialized. ContextDestroyed is executed when the application is destroyed. ContexItnitialized is executed when the application is initialized. Servlet context object can be obtained from ServletContextEvent and the listener can set the attributes in the servlet context object which can be used in servlets later. We can use the ServletContextListener listener for any activity that is required either at the application deployment time or any cleanup activity required when the application is destroyed. Initializing database connections and clean up of database connections are a good example of these operations. ServletContextAttributeListener listens to the SessionContextAttribute event which gives a notification when any object is added, removed or replaced from a servlet context. ServletContextAttributeListener is the interface and it defines three methods; attributeAdded, attributeRemoved, attributeReplaced.

AttributeAdded notifies whenever a new attribute is added to the servlet context. AttributeRemoved notifies whenever the attribute is removed from the servlet context. AttributeReplaced notifies whenever the attribute gets replaced on the servlet context. Attribute name and value that has been added, removed, or replaced can be obtained from the getName and getValue method of ServletContextAttribute event. HttpSessionListener listens to the HttpSessionEvent event, which gives a notification when a session is created or destroyed. HttpSessionListener is the interface and it defines two methods; session destroyed, sessions created. Session destroyed is executed when session is destroyed. Sessions created is executed when session is created.

Session objects can be obtained from HttpSessionEvent. HttpSessionAttributeListener listens to HttpSessionBinding event, which gives a notification when any object is added, removed, or replaced from session.  HttpSessionListener is the interface, HttpSessionAttributeListener is the interface and it defines three methods; attributeAdded, attributeRemoved, attributeReplaced. AttributeAdded notifies whenever a new attribute is added to the session. AttributeRemoved notifies whenever the attribute is removed from the session. AttributeReplaced; it notifies whenever the attribute gets replaced on the session. Attribute name and value that has been added, removed, or replaced can be obtained from the getName and getValue method of HttpSessionBindingEvent. ServletRequestListener listens to ServletRequestEvent event, which gives a notification when request is created or destroyed. ServletRequestListener is the interface and it defines two methods; requestDestroyed, RequestInitialized. RequestDestroyed is executed when the request is destroyed. RequestInitialized is executed when request is initialized. Request object can be obtained from HttpRequestEvent.

ServletRequestAttributeListener listens to the ServletRequestAttributeEvent, which gives a notification when any object is added, removed, or replaced from request. ServletRequestAttributeListener is the interface, and it defines three methods; attributeAdded, attributeRemoved, attributeReplaced. AttributeAdded notifies whenever a new attribute is added to the request. AttributeRemoved notifies whenever the attribute is removed from the request. AttributeReplaced notifies whenever the attribute gets replaced on the request. Attribute name and value that has been added, removed, or replaced can be obtained from the getName and getValue method of ServletRequestAttributeEvent. HttpSessionActivationListener listens to a session when session objects migrate from one virtual machine to another. HttpeSessionActivationListener listens to HttpSessionEvent and http SessionActivationListener interface has two methods; sessionDidActivate, sessionWillPassivate.

SessionDidActivate is executed when session has activated. SessionWillPassivate is executed when session is about to passivate. HttpSessionBindingListener. This listener is used when objects themselves want to know that they have been added to session or removed from the session. HttpSessionAttributeListener class just listens if any object is added, removed, or replaced to session. The HttpSessionBindingListener notifies the object of classes which are getting added or removed from the session. HttpSessionBindingListener listens to HttpSessionBindingEvent and defines following methods; valueBound, valueUnbound. ValueBound notifies object when object is added to session. ValueUnbound notifies when object is removed from session. That's all I have to say about listeners. In the following video, we'll create projects with what we've learned. Until the next video, take care.

 

About the Author
Students
2805
Courses
64
Learning Paths
4

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics