File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Can JSP spawn persistent/independent serverside objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can JSP spawn persistent/independent serverside objects?" Watch "Can JSP spawn persistent/independent serverside objects?" New topic

Can JSP spawn persistent/independent serverside objects?

Michael Arnett
Ranch Hand

Joined: Mar 22, 2001
Posts: 65
Hi all,
I was wondering if anyone had utilized JSP to spawn objects that ran independently on the server, where independent means independent from the calling applications thread/session/lifetime. I am brand spanking new to JSP so do not be afraid to give lots of details if indeed this is possible or alternatives if not. Or alternatively, is it possible to have a global object on the server (similar to an application object in ASP) that comes into existence when the server is booted and persists till the server is shutdown? If both are possible, feel free to weigh in on the advantages or disadvantages of either or both.
Thanks in advance!

Sun Certified Programmer for the Java 2 Platform 1.4
Jason Menard

Joined: Nov 09, 2000
Posts: 6450
The short answer is no, you can only spawn objects for the lifetime of your application. This means basically that you can have an object in existance from the time your application is first run until it is destroyed. You can store these objects within the ServletContext (application scope for JSPs). Only applications which share the same ServletContext will have access to objects stored there.
It is possible on some servers to specify servlets to run at server startup I believe. A cheap hack is to make such a servlet which instantiates an object in its init() method and then places that object into the ServletContext. Now all other applications sharing that ServletContext have access to your object.
The preferred way is that each application has its own ServletContext, but I know that often people don't bother configuring things this way and throw all their servlets into one place. If this is your case, this might be a workaround for you.
Michael Arnett
Ranch Hand

Joined: Mar 22, 2001
Posts: 65
Can I utilize Enteprise Beans to accomplish this? Basically I am just trying to make one object responsible for inputting a batch of records into a database and another object to run on a schedule and periodically process some subset of database records...Thanks again for any help you can offer.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17417

No, you can't play thread games with EJB's! It's expressly forbidden by the EJB spec. You can do threads in servlets, subject to the limits of the servlet thread's lifespan, but EJBs can't do threads at all.
However, JMS is often a good solution if you want to trigger the task from a server event. Many J2EE server systems also have some sort of process that can be time-triggered.
Two examples. 1) In WebLogic, you can specify a WebLogic startup class (server-specific) task and use their timer services to do what you want. 2) In Unix/Linux using Tomcat, you can set up a "cron" task to run the process (Windows uses the "AT" command similarly).

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Can JSP spawn persistent/independent serverside objects?
It's not a secret anymore!