• Post Reply Bookmark Topic Watch Topic
  • New Topic

omitted extends HttpServlet

 
Mirko Bernardini
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have any idea?

They probably forgot the "extends HttpServlet" unintentionally. Try to imagine that "extends HttpServlet" was there
 
Joy Mukherjee
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Amruth Puppala
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Joy Mukherjee
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per the spec thats the only difference
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!