java left logo
java middle logo
java right logo

Home arrow Java Tutorials arrow Introduction to Servlet Listener using NetBeans
Main Menu
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

Registered Users: 4116
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


Listener is one of the most popular technologies used in the J2EE web application. It is part of the Java Servlet as defined in Servlet 2.3 but they have their own specific functionalities. There are two most widely used Servlet Listener i.e. ServletContextListener and HttpSessionListener.

By the end of this tutorial, you are expected to be able to implement ServletContextListener as well as HttpSessionListener and able to customize them based on your needs. Additionally, it would be good if you have some basic knowledge on general J2EE web application such as JSP, Java Servlet and NetBeans.


In this article, we are going to see how to implement the simple ServletContextListener as well as simple HttpSessionListener. Here are the details for this article.

  1. What is Listener?
  2. Creating Project in NetBeans
  3. Implementation of ServletContextListener
  4. Implementation of HttpSessionListener
  5. Conclusion

What is Listener?

Listener is basically pre-defined interfaces that are available for developers in the application lifecycle to achieve some tasks especially when dealing with the ServletContext as well as HttpSession objects. While it saves a lot of time, it also makes the application less complex and more maintainable. In one web application, multiple listeners are allowed so it means that ServletContextListener may co-exist with HttpSessionListener. As you may have known, there are two Listeners that are widely used i.e. ServletContextListener and HttpSessionListener. They both are having different functionalities but both are equally important.

ServletContextListener will be executed once your web application is deployed in your application server (Tomcat or etc). If you have any requirements that need to be executed before the application is started, ServletContextListener is the best place for you. ServletContextListener also detects when your web application is removed. For example, if you replace the WAR file in Tomcat, Tomcat will automatically re-deploy your web application based on the latest WAR. Re-deploying means that Tomcat first removes the web application and then deploy the new web application. In this case, ServletContextListener should be able to notice when the web application is destroyed (removed) as well as when the web application is started (deployed). Just for your information, ServletContextListener is produced for you to deal with the ServletContext. Every web application in J2EE will have one ServletContext associated with it. The details of the ServletContext are not covered in this tutorial.

Unlike ServletContextListener, HttpSessionListener deals with the HttpSession object. HttpSession object are always used in every web application and are very useful in maintaining the data as it is available throughout the lifecycle of the web application until it is invalidated or the user closes the browser. This is the definition of HttpSession taken from the Sun website – “Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user”. The details of the HttpSession object are not covered in this tutorial.

Okay, without any more delay, let’s get ready for our tutorial. Start your NetBeans 5.0. After it has been completely started, it should look like below screenshots.

Creating Project in NetBeans

Assuming that you have installed NetBeans 5.5 and having Java Development Kit (jdk) 1.5.

First, we have to create a new Web Application Project for our Listener. This Web Application contains all the JSP pages as well as our Servlet and Listener classes. To create a new Web Application in NetBeans 5.5, you can go to the menu and choose File > New Project. A wizard will instantly be displayed to you and you are required to provide some information to configure your Web Application.


As the wizard is displayed as shown on above illustration, choose Web on the left panel and Web Application on the right panel and click Next button. All the other options are used to develop other kind of projects in NetBeans and irrelevant for our tutorial.


In the next step or the second step of configuring our Web Application, you can provide your Web Application a name. Well, please feel free to name it whatever you want. In this case, to make it self-explanatory, I name our Web Application as ListenerWebApp as shown in the below illustration. In the middle of the wizard, there is an option called Server and it has the value of Bundled (Tomcat 5.5.16). It demonstrates that NetBeans 5.5 will use its bundled Tomcat as the default server. Other configuration should remain the same and press Finish button. You can also press the Next button to go to the last page where you can define the frameworks that you would like to use as shown in below illustration. However, we can skip the last step as we do not use any framework for our Listener. Okay, we have completed our configuration of Web Application and we are ready to implement our first Listener.

< 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

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
Java is a trademark of Sun Microsystems, Inc.