Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Relation between ServletConfig and ServletContext

 
Mohit Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers

On page 163 HFSJ, it says -

getServletConfig().getServletContext().getInitParameter()

is same as -

this.getServletContext().getInitParameter()

Which object does 'this' refers to here? If it is ServletConfig object then how come our service()/doXXX() method has an implicit reference to ServletConfig object?

Please explain.
 
vitesse wei
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"this" is refered to servlet,servlet have both getServletConfig() and getServletContext(),first one return a ServletConfig,second one return a ServletContext,ServletConfig has a method named getServletContext,which return a ServletContext,and ServletContext has a method named getInitParamter() retrive init parameter.
am i clear?
 
Pawanpreet Singh
Ranch Hand
Posts: 264
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We know that GenericServlet is extending Servlet and ServletConfig interfaces.

In javax.servlet.Servlet interface contains getServletConfig()

and

javax.servlet.ServletConfig contains getServletContext()

1.* So the first one is getServletConfig(). that is one called from GenericServlet and further we can get ServletContext on called getServletContext() on object of ServletConfig as it is having one and that is implemented in GenericServlet

2.** In second case, this is used to get ServletContext as the method is declared in ServletConfig and defined in GenericServlet.


Hope it is clear to you.

Pawan
SCJP 5.0, SCWCD 1.4
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic