aspose file tools*
The moose likes Servlets and the fly likes Clarification abt init() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Clarification abt init()" Watch "Clarification abt init()" New topic
Author

Clarification abt init()

Rishi Wright
Ranch Hand

Joined: Jun 25, 2003
Posts: 46
Hi'all,
Please let me know my concept regarding init method of servlet is correct or not....
1)If I override init() method(w/o any parameters) in myServlet
Then the servlet engine calls init(ServletConfig) of GenericServlet which in turn calls the init() method defined in myServlet
2)If I override init(ServletConfig) method in myServlet
Then the servlet engine calls init(ServletConfig) of myServlet and we call super.init(ServletConfig) to get proper servlet config object.

3) init(ServletConfig) in GenericServlet must be called in either way for proper initialization of myServlet.
Thanks in advance
rishi
yogendra singh
Greenhorn

Joined: Jan 08, 2004
Posts: 21
I also have one thing to know....
if we override the init(config) method . and dont call the super(config) ..what wud happen...will the instance of servlet will get created or not.. is this mandatory.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12805
    
    5
if we override the init(config) method . and dont call the super(config) ..what wud happen...will the instance of servlet will get created or not.. is this mandatory.

As I recall, the servlet will be created and will run but give very odd error messages.
Basavaraju Banakar
Greenhorn

Joined: Jul 18, 2002
Posts: 25
Originally posted by yogendra singh:
I also have one thing to know....
if we override the init(config) method . and dont call the super(config) ..what wud happen...will the instance of servlet will get created or not.. is this mandatory.

When init(ServletConfig config) method of GenericServlet is overridden, it is required to call super.init(config).. but u can avoid this by overriding init() method of GenericServlet.
If u override the init(config) method and don't call the super(config) then there could be problems in using the ServletConfig object...


It is impossible to make anything foolproof because fools are so ingenious..Murphy<br />Basu.
Rishi Wright
Ranch Hand

Joined: Jun 25, 2003
Posts: 46
Thanks for the reply BUT Exactly what I wanted to know is
how the servlet engine works and which init method it calls in different cases
like 1)When I override init()
2)when I override init(config)
3)and when I dont override any init?
when servlet engine loads the servlet for the first time, will it call GenricServlet init method or will it look for overridien init method in myServlet?
Thank you
rishi
Moorthi Ramasamy
Greenhorn

Joined: Jan 02, 2004
Posts: 15
Originally posted by yogendra singh:
I also have one thing to know....
if we override the init(config) method . and dont call the super(config) ..what wud happen...will the instance of servlet will get created or not.. is this mandatory.

You shouldn't see any drastic result unless you call getInitParameter() method outside the init(ServletConfig).

a) init(ServletConfig config) {
super.init(config)
String initParam = config.getInitParameter();//should be able to get the init param set in web.xml
}
service(ServletRequest req,ServletResponse res){
String initParam = getInitParameter()();//should be able to get the init param set in web.xml
}

b) init(ServletConfig config) {
String initParam = config.getInitParameter();//should be able to get the init param set in web.xml
}
service(ServletRequest req,ServletResponse res){
String initParam = getInitParameter()();//get Exception here.Coz the getInitParameter method of GenericServlet is being called without setting the config info to it.
}
The reason for calling super.init(config) is pass on the servlet config information to the generic servlet and hold the reference. This information can be retrieved from methods other than init() later on.
Thanks,
Moorthi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Clarification abt init()