I am doing the mock exam for the jiris.com and confused with one of their question which is shown below.
The ServletContext object are accessible from which of the following objects?
Please select three correct answers.
A : HttpServlet B : GenericServlet C : HttpSession D : ServletConfig E : ServletResponse
My Answer: A,B,D.
But the engine says its A,C,D. But when I read the explanation (shown below in bold), it seems that it's really the GenericServlet who has the access to ServletContext object and not HttpSession. Is this an error for the engine? or am I missing something here?
The ServletConfig interface has a method called getServletContext() that returns a handle to the ServletContext object.
Since class GenericServlet implements ServletConfig, it can also access ServletContext through getServletContext(), this is also apply to HttpServlet, which is a subclass of GenericServlet class.
public ServletContext getServletContext() Returns a reference to the ServletContext in which the caller is executing.
Returns: a ServletContext object, used by the caller to interact with its servlet container
"If you fail to plan, you plan to fail."<br /> <br />Chad<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS (next)
C: ServletConfig interface has access to ServletContext from getServletContext() B: GenericServlet implements ServletConfig, so you know that its right A: HttpServlet extends GenericServlet, again correct answer D: HttpSession interface also provides method getServletContext(), which returns the ServletContext to which that session belongs.
Joined: Sep 19, 2005
Hi Kiran, Gunaselan, and Rajkishore,
Thanks for your ideas guys.
I've searched for the HttpSession API and found out that it has a method named getSessionContext() that returns the context in which the session is bound. But this method is already deprecated. So I think the best choice are HttpServlet, GenericServlet, and ServletConfig if the question asks for 3 answers. And just add the HttpSession if the question requires 4 answers. I just hope this won't be the same case in the real exam.
Thanks again to you guys!
Joined: Sep 03, 2005
Hi, getSessionContext() is deprecated. Not getServletContext(). Correct me if I am wrong.
Raj you're right, he got confused I guess only getSessionContext is deprecated, getServletContext is not
I think the question should have had 4 options as answers, nothing to worry about Rex, as long as u have the specs to back you up, there's no need to worry about the answers in the mock engines, the real exam will definitely be more consistent.