Connecting Tech Pros Worldwide Help | Site Map

Call servlet On Server Startup

  #1  
Old October 3rd, 2008, 12:26 PM
Newbie
 
Join Date: Sep 2008
Location: Chennai - India
Posts: 7
How can i call a function automatically when the server started ?
  #2  
Old October 3rd, 2008, 12:54 PM
Member
 
Join Date: Sep 2008
Posts: 93

re: Call servlet On Server Startup


From where you want to call the function or which event do you want to perform?
Ask a question which is clear to understood .
  #3  
Old October 4th, 2008, 03:32 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,526
Provided Answers: 12

re: Call servlet On Server Startup


I don't think this is a JavaScript question unless you want to call this servlet from the client-side (in which case, you would use Ajax).

What server software is it? Even Java/JSP wouldn't be enough. You will need some server setting (I think Tomcat has this ability) to trigger this, or you could poll the server using a page on another server.
  #4  
Old October 6th, 2008, 03:21 PM
Newbie
 
Join Date: Oct 2008
Posts: 4

re: Call servlet On Server Startup


u can implement the ServletContextListener interface in a java class
to access 2 methods namely,
Expand|Select|Wrap|Line Numbers
  1. public void contextInitialized(ServletContextEvent e)
  2. {
  3. //your code here;
  4. }
  5.  
and
Expand|Select|Wrap|Line Numbers
  1. public void contextDestroyed(ServletContextEvent e)
  2. {
  3. //cleaning up;
  4. }
  5.  
from the parameter e you can access the context and write whatever code u wish to execute within the first of the two methods i mentioned.

and one more thing
add this code to your web.xml config file

<listener>
<listener-class>mypackage.myclass</listener-class>
</listener>

Last edited by Nepomuk; October 7th, 2008 at 03:01 PM. Reason: Added [CODE] tags
  #5  
Old October 6th, 2008, 04:08 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,526
Provided Answers: 12

re: Call servlet On Server Startup


Moved to the Java forum. If someone feels it doesn't belong here, please move to a more appropriate forum, thanks.

Moderator.
  #6  
Old October 6th, 2008, 05:10 PM
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,634
Provided Answers: 2

re: Call servlet On Server Startup


Quote:
Originally Posted by senthilkumarb
How can i call a function automatically when the server started ?
A Servlet is bound to a certain application (or more of them) but a server has
to deal with all of them. There is no reason for a server to call a method from a
particular Servlet. Check your server documentation if it can handle callbacks
when it starts up.

kind regards,

Jos
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
Request for input : What makes most sense in *your* PHP environment? Bjarne answers 2 January 18th, 2007 02:25 PM
Tomcat > stdout kimbuba answers 2 July 17th, 2005 11:06 PM