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: 4093
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: / 258
PoorBest 

Introduction

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

Roadmap

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

So let’s start our Eclipse. If you are not sure on how to setup Eclipse and Tomcat, please have a look at the previous tutorials (Java Servlet tutorial).

Creating Project in Eclipse

Assuming that you have installed Eclipse with Web Tools Platform (WTP) Plugin and having Java Development Kit (jdk) 1.5 as well as Tomcat Server 5.5, let’s start to create our first Listener. Start Eclipse and you should see similar to below illustration in your screen. Yes, this is the main page of Eclipse.


Image

To create our Listener, we are required to create a Project called Dynamic Web Project. Actually, in the real world application, this Web Project should contain many things such as Servlet, Listener, Filter, JSP, jar files and etc. Well, back to our tutorial, go to your menu, and choose New then Project. A wizard will be prompted to you.


Image

Go to the Web and choose Dynamic Web Project and press Next. If you do not have Dynamic Web Project available, it means that Web Tools Platform (WTP) plugin is not correctly installed. Please re-download it or look at the documentation of Web Tools Platform (WTP) plugin for more details. You may also create your web project manually using the Java Project in Eclipse but it would be more complex and complicated.


Image

A wizard will be displayed to you. The wizard is mainly used to configure our Dynamic Web Project. You are allowed to name your Dynamic Web Project with any name that you wish but please consider to choose the name that is self-explanatory.



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