Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

What is happening here inside getServletContext() of javax.servlet.GenericServlet class?

 
Eshan Kapoor
Ranch Hand
Posts: 39
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following overridden method is a snippet of javax.servlet.GenericServlet class from URL:- http://grepcode.com/file/repo1.maven.org/maven2/javax.servlet/servlet-api/2.5/javax/servlet/GenericServlet.java?av=f



I know that ServletContext object is contained inside the ServletConfig object. What I couldn't understand is if ServletConfig really has a reference to ServletContext, why the ServletContext object is not returned and rather getServletContext() is again called on config object here? Also, how the object will be returned to our class from where we call getServletContext(), if this is recursion? Please help.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do not correctly use the init method, it is possible for the getServletConfig() method to return null.

The check for null allows for an informative Exception message.

Bill
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65542
110
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is not recursion.
 
Ishan Pandya
Ranch Hand
Posts: 228
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eshan Kapoor wrote:
I know that ServletContext object is contained inside the ServletConfig object.


I really dont think so and have never read this thing anywhere (Please correct me if I am wrong). It is like a ServletConfig has a reference to a ServletContext as you mentioned below.



Can't comment on the code that you referenced but you can direct get a ServletContext Object without going through a ServletConfig(Provided that your Servlet class extends either a GenericServlet or an HttpServlet)



It is not recursion as said above, getServletContext() is a method in GenericServlet class (More specifically an Abstract Class). So when you extend HttpServlet or GenericServlet you can use this method directly without going to ServletConfig object.

Going through ServletConfig and getting ServletContext is legal but that is mostly used if you are in a class that does not extends GenericServlet or an HttpServlet.

Hope it helps.

P.S. I understand that some lines are recursively used.
 
Eshan Kapoor
Ranch Hand
Posts: 39
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone. But can someone please tell why is this not recursion?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eshan Kapoor wrote:Thanks everyone. But can someone please tell why is this not recursion?

Because the method is in a servlet and is providing access to the Servlet context by looking it up from the servlet config. It's not calling the same method. It is juts a form of delegation.
A similar example would be, suppose a Cricketer has Statistics and the Statistics are modelled using a class that contains totalRunsScored and other statistics.
One could add a method called getTotalRunsScored in the Cricketer class and there they can return statistics.getTotalRunsScored().
The Cricketer class is declaring a method with a similar name to another class and delegating the call to an object of that class.
In your code the Servlet is just delegating the getServletContext call to its ServletConfig.



 
Eshan Kapoor
Ranch Hand
Posts: 39
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks E Armitage. I also created a similar program and got the concept. Besides this, where can we see the actual code where ServletConfig returns a reference to ServletContext?
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ServletContext is created by your container which then creates a ServletConfig object and places the servlet context onto that. The getServletContext on the servlet config then just returns that precreated config. The fact that Servlets implement the ServletConfig interface is a convenient (adapter) for you to call methods on the config from within the servlet without needing to get the ServletConfig yourself.
The (abstract) GenericServlet class is part of the servlet api so you can find it in source code of the servlet api.
 
Eshan Kapoor
Ranch Hand
Posts: 39
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
She still doesn't approve of my superhero lifestyle. Or this shameless plug:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!