aspose file tools*
The moose likes Servlets and the fly likes Calling a servlet on startup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling a servlet on startup" Watch "Calling a servlet on startup" New topic
Author

Calling a servlet on startup

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
I want to call a servlet everytime Tomcat boots up. I have made the necessary entries in the web.xml file of my web-app. But the servlet is not getting invoked.

<servlet>
<servlet-name>DaemonServlet</servlet-name>
<servlet-class>com.kalzoom.DaemonServlet</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
The class file is present in the directory.
Ken Loh
Ranch Hand

Joined: Feb 16, 2005
Posts: 190
Your servlet loads after 4 others before it. Is there any impact if there is a skip in the sequence ?

Originally posted by Kedar Dravid:
I want to call a servlet everytime Tomcat boots up. I have made the necessary entries in the web.xml file of my web-app. But the servlet is not getting invoked.

<servlet>
<servlet-name>DaemonServlet</servlet-name>
<servlet-class>com.kalzoom.DaemonServlet</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
The class file is present in the directory.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Why not use a ServletContextListener instead?


James Carman, President<br />Carman Consulting, Inc.
Mike Cronin
Greenhorn

Joined: Apr 17, 2002
Posts: 25
I don't think a skip in the sequence would cause a problem, but you will have a problem if the sequence is duplicated. Does another servlet start at sequence 5 in your web.xml?

Mike Cronin
Data On Call
[ April 24, 2005: Message edited by: Mike Cronin ]
Jason Kwok
Ranch Hand

Joined: Mar 31, 2005
Posts: 126
Originally posted by Kedar Dravid:
I want to call a servlet everytime Tomcat boots up. I have made the necessary entries in the web.xml file of my web-app. But the servlet is not getting invoked.

<servlet>
<servlet-name>DaemonServlet</servlet-name>
<servlet-class>com.kalzoom.DaemonServlet</servlet-class>
<load-on-startup>5</load-on-startup>
</servlet>
The class file is present in the directory.


If I understand the problem correctly, you want a specific servlet to run when you simply run tomcat and go to your webapp through your browser? If so, you could do this by using a servlet mapping in your web.xml file. If you are using a welcome-file-list element, then set the url-pattern in the mapping to whatever you've set your index to.

Ex.
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
I have tried using the ServletContextListener.
But now I am getting the following error:
Element type "listener" must be declared.
This is the entry in my web-app's web.xml file:
The necessary class file is in the appropriate directory.

<listener>
<listener-class>
com.kalzoom.MyServletContextListener
</listener-class>
</listener>
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
Originally posted by Kedar Dravid:
I have tried using the ServletContextListener.
But now I am getting the following error:
Element type "listener" must be declared.
This is the entry in my web-app's web.xml file:
The necessary class file is in the appropriate directory.

<listener>
<listener-class>
com.kalzoom.MyServletContextListener
</listener-class>
</listener>



What DOCTYPE are you using at the top of your web.xml file? Is it the 2.3 version or greater?
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
I am using the 2.2 version.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Listeners started with version 2.3.
What version of Tomcat are you using?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
I am using Tomcat 4.1
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Did you bring your web.xml file over from an older version?
Tomcat 4.1 supports the servlet 2.3 spec.
You may just need to update the DTD clause at the top of your deployment descriptor.
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
This is the DTD clause at the top of my web.xml:
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">

I changed it to:
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.3.dtd">

Still, its not working.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I have a copy of 4.1.24 on my machine.
I copied the top of the web.xml file that ships in the ROOT webapps.
It's a little different that what you've got.
See if this works

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a servlet on startup