Solved

Servlet or ServletContextListener

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

911 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