Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old October 3rd, 2008, 12:26 PM
Newbie
 
Join Date: Sep 2008
Location: Chennai - India
Age: 28
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, 12:54 PM
Member
 
Join Date: Sep 2008
Posts: 87
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, 03:32 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,752
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, 03: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 03:01 PM. Reason: Added [CODE] tags
Reply
  #5  
Old October 6th, 2008, 04:08 PM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 12,752
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, 05:10 PM
Moderator
 
Join Date: Mar 2007
Location: Voorschoten, the Netherlands
Age: 52
Posts: 8,480
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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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 network members.
Post your question now . . .
It's fast and it's free

Popular Articles