aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes How many ways we can obtain ServletContext? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "How many ways we can obtain ServletContext?" Watch "How many ways we can obtain ServletContext?" New topic
Author

How many ways we can obtain ServletContext?

Bahadar Khan
Ranch Hand

Joined: Jun 19, 2003
Posts: 81
In j2eeCertificate.com, I have seen this question and among those they have mentioned one of the correct answer as this:

req.getSession().getServletContext();


However I couldn't find anywhere getting ServletContext out of Session!

Please let me know if this is possible?
Mikalai Zaikin
Ranch Hand

Joined: Jun 04, 2002
Posts: 3212
    
    6
Originally posted by Bahadar Khan:
In j2eeCertificate.com, I have seen this question and among those they have mentioned one of the correct answer as this:

req.getSession().getServletContext();


However I couldn't find anywhere getting ServletContext out of Session!

Please let me know if this is possible?


Hi Bahadar !

This is absolutely valid way to obtain reference to ServletContext.
Take a look at HttpSession JavaDoc :


getServletContext

public ServletContext getServletContext()

Returns the ServletContext to which this session belongs.

Returns:
The ServletContext object for the web application
Since:
2.3


regards,
MZ


Free SCDJWS 5.0 Study Guide - SCDJWS 5.0 Quiz (How to get SCDJWS 5.0 Quiz)
Java Platform, Enterprise Edition 6 Web Services Developer Certified Expert Exam Study Guide and Quiz
Ture Hefner
Greenhorn

Joined: Nov 14, 2004
Posts: 18
There is no HttpSession.getServletContext() method.

Here are the ways I know of to get a ServletContext:

1) ServletConfig.getServletContext()
2) GenericServlet implements ServletConfig, so HttpServlets all have a getServletContext() method
3) In a Filter you have access to the FilterConfig which is set in the init(FilterConfig fc) callback. You can use FilterConfig.getServletContext().
4) In a ServletContextListener or a ServletContextAttributeListener, the event passed to the listener methods has a getServletContext() method.


Ture
Ture Hefner
Greenhorn

Joined: Nov 14, 2004
Posts: 18
Ooops, Mikalai is right. I am so tired I can't read javadoc.

HttpServlet, of course, does have a getServletContext() method. Thanks. Maybe I'll get this question right on the test now.
Bahadar Khan
Ranch Hand

Joined: Jun 19, 2003
Posts: 81
Mikalai

You might find this intresting.

http://jdocs.com/servlets/2.4/api/javax/servlet/http/HttpSession.html

and

http://java.sun.com/products/servlet/2.2/javadoc/javax/servlet/http/HttpSession.html

So I assume that getServletContext is a new addition in 2.4 Servlet API!

When you try first link, you'll find getServletContext while in latter link you wouldn't.
Mikalai Zaikin
Ranch Hand

Joined: Jun 04, 2002
Posts: 3212
    
    6
Originally posted by Bahadar Khan:

So I assume that getServletContext is a new addition in 2.4 Servlet API!



Since:
2.3


regards,
MZ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many ways we can obtain ServletContext?