java left logo
java middle logo
java right logo
 

Home arrow Java Tutorials arrow Introduction to Servlet Listener using Eclipse
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4086
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Introduction to Servlet Listener using Eclipse E-mail
User Rating: / 257
PoorBest 

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.

  1. public void contextInitialized(ServletContextEvent arg0)
  2. 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.


Image

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.


Image

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.


Image

Once it is completed, it should look like below.


Image


 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.