Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can let servlet alive as long as Tomcat

 
yangelq yu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I create a servlet, and I want it to be called when Tomcat start. This servlet should be alive as long as Tomcat alive.
Any comments will be highly appreciated.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably don't need a Servlet. What you're saying is you want a Java object to be around for the lifetime of Tomcat, right? If so you can just create a singleton in an instance in any of your Servlet.init() methods and make sure that servlet is loaded on startup.
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
yangelq yu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kyle,
You're right. But, how can I make sure that servlet is loaded on startup?? any configuration in server.xml? pls help.
--y
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13061
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like I always say - every servlet / jsp developer should download a copy of the API from java.sun.com
You will find that there is a load-on-startup tag used in the description of a jsp or servlet in the web.xml file.
You can even specify the order in which servlets are loaded.
Here is an example of the use:
<servlet>
<servlet-name>webdav</servlet-name> <servlet-class>org.apache.catalina.servlets.WebdavServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>0</param-value>
</init-param>
<init-param>
<param-name>listings</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Bill
------------------
author of:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic