Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Servlets and the fly likes How to run code on server startup? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to run code on server startup?" Watch "How to run code on server startup?" New topic
Author

How to run code on server startup?

Dudley Dawson
Ranch Hand

Joined: Dec 14, 2004
Posts: 57
Hi-
I have some objects that will be used frequently throughout my app. I want to add them as servlet context attributes when the web app starts.
What is the best way to do this? I guess I could create a servlet that never gets called via a url, add the attributes on the init() method,and specify load-on-startup in web.xml.
This seems kind of sloppy. Is there a better way?

Thanks!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Originally posted by Dudley Dawson:
This seems kind of sloppy. Is there a better way?


It is sloppy. Use a context listener.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
gopinathang nathan
Ranch Hand

Joined: Oct 28, 2006
Posts: 41
How about binding the object to JNDI tree and refering whenever needed?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Originally posted by gopinathang nathan:
How about binding the object to JNDI tree and refering whenever needed?


Why?
Lucas Lee
Ranch Hand

Joined: Oct 02, 2006
Posts: 53
Agree with Bear .
Context Listener is standard method.
gopinathang nathan
Ranch Hand

Joined: Oct 28, 2006
Posts: 41
Bear, agree with you. I feel JNDI lookup might cause a performance impact and inefficient at this point.
Dudley Dawson
Ranch Hand

Joined: Dec 14, 2004
Posts: 57
Yep. ContextListener seems to be just what I need.

Thanks All!!!
vijaya bacina
Ranch Hand

Joined: Aug 23, 2005
Posts: 155
Does the information in the objects get changed by the users? if that is true and if your application deployed across a server cluster then you can not use the context Listener.
gopinathang nathan
Ranch Hand

Joined: Oct 28, 2006
Posts: 41
good question. seems to be valid though.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

In a clustered environment, a context listener can still be used. You just need to be aware that the application contexts are disparate acrsoss the cluster.
gopinathang nathan
Ranch Hand

Joined: Oct 28, 2006
Posts: 41
Good. i never knew that. Thanks Bear for the info.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

A context listener is exactly what you need.

In years past, Servlet and JSP developers were always running into just this very problem. The solution typically was to code everything into a Servlet that loads at startup, but as you say, that is sloppy.

Similarly, the other problem that existed was "how do you make sure a user has something inside the session as soon as it is created." To address this question, they now provide a session listener.

When you code this listener, don't forget to update the deployment descriptor. A Java file alone won't be recognized by the web container. I put together an online tutorial for coding a listener, just in case you're interested:

How to Create a ContextListener, and other Servlet Lifecycle Listeners

Cheers!

-Cameron McKenzie
Shahar Muky
Greenhorn

Joined: May 19, 2011
Posts: 7
Hi Cameron, the link is broker.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Not surprising after 5 years. I'm sure you can find more recent info by googling.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to run code on server startup?
 
Similar Threads
Can't include other page content with RequestDispatcher.include()
ServletContext attributes and threading issues
how to make a servlet to run at startup of the application
application scope problem
How to share a context (Struts and servlet)