File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Want to instance a class on startup of tomcat in the JVM that will be used for JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Want to instance a class on startup of tomcat in the JVM that will be used for JSP" Watch "Want to instance a class on startup of tomcat in the JVM that will be used for JSP" New topic
Author

Want to instance a class on startup of tomcat in the JVM that will be used for JSP

Chris Huisman
Greenhorn

Joined: Feb 12, 2001
Posts: 21
I need to have certain classes loaded on startup of tomcat so that their initialization can take place before any JSP pages are loaded, on a specific JVM
Rama Chandra
Greenhorn

Joined: Feb 07, 2001
Posts: 8
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.
Chris Huisman
Greenhorn

Joined: Feb 12, 2001
Posts: 21
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.
Ajan Balakrishnan
Ranch Hand

Joined: Jan 04, 2001
Posts: 71
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

Ajan

[This message has been edited by Ajan Balakrishnan (edited February 13, 2001).]
Chris Huisman
Greenhorn

Joined: Feb 12, 2001
Posts: 21
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.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12785
    
    5
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

------------------
author of:
Chris Huisman
Greenhorn

Joined: Feb 12, 2001
Posts: 21
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!
Joe Paolangeli
Ranch Hand

Joined: Apr 05, 2000
Posts: 73
The web.xml (Deployment Descriptor) file is part of the Servlet 2.2 specification: http://java.sun.com/products/servlet/download.html#specs
The Deployment Descriptor is described in chapter 13.
You would have one web.xml file for each web application. Here is an example:

Hope this helps,
Joe
[This message has been edited by Joe Paolangeli (edited February 19, 2001).]
Ajan Balakrishnan
Ranch Hand

Joined: Jan 04, 2001
Posts: 71
Chris ,
Test.bat comes before the tomcat start.
Ajan
[This message has been edited by Ajan Balakrishnan (edited February 19, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Want to instance a class on startup of tomcat in the JVM that will be used for JSP