Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2008

Tip: Looking for answers? Try searching our database.

Servlet startup completion

Thread view: 
Rob Whiteside - 07 Feb 2008 18:09 GMT
Hi,

The "init" method for a servlet provides a place for you to do things
during the Web Server's startup.  Is there a way to know when the Web
Server has completely finished starting up and is forwarding requests
to its servlets?

My situation is this:
When my servlet starts, it needs to populate the database with some
data that it grabs from another web service.  The interaction with
that web service is such that I send it a request for data, and the
web service immediately responds, then later it sends the data to my
servlet via http post/SOAP.

The problem is that when I send that request for data in the servlet's
init method, I have no guarantee that my Web Server is actually
started and receiving requests.  So, if the external web service posts
its data to me right away, I never get it.

I wish there was another method in the servlet that got called when
the web server startup was complete.

currently, my solution is to spawn a thread in my init method that
sends requests to myself over and over until I get a valid response
code.

I was hoping that I was missing a simpler solution (and one slightly
less hokey).

Currently I am using Apache Tomcat, but the solution must be generic
to all web servers

Thanks for your help!
KosciaK - 07 Feb 2008 18:39 GMT
Rob Whiteside pisze:
> Hi,
>
> The "init" method for a servlet provides a place for you to do things
> during the Web Server's startup.  Is there a way to know when the Web
> Server has completely finished starting up and is forwarding requests
> to its servlets?

Check the ServletContextListener interface

Signature

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 KosciaK     mail: kosciak1(at)gmail(dot)com
             www :   http://kosciak.blox.pl/
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Rob Whiteside - 07 Feb 2008 22:23 GMT
> Rob Whiteside pisze:
>
[quoted text clipped - 6 lines]
>
> Check the ServletContextListener interface

Exactly what I was looking for, thanks!
Lew - 08 Feb 2008 01:57 GMT
>>> The "init" method for a servlet provides a place for you to do things
>>> during the Web Server's startup.  Is there a way to know when the Web
>>> Server has completely finished starting up and is forwarding requests
>>> to its servlets?

That is not when init() runs.  The init() method runs when
> the servlet is being placed into service.
...
> The servlet container calls the init  method exactly once after instantiating the servlet.
> The init method must complete successfully before the servlet can receive any requests.
<http://java.sun.com/javaee/5/docs/api/javax/servlet/Servlet.html#init(javax.serv
let.ServletConfig
)>

The web server could have been running for years when init() is called.  For
that matter, the init() method for that servlet class might have been called
before any number of times on other instances of the servlet.

Signature

Lew

Rob Whiteside - 08 Feb 2008 17:22 GMT
> >>> The "init" method for a servlet provides a place for you to do things
> >>> during the Web Server's startup.  Is there a way to know when the Web
[quoted text clipped - 14 lines]
> --
> Lew

Thanks Lew,
you are quite right, I just tested the servletContextListener, and it
isn't what I was looking for after all.

I suppose I should have read the servlet javadoc more carefully,
because it says pretty plainly, that a servlet will not be put into
service until the init method has completed successfully.

Do you know of a way to verify that init is complete?
Rob Whiteside - 08 Feb 2008 17:29 GMT
> Do you know of a way to verify that init is complete?

Or more specifically, that the servlet has been put into service
Lew - 09 Feb 2008 00:28 GMT
>> Do you know of a way to verify that init is complete?
>
> Or more specifically, that the servlet has been put into service

I don't know what you mean by "verify".  There are two ways that I can think
of offhand - program invariant checks and log output.

Presumably you care about init() because you want it to establish some
condition for the rest of the servlet's life.  Let's say it's to make sure
that the servlet context-wide variable 'foo' has been set to a sensible value,
say, not null.  Then in your service() method or the doPost() / doGet() called
by service(), you start with a check for 'foo != null' before doing any other
work.

Of course, the service() method isn't even called unless init() completed.
This technique simply ensures that it completed correctly.

Another, complementary technique is to issue a log statement at the end of the
init() method.  This tells you afterward if init() completed, and usually
when.  The invariant-check technique ensures your program will only proceed if
init() had completed correctly in the moment.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.