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?
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?