This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Chris Huisman
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic