File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes purpose of init  method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "purpose of init  method " Watch "purpose of init  method " New topic

purpose of init method

kesava chaitanya
Ranch Hand

Joined: Aug 15, 2001
Posts: 140
What is purpose of init method in servlet
If I don�t write init () what will happen? Will it compile?
If I write static block and do whatever init method do?will it compile?
if i write static block and init method which block will run first ()?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13037
Since init() is an instance method, your static block would be executed first. The servlet container is guaranteed to call init before servicing the first request.
Why are you interested in circumventing the servlet API conventions?
Afroz Ahmed
Ranch Hand

Joined: Jan 18, 2004
Posts: 64
init() is mainly implemented by the developer to get the initialization parameters.
public void init(){
The above can not be done using static block or anything else.
Conatiner calls init(ServletConfig) method and passes the initialization parameters by wrapping to ServletConfig object....which in turn calls
init() method implemented by developer.Here you can access the config object by getServletConfig() method.
Afroz Ahmed
SCWCD...on track

The value of an idea lies in the usage of it.
I agree. Here's the link:
subject: purpose of init method
jQuery in Action, 3rd edition