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

doubt on RequestDispatcher

 
koteswararao arvapalli
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i have seen problem in question bank that is:
public void doGet(HttpServletRequest req, HttpServletResponse res)

{

try

{

RequestDispatcher rd = this.getServletContext().getRequestDispatcher("Login.jsp"); // 1

rd.forward(req, res); // 2

}

catch(Exception e)

{

e.printStackTrace();

}

}

Select 2 correct options.
A.This will not compile.
B.This will compile but will not work as expected.
C.This code will work just fine.
D.It will compile but not work properly if //1 is replaced with: RequestDispatcher rd = req.getRequestDispatcher("Login.jsp");
E.It will compile and will work properly if //1 is replaced with: RequestDispatcher rd = req.getRequestDispatcher("Login.jsp");


i think answer are B,E.

But answers are C,E.

is it correct? can you give me some explanation upon it?

Regards,
koti
 
Dan Walin
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked at the question and before I saw the answers, I assumed the correct choices were C and E. My reason would be that I don't see any reason why it would on work and would also work if the RequestDispatcher was created from the request object. I know there is a difference between the two ways that affects the path that can be used (I forget this and need to re-read up on it) but I don't think we have any information in the question that would make us think there would be a path problem.
 
koteswararao arvapalli
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dan,
i am sure that "the path string passed to ServeletContext.getRequestDispatcher() must start with '/' "
what do you say?

Regards,
koti.
 
Shruthi Karthick
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
option c is incorrect. This code wont work as such.
If we get RequestDispatcher from ServletContext we cannot specify relative path in argument of getRequestDispatcher().
The path should start with forward slash.
So options B and E are correct.
 
Dan Walin
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks - I need to restudy that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic