I believe that is perfectly normal behavior. Init() is called whenever a servlet
instance is created, and they may be created at any time and in any number (unless
you specify a limit in your servlet engine, or you use the single threaded model
for your servlet, which I do not recommend for obvious reasons). This is why your
servlet must be written for a multi-threaded environment.

The only workaround I can think of, if you can not redesign your code, is to use
a Singleton to get the loading done, and make the Singleton synchronized.

tim.

> Hi,
> 
> i have a servlet as a front component in my webapp; the servlet has also the
> task of loading objects from a db during his inizialization. Loading the
> objects takes some time & i have encountered a situation wich i didn't
> expected & wich i'm not sure if i'm doing something wrong or it is orion.
> The situation is that if orion recive a request wich point to the servlet
> before the servlet has finished the init() method (wich was triggered from a
> previous request) it creates another instance of the servlet.
> 
> I have done a small example so that i can explain me in a more clear way :
> 
> in my web.xml file i have defined &  mapped the servlet:
>       <servlet>
>               <servlet-name>testServlet</servlet-name>
>               <servlet-class>TestServlet</servlet-class>
>       </servlet>
>       <servlet-mapping>
>               <servlet-name>testServlet</servlet-name>
>               <url-pattern>/test</url-pattern>
>       </servlet-mapping>
> 
> the code of the servlet is:
> 
> import java.io.*;
> import java.util.*;
> import javax.servlet.*;
> import javax.servlet.http.*;
> public class TestServlet extends HttpServlet {
>     public void doPost(HttpServletRequest req, HttpServletResponse res)
> throws ServletException, IOException {
>         doGet(req, res);
>     }
>     public void doGet(HttpServletRequest req, HttpServletResponse res)
> throws ServletException, IOException {
>         System.out.println("doGet()");
>     }
>     public void init() {
>         System.out.println("init() " +this);
>         try {
>             Thread.currentThread().sleep(5000);    // this is for simulating
> the time consuming task
>         } catch (Exception e) {}
>         System.out.println("done! " + this);
>     }
> }
> 
> now, if I start the server & i call the servlet 2 times (the second before
> init() finish) i can see 2 servlet instance created (see the output below)
> 
> D:\orion>java -jar d:/orion/orion.jar
> Orion/1.4.5 initialized
> init() TestServlet@704baa
> init() TestServlet@34fad5
> done! TestServlet@704baa
> doGet()
> done! TestServlet@34fad5
> doGet()
> 
> wich is not really what i want(& expected). I haven't watched the specs so
> i'm not sure if this is the correct behavior in a situation like that & i
> would like to know what u people think about it.
> If someone has also a workaround it will be very much appreciated.. ;)
> 
> I'm running orion on w2k (i tried 1.4.5 & 1.4.4, same behaviour for both).
> Marco 
> 
> 
> 
> --
> 
> Ing. Marco Isella, Software Engineer
> TINET SA, Via Violino 1, CH-6928 Manno-Lugano
> tel. +41 91 612 22 22, fax. +41 91 612 22 23
> email [EMAIL PROTECTED]
> http://www.tinet.com
> 
> 


Reply via email to