Forums Register Login

How many ways we can obtain ServletContext?

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

Originally posted by Bahadar Khan:

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




Since:
2.3



regards,
MZ
Everyone is a villain in someone else's story. Especially this devious tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 21556 times.
Similar Threads
What is the name of the object that is created/maintained by the Servlet Container and contains all
Determining the location of the file
Absolute path of a webapp
How do you set a servlet context attribute?
how to access the servlet's context in a different file?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:09:06.