About Question enthuware.jwpv6.2.1090 :
Posted: Sun Dec 23, 2012 8:18 am
I believe the statement is incomplete, because it does not specify the url used by the servlet.
Consider the following testing code:
The third answer is correct only in case the servlet is called with an url like "/p1/servlet". In case the url of the servlet is "/servlet", the RequestDispatcher is null and in case the url of the servlet is longer ("/p1/p2/servlet" for instance) the RequestDispatcher is not null, but there is a runtime error : FileNotFoundException at address "/p1/html/copyright.txt"
So, the servlet url must be specified in the question statement in order to keep the third answer valid.
Consider the following testing code:
Code: Select all
@WebServlet(urlPatterns = { "/servlet", "/p1/servlet", "/p1/p2/servlet", "/p1/p2/p3/servlet" })
public class TestServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
RequestDispatcher requestDispatcher = req.getRequestDispatcher("../html/copyright.txt");
if (requestDispatcher != null) {
requestDispatcher.include(req, resp);
} else {
resp.getWriter().println("RequestDispatcher is null");
}
}
}
So, the servlet url must be specified in the question statement in order to keep the third answer valid.