Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can i run a servlet on tomcat startup?

 
Pradeep Daniel
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to run a servlet code every time tomcat startup. Is it possible? Please let me know the procedure to achieve it and the configuration needed for it (ie, if any entry in the server.xml of tomcat is needed)
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By adding <load-on-startup>1</load-on-startup> in your Servlet configuration, you can load your Servlet when Servlet starts.

<servlet>
<servlet-name></servlet-name>
<servlet-class></servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if your requirement is such that you want to execute / perform a specific task when your Server loaded, then better to use ContextListener.

Check this example for better understanding of ContextListener.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic