• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How many ways we can obtain ServletContext?

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 3904
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 Hefner
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 3904
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bahadar Khan:

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




Since:
2.3



regards,
MZ
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic