Connecting Tech Pros Worldwide Help | Site Map

Call servlet On Server Startup

 
LinkBack Thread Tools Search this Thread
  #1  
Old October 3rd, 2008, 11:26 AM
Newbie
 
Join Date: Sep 2008
Location: Chennai - India
Age: 29
Posts: 7
Default Call servlet On Server Startup

How can i call a function automatically when the server started ?
Reply
  #2  
Old October 3rd, 2008, 11:54 AM
Member
 
Join Date: Sep 2008
Posts: 93
Default

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

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.
Reply
  #4  
Old October 6th, 2008, 02:21 PM
Newbie
 
Join Date: Oct 2008
Posts: 4
Default

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 02:01 PM. Reason: Added [CODE] tags
Reply
  #5  
Old October 6th, 2008, 03:08 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,257
Default

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

Moderator.
Reply
  #6  
Old October 6th, 2008, 04:10 PM
JosAH's Avatar
Moderator
 
Join Date: Mar 2007
Location: Voorschoten, the Netherlands
Age: 52
Posts: 10,062
Default

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
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search


Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.