Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

WhizLabs Question : RequestDispatcher - Please help me out

 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Could you please help me out ?

Question :

Assuming that request is the ServletRequest object and context is the ServletContext object. Which of the following request dispatcher elements will correctly fetch the dispatcher to the "ControllerServlet" ?

The code is executed from the "RedirectServlet".

web.xml of "whiz" web application.



a) context.getRequestDispatcher("/whiz/ControllerServlet");
b) request.getRequestDispatcher("ControllerServlet");
c) request.getRequestDispatcher("/whiz/ControllerServlet");
d) context.getRequestDispatcher("/ControllerServlet");
e) context.getRequestDispatcher("ControllerServlet");
f) context.getNamedDispatcher("ControllerServlet");
g) context.getNamedDispatcher("/ControllerServlet");
h) request.getNamedDispatcher("/ControllerServlet");

I chose options D and F but according to WhizLabs the correct options are B, D and F.

Why B is correct ?
Once the request is executed from the /Redirect path, requesting such dispatcher I would get a /Redirect/ControllerServlet which is not mapped in DD, right ?

Did I forget anything ?

Oh, can <servlet-name> element come after the <servlet-class> element ?
I thought it was the opposite.
 
Kishore Balla
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edisandro Bessa:

Why B is correct ?
Once the request is executed from the /Redirect path, requesting such dispatcher I would get a /Redirect/ControllerServlet which is not mapped in DD, right ?



This will match with url-pattern /Redirect/* which was mapped to servlet named RedirectServlet


Originally posted by Edisandro Bessa:

Did I forget anything ?

Oh, can <servlet-name> element come after the <servlet-class> element ?
I thought it was the opposite.


Order was important till Servlets 2.3 and no more important in Servlets 2.4
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Kishore for your prompt reply.


Kishore wrote ...
Order was important till Servlets 2.3 and no more important in Servlets 2.4


Regarding the above information, I really didn't know about it. Thanks a lot.


Kishore wrote ...
This will match with url-pattern /Redirect/* which was mapped to servlet named RedirectServlet


Regarding the above affirmation, it's exactly where I got confusing.

The question asks : Which of the following request dispatcher elements will correctly fetch the dispatcher to the "ControllerServlet" ?

As per your previous explanations and as I would expect, choice B will map the request to RedirecServlet and NOT to ControllerServlet as the question asks.

Based on that, Am I still wrong ?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic