• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Clarification abt init()

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic