You can use useBean tag in JSP to instantiate a class. If you give scope parameter of tag as application it will be instantiated at first call to JSP containg the tag and it will remain in the memory till the sever shut down, so it can be referenced in other JSPs having the same tag and same id parameter in the tag.
Joined: Feb 12, 2001
I would like to have this class start before any JSPs are started. I want it to run when tomcat starts, not when a jsp is instanced.
Chris, Did you check your original post. I think there was a solution in that post(http://www.javaranch.com/ubb/Forum7/HTML/002128.html) You can have your class loaded into your JVM by calling your runtime program(java) inside your bat file and call that bat file from the Tomcat startup. For example The following will be your bat file , i'm calling Test Test.bat -------- java yourClass
Add the following line to your Tomcat startup.bat under the :start function ------------------------------------------------- call "Test.bat" Follow the above post as well Let us know if this helps
[This message has been edited by Ajan Balakrishnan (edited February 13, 2001).]
Joined: Feb 12, 2001
Hey guys, Thanks for your replies, but I am still unsure of whether or not this is what I need, so I will tell you what I am trying to do. I have a singleton class that I want all of my servlets to be able to acces. I want to create an instance of this object at tomcat startup, so it is available whenever a JSP call is made. It also needs application scope. Does your previous reply address these issues? And if so is the call to Test.bat come before or after: call %TOMCAT_HOME%\bin\tomcat start %1 %2 %3 %4 %5 %6 %7 %8 %9 ? Thanks. Chris.
Just create a servlet that uses the singleton and specify that it is a load on startup in your web.xml. This is an example I found in tomcat examples - I dunno why they used a negative number, I think any number works. <load-on-startup>-2147483646</load-on-startup> It is handy to make this servlet a sort of debugging access point protected by a password. Bill
Thanks Bill, But do you know of any good resources that have examples of how to use load-on-startup? I am trying to figure out how to configure a web.xml file for my project. I know tomcat has an web.xml file, a question I have is, would/could/should each JSP project have it's own web.xml file? Thanks!