Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to run code on server startup?

 
Dudley Dawson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dudley Dawson:
This seems kind of sloppy. Is there a better way?


It is sloppy. Use a context listener.
 
gopinathang nathan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about binding the object to JNDI tree and refering whenever needed?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by gopinathang nathan:
How about binding the object to JNDI tree and refering whenever needed?


Why?
 
Lucas Lee
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Bear .
Context Listener is standard method.
 
gopinathang nathan
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear, agree with you. I feel JNDI lookup might cause a performance impact and inefficient at this point.
 
Dudley Dawson
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep. ContextListener seems to be just what I need.

Thanks All!!!
 
vijaya bacina
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
good question. seems to be valid though.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good. i never knew that. Thanks Bear for the info.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cameron, the link is broker.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65218
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not surprising after 5 years. I'm sure you can find more recent info by googling.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic