Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a servlet on startup

 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use a ServletContextListener instead?
 
Mike Cronin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the 2.2 version.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Listeners started with version 2.3.
What version of Tomcat are you using?
 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Tomcat 4.1
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic