|
Page 4 of 5
Now, we have an error in our class. Why? Because once we try to implement the javax.servlet.ServletContextListener interface, we need to define a few implementation methods for it. That’s why I mentioned earlier that it is the pre-defined interfaces. This is where our job will be started. You need to add two methods for it.
- public void contextInitialized(ServletContextEvent arg0)
- public void contextDestroyed(ServletContextEvent arg0)
So here is my latest ServletListener.java
public class ServletListener implements javax.servlet.ServletContextListener {
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("Servlet Context is initialized....");
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("Servlet Context is destroyed....");
}
}
|
Okay, logically, if our web application is started, the contextInitialized method is executed. If our web application is removed (perhaps shutting down / redeploying web application in Tomcat), contextDestroyed method is executed.
Now, we need to add one more entry in our web.xml.
<listener>
<listener-class>com.mycompany.listener.ServletListener</listener-class>
</listener>
One thing you need to know that this listener entry MUST be before the <servlet> entry (if any). So here is my latest web.xml looks like.
If you have completed everything, simply export your project into WAR file and deploy it to Tomcat and see what’s happening in Tomcat console.
Okay, by this, it means that you have successfully created the ServletContextListener. You can do whatever you want when your web application is started. Normally, this is useful if you are scheduling something then you need to initialize it here.
Implementation of HttpSessionListener
Okay, now, let’s go to HttpSessionListener. HttpSessionListener is mostly used for session management. Unlike ServletContextListener, HttpSessionListener is used to deal with the HttpSession object.
Session is considered created if you assign some attribute to it and similarly, it is considered to be destroyed if you have invalidated it. There is a method that you can call in the Session class to invalidate the session. Have you ever seen a web application that will ask you to re-login if you leave the site untouched for certain periods of times? It checks the time you are not active and will invalidate the session if the inactive time has reached the limit.
Albeit Listener is part of the Servlet, we can create a simple Java class and modify it to be a Listener similar to the ServletContextListener.
You can right click on the src folder (this is your source folder) and choose New and Class. Although Listener is part of Servlet, we can simply create it as a simple Java class. Subsequently, we need to customize it to be a Listener. Do not worry; it would not be too hard. Please follow the tutorial and you should see how we convert this simple Java class to Listener.
Once it is completed, it should look like below.
|
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.