?
Solved

Servlet or ServletContextListener

Posted on 2006-06-13
4
Medium Priority
?
880 Views
Last Modified: 2013-11-24
I have code that needs to run at application startup and shutdown in my web application. I currently am handling it in servlet. At startup I instantiate some singletons and "initialize" them. At shutdown I have the singletons perform some "clean up" operations.

My question is would it be better to do this with a ServletContextListener? If so, why?
0
Comment
Question by:FeralCTO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 375 total points
ID: 16900088
I'd use a servlet as your are already doing and store the instance in the ServletContext (if not already there) to make it available to the application. This would ensure there was only a single instance.

0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16900654
>> would it be better to do this with a ServletContextListener?

You can use it, and implement the contextDestroyed () and contextInitialized () methods to take care of that.

>> store the instance in the ServletContext

Yes, that's fine for storing the singleton but you need to trap the shut-down event too....
0
 
LVL 28

Expert Comment

by:rrz
ID: 16903730
>My question is would it be better to do this with a ServletContextListener? If so, why?  
Yes, but only slightly better. I quote the API
"All ServletContextListeners are notified of context initialization before any filter or servlet in the web application is initialized."  
So I guess it depends on whether you intend to use any of your singletons in any Servlet or Filter init methods.   rrz
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question