Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is ServletConfig obj passed to init() of GenericServlet when the class implements the interface?

GenericServlet implements ServletConfig interface which means all the interface functions can be invoked from init() function of GenericServlet. Given this context, why does the Servlet container sends ServletConfig object to init() method? I would also like to know if the ServletConfig object that is passed to GenericServlet.init(ServletConfig) different from GenericServlet object.

Regards, Ravi

like image 577
Ken Russell Avatar asked Oct 14 '11 13:10

Ken Russell


People also ask

What is the purpose of GenericServlet class in Java?

GenericServlet makes writing servlets easier. It provides simple versions of the lifecycle methods init and destroy and of the methods in the ServletConfig interface. GenericServlet also implements the log method, declared in the ServletContext interface.

What does ServletConfig interface do?

Interface ServletConfig A servlet configuration object used by a servlet container to pass information to a servlet during initialization.

Is GenericServlet an interface?

GenericServlet. The GenericServlet class implements the Servlet interface and, for convenience, the ServletConfig interface.

What are the parameters which service () method takes in GenericServlet?

service() method This method is automatically called by the server whenever a request for a GenericServlet arrives. The service() method accepts two parameters: A ServletRequestObject. A SerlvetResponseObject.


1 Answers

The GenericServlet implements the ServletConfig methods by simply delegating to the config object passed to the init method. So it implements ServletConfig simply for convenience - it then simply delegates. So instead of calling getServletConfig().getInitParameter() you can call getInitParameter()

like image 77
Bozho Avatar answered Oct 20 '22 15:10

Bozho