Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Clarification abt init()

 
Rishi Wright
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Rishi Wright
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic