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
Solved

Servlet or ServletContextListener

Posted on 2006-06-13
4
859 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: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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

792 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