Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Why does GenericServlet implements ServletConfig?

 
Ranch Hand
Posts: 355
Redhat Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've beginner level of Servlet/JSP knowledge.

Is it right to say, a servlet that we write IS-A ServletConfig? Further, why a servlet has to implement ServletConfig interface? It gets an object of ServletConfig through its init method. Which means, a servlet HAS-A ServletConfig. In spite of this, why it implements ServletConfig interface?
 
Ranch Hand
Posts: 256
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faisal,

why a servlet has to implement ServletConfig interface?



Servlet Please go through the Servlet interface. I dont think there is any hard coded rule saying that a servlet has to implement ServletConfig. Moreover the servletConfig object is made by the container and passed to the init(servletConfig) in your servlet.
 
Faisal Ahmad
Ranch Hand
Posts: 355
Redhat Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet interface doesn't implement ServletConfig. But, why does GenericServlet? Already it HAS-A ServletConfig! In spite of this, what is the purpose of implementing a ServletConfig? I find it difficult to agree with this sentence: "A servlet IS-A ServletConfig". It's confusing.
 
Faisal Ahmad
Ranch Hand
Posts: 355
Redhat Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moderators!

I want to rename the title of this thread to "Why does GenericServlet implements ServletConfig?"

Could you please help? Thanks!
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even I am preparing for SCWCD and am curious to know. If a servlet IS-A servletConfig why does the container has to pass a servletConfig to servlet:init(ServletConfig)?
(this is altogether a different question from the original)
Thanks for your thoughts
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to differentiate between Servlet and GenericServlet. Servlet (which is an interface) does not implement ServletConfig, whereas GenericServlet (an actual class) does. It's perfectly possible (although very uncommon) for a servlet to implement Servlet instead of extending GenericServlet or HttpServlet.

As a bit of a history lesson (for those who have not been using the Servlet API for a dozen years or so...), GenericServlet originally did not implement ServletConfig; that was something that got added later as a convenience. So the ServletConfig object passed in via the init method was the one you had to use.
 
Milan Sutaria
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf
 
a wee bit from the empire
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic