A Question on servlet initialization


Web tier: servlets, JSP, Web frameworks: A Question on servlet initialization

  1. A Question on servlet initialization (4 messages)

    If I were to write initialization code inside a static initializer block of a servlet, would it have the same effect as writing the same code inside the init method of the servlet?.

    Threaded Messages (4)

  2. The code in static initializer block will execute first than the code in init() method of the servlet.
  3. What I mean is both the effects would be the same, why do we need an init method if we can achieve the same by inserting the code in the static initializer?.
  4. The init() method is passed the ServletContext which gives you access to any initialisation parameters set for the servlet in the web.xml
  5. Exactly[ Go to top ]

    The init method gives you access to variables defined outside the code (in the web.xml) so you can build a customizable application that multiple customers could use, they simply change the web.xml to get their particulars loaded (i.e. different database drivers, etc)