• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Instantiate and Save Object on Application Startup?  RSS feed

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a datasource that will be used all across the application. I know that Servlet engines do not have jndi providers (app servers do, but I cannot use an app server on this project), so I need something slightly similar - application variable. Also, I want the load of instantiating the object to be only at startup, NOT at the first request of a servlet that uses it. How can I do this? Is it the same in all servlet engines? e.g. Tomcat, Jetty, Resin?
Thanks!
 
boyet silverio
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one way of doing what you want is to create a servlet and a data access object that you'll set in application scope. In your servlet's init() method, instantiate your data accessing object (including calling its data loading method or whatever your implementation); get the servlet context and set this object as an attribute
e.g. getServletContext().setAttribute("data", dataAccessingObject))...
Configure your web.xml to make this servlet be loaded on servlet-engine startup thru the <load-on-startup> tag.
hope this helps.
[ January 28, 2003: Message edited by: boyet silverio ]
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does indeed help! That's what I'm going to do!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!