This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Why GenericServlet implements ServletConfig? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Why GenericServlet implements ServletConfig?" Watch "Why GenericServlet implements ServletConfig?" New topic
Author

Why GenericServlet implements ServletConfig?

Faisal Ahmad
Ranch Hand

Joined: Aug 31, 2006
Posts: 346

I didn't understand the exact reason why GenericServlet implements ServletConfig. A servlet will receive its configuration object through init(ServletConfig) which is called by the container itself. This object will stay with the servlet till it dies. So, what's the point in implementing ServletConfig? Doesn't this look odd: a servlet is a ServletConfig ?
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

every servlet can have a configuration object. when we say class Ball implements Bounceable, we can think this as that a ball has the property of bouncing. it can bounce. extending the analogy to GenericServlet implements ServletConfig we can say that a generic servlet has a configuration stuff associated with it, which is very much reasonable.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
ServletConfig is an Interface, can't be instantiated.
GenericServlet and HttpServlet implements ServletConfig, So they have to give implementation to the abstract methods of it.

init(ServletConfig) and init() methods are two initialization methods.

If you think that with init(ServletConfig) method; Servlet get the Object of ServletConfig through Servlet Container, then Have you think how the Container create that Object? (Maybe ServletConfig sc = new GenericServlet(); OR new HttpServlet();)

Also, think about if you can initialize servlet with init(), then Servlet can also able to get ServletConfig object through getServletConfig() method (How it will get that Object?).
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Gaurangkumar Khalasi wrote:ServletConfig is an Interface, can't be instantiated.
GenericServlet and HttpServlet implements ServletConfig, So they have to give implementation to the abstract methods of it.

init(ServletConfig) and init() methods are two initialization methods.

If you think that with init(ServletConfig) method; Servlet get the Object of ServletConfig through Servlet Container, then Have you think how the Container create that Object? (Maybe ServletConfig sc = new GenericServlet(); OR new HttpServlet();)

Also, think about if you can initialize servlet with init(), then Servlet can also able to get ServletConfig object through getServletConfig() method (How it will get that Object?).


you cannod do ServletConfig sc = new GenericServlet() as well as new HttpServlet(); since both GenericServlet and HttpServlet are abstract classes.
Gaurangkumar Khalasi
Ranch Hand

Joined: Jun 02, 2012
Posts: 186
gurpeet singh wrote:you cannod do ServletConfig sc = new GenericServlet() as well as new HttpServlet(); since both GenericServlet and HttpServlet are abstract classes.

Maybe with anonymous inner class definition of those abstract classes!!...
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

Gaurangkumar Khalasi wrote:
gurpeet singh wrote:you cannod do ServletConfig sc = new GenericServlet() as well as new HttpServlet(); since both GenericServlet and HttpServlet are abstract classes.

Maybe with anonymous inner class definition of those abstract classes!!...


for that correct syntax would have been ServletConfig sc = new GenericServlet(){ }; and not ServletConfig sc = new GenericServlet();

but i think we are deviating from the original topic , which is as to WHY generic servlet implements ServletConfig. the clever answer would be thats how the spec implementors thought of GenericServlet class which abstracts Servlet Functionality
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Why GenericServlet implements ServletConfig?
 
Similar Threads
about GenericServlet
why sun people declared config variable as transient in generic class?
Why is ServletConfig obj passed to init() of GenericServlet when the class implements the interface?
init method of servlets
Why HttpServlet also implements Serializable?