java left logo
java middle logo
java right logo
 

Home arrow Java Tutorials arrow Introduction to Servlet Listener using NetBeans
 
 
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: 4097
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 NetBeans E-mail
User Rating: / 80
PoorBest 

Image

Once it is completed, it should look like below.


Image

Now, the most important thing is that we need to make the class “implements” the ServletContextListener. So here is my latest ServletListener.java.


Image

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

After that, we can run the project to test our first Listener which is ServletContextListener.



 
< 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.