Solved

Servlet or ServletContextListener

Posted on 2006-06-13
4
850 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
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 125 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:mayankeagle
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 27

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now