Skip to main content

Events and Listeners | Servlet Context Listener

 

Event:

Event is an entity, that notifies someone (listener) when something happens.

There are few kinds of events:

  • ServletContextEvent (notifies when the webapp is initialized / when the servlet Context is made)

  • ServletRequestEvent (notifies the listener each time the request comes)

Listener:

Listener is an entity that is notified each time an event occurs. It is like a piece of code which runs when an event happens. In other words, when we need to associate some part of code to some event, and we want that code to run only when that specific event occurs, then we use event listeners.

Types:
Few event listeners are mentioned below:
  • ServletContextListener        (Initializes resources and performs setup when the web application starts and shuts down).
  • HttpSessionListener           (Notifies when a user's session is created or destroyed).
  • ServletRequestListener        (Notifies when an HTTP request enters or leaves the application). 
  • HttpSessionAttributeListener (Notifies when an attribute is added, removed, or replaced in a session.)

Servlet Context Listener:

Here we should discuss about ServletContextListener:

ServletContextListeneris an interface which we implement in a class. In simple words it is used to make objects or implement logic for the webapp.
 
For Example: We get initParameters of ServletContext given in the web.xml and set an attribute in the ServletContext instance. 



As we know that we can't set objects as parameters, so we use Context listener to set the attributes in the servlet context which we can get in our servlet.

Remember:

getAttribute() returns Object type, so we need to cast it to our desired instance type.

Why we need listeners?

So, we can conclude that these are our flags or signals, when we need to run some code after a specific event, e.g. after a session starts, or after an attribute is created, then we use listeners of that specific event.



Comments

Popular posts from this blog

Java Servlets

  According to me its just a translator, who manages the HTTP requests and handle them to the appropriate function of the API. So then, API turns out to be a term or set of those functions which are kept public. Now let us see in little bit of detail what servlets are: For that we need to see few terms: Static Web Pages: Static web pages are the pages, which are already built, and whose content and design always remain the same.  For Example: About us page of some website. Contact info page of some website Dynamic Web Pages: Dynamic pages are not already defined. They are dependent on search queries. For Example: If we search top 5 books, and click on search button, result would be different. If we search top 5 countries and enter search, result would be different.  So, the search result for every search is different. Working: Now if we have to search static page let's say the first page of any website (which is same for every use in every country) then the process is sim...

What is an API? A Simple Explanation for Programming Beginners

API What is an interface? Interface is a control system, through which we can use something. For Example: The interface of car includes: Steering Wheel Brakes Gear Acceleration pedal Mirrors Interface is concerned with the fact that how to use something, rather than how it works. The Easiest Analogy for an API : The Restaurant Imagine you're at a  restaurant . You are the user. You want food (data or a service). You can't just walk into the kitchen and start cooking. That would be  chaotic  and unsafe! Instead, you interact with the  waiter . You look at a  menu  (the  API  documentation) and give your order to the waiter. The waiter takes your request to the kitchen. The kitchen prepares your food. The waiter then brings the response (your delicious meal) back to you. The  waiter  is the contract, the messenger, the interface in our case the  API . They provide a secure and simple way for you to get what you need from the kitchen ...

Why do I need to create Java Classes? Why don't just use database for searching?

The Bridge Between Data and Display A Conceptual Guide to Java, Databases, and the "Why" behind the Code. 1. The Core Problem When building a web application, you have data sitting in a Database (SQL Server) and a user waiting at a Browser . The challenge is moving that data efficiently and safely. Beginners often ask: "Why do I need to create Java Classes ( Student.java ) and copy data into Lists? Why can't I just print the database results directly to the screen?" To answer this, we must understand how the database actually talks to Java. 2. The Anatomy of a Connection The ResultSet is NOT Data — It is a "Live Wire" A common misconception is that when you run a query, the data is instantly sent to your Java program. It is not. The ResultSet is a Cursor (Pointer) . It points to a row on the database hard drive. The Connection is a Pipe . The Streaming Analogy: Think of a ResultSet like a Phone Call . When you say rs.next() , you are asking the da...