Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlets

 
Lalit Kapoor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
I just want to know the concept behind using super.init(config) in the init() method of an Http Servlet. If GenericServlet class implements the ServletConfig interface, then what is the need of using the init() method of GenericServlet class.

Thanks
lalit
 
Praful Thakare
Ranch Hand
Posts: 642
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lalit,
if you override following method in your servlet then a config object will be created by server and will be assigened to config variable defined in this method
init(ServletConfig config)
{
}


this object can be used in doget or dopost method,but as it is local to
your init method it is must to initilize config variable of generic servlet
so that it will be useful in other methods,and this is done by calling super.init(config)...

hope this helps
Cheers
Praful
 
Lalit Kapoor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Praful,
I have seen the declaration of GenericServlet class but there is no instance varible name config or something else where the reference to the ServletConfig object passed as parameter to the init() method can be saved. Also, GenericServlet class implements the ServletConfig interface so the abstract methods, such as getInitParameter() etc., declared in the ServletConfig interface are automatically available in the subclasses that extends HttpServlet class.
That's why i am a bit confused about it..

Please explain this concept in detail about how and where the reference is saved...

Lalit
 
Rishi Tandon
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want to know the concept behind using super.init(config) in the init() method of an Http Servlet.


A convenience method ( GenericServlet.init() ) which can be overridden so that there's no need to call super.init(config).
Instead of overriding init(ServletConfig), simply override this method and it will be called by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via getServletConfig().
 
Lalit Kapoor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I read on a site that:
The default implementation of the init() method stores the ServletConfig object for use by the methods in the ServletConfig interface.

How is it possible that the methods of ServletConfig interfaces will be implemented using the reference of its own object?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic