This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes why Servlet interface doesn't have getServletContext()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "why Servlet interface doesn Watch "why Servlet interface doesn New topic
Author

why Servlet interface doesn't have getServletContext()?

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Why javax.servlet.Servlet interface doesn't have getServletContext() method?

Why this question came in my mind because:
1] It has getServletConfig() method.
2] javax.servlet.GenericServlet class has getServletContext() method.

Thanks.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Servlet can access the ServletConfig, the ServletConfig provides access to the ServletContext. It's all to do with the intent of each interface. The Servlet interface Defines methods that all servlets must implement, the The ServletConfig interface (is) used by a servlet container to pass information to a servlet during initialization.

If you check the API description for GenericServlet it states:
This method is supplied for convenience. It gets the context from the servlet's ServletConfig object.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Thanks David for good explanation.
[ November 30, 2005: Message edited by: rathi ji ]
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
So, context is something optional, right? Can be or not.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by dema rogatkin:
So, context is something optional, right? Can be or not.


Please describe the question?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

It is not optional, but it is not the responsibility of the Servlet to return the ServletContext, instead it is the ServletConfig's responsibility (via the container) to define the servlet context, and the config is made availabel to the Servlet.

When you extend GenericServlet, it provides a getServletContext method, but it is just a convenient short cut provided by the class and not related to the behaviour defined by the interfaces.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why Servlet interface doesn't have getServletContext()?
 
Similar Threads
getting reference to the servletContext
Entity Bean
"application" and "getServeltContext"
jspInit()
getServletContext() method?