Connecting Tech Pros Worldwide Forums | Help | Site Map

Call servlet On Server Startup

Newbie
 
Join Date: Sep 2008
Location: Chennai - India
Posts: 7
#1: Oct 3 '08
How can i call a function automatically when the server started ?

Member
 
Join Date: Sep 2008
Posts: 93
#2: Oct 3 '08

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 .
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#3: Oct 4 '08

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.
Newbie
 
Join Date: Oct 2008
Posts: 4
#4: Oct 6 '08

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>
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#5: Oct 6 '08

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.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Oct 6 '08

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