jQuery in Action, 2nd edition*
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
Author

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 ()?
bye
chaitanya
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
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?
Bill
Afroz Ahmed
Ranch Hand

Joined: Jan 18, 2004
Posts: 64
Hello,
init() is mainly implemented by the developer to get the initialization parameters.
public void init(){
getServletConfig().getInitParameter("parameter1");
}
.......web.xml......
<servlet>
<servlet-name>.......
<init-param>
<param-name>parameter1</param-name>
<param-value>first</param-value>
</init-param>
</servlet>
.......................
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.
Regards
Afroz Ahmed
SCJP(1.4)
SCWCD...on track


The value of an idea lies in the usage of it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: purpose of init method
 
Similar Threads
overwrite the init() method in servlet?
Static Init Block Problem
initialization sequence - what is done first?
How to deal with configuration data in a (web) application?
Global constant in a static block