Forums Register Login

omitted extends HttpServlet

+Pie Number of slices to send: Send
Source: Sun Certified Web Component Developer Study Guide by David Bridgewater (Chapter 3)

11. What is the outcome of executing ServletA? You can assume that (1) ServletB has a mapping of �/ServletB� and a name of �ServletB,� and (2) imports have been omitted from the code for brevity; the code will compile successfully. (Choose one.)

public class ServletA extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

RequestDispatcher rd = getServletContext().getNamedDispatcher("ServletB");
rd.forward(req, resp);
}
}

public class ServletB {

public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String attr = (String)
req.getAttribute("javax.servlet.forward.servlet_path");
PrintWriter out = resp.getWriter();
out.write("Attribute value: " + attr);
}
}

A. NullPointerException thrown
B. �Attribute value: null� output to the web page
C. A blank web page
D. ServletNotFoundException thrown
E. �Attribute value: /ServletB� output to the web page
F. �Attribute value: ServletB� output to the web page
G. ClassCastException thrown


Response:

B is the correct answer. The code executes correctly. However, because the method used to obtain a RequestDispatcher in ServletA is setNamedDispatcher(), the attribute javax.servlet.forward.servlet_path is not set up in the servlet that is the target of the forward, ServletB.
A, D, and G are incorrect, for the code runs perfectly well. In ServletA, the line rd.forward() has the potential to throw a NullPointerException�but not when a valid servlet is found. The getAttribute() output cast to a String in ServletB is quite correct, hence no ClassCastException. ServletNotFoundException does not exist. C is incorrect because there
is output on the web page. E and F are incorrect�E would have been a correct version of the servlet path had the dispatcher used arisen from a getRequestDispatcher() method.


------------------------------
For me the response is C because the ServletB doen't extends HttpServlet.
Do you have any idea?
+Pie Number of slices to send: Send
 

Do you have any idea?


They probably forgot the "extends HttpServlet" unintentionally. Try to imagine that "extends HttpServlet" was there
+Pie Number of slices to send: Send
Yes the answer is B.

As per the spec:
If the forwarded servlet was obtained by using the getNamedDispatcher
method, these attributes (javax.servlet.forward.request_uri,
javax.servlet.forward.context_path,javax.servlet.forward.servlet_path,
javax.servlet.forward.path_info,javax.servlet.forward.query_string) must not be set.

Joy
+Pie Number of slices to send: Send
Hi Joy and Verri,

So basically what is the defference between
RequestDispatcher rd = getServletContext().getNamedDispatcher("ServletB"); and
RequestDispatcher rd = getServletContext().getRequestDispatcher("ServletB");

Joy - Whatever you said is the only difference or is there any difference is there?
+Pie Number of slices to send: Send
As per the spec thats the only difference
I am not a spy. Definitely. Definitely not a spy. Not me. No way. But this tiny ad ...
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 883 times.
Similar Threads
RequestDispatcher : content in the forwarding page.
Urgent
Forward giving error
findParent() method in classsic tags
RequestDispatcher forward doubt?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 08:59:30.