• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

purpose of init method

 
kesava chaitanya
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic