Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in Mock exam

 
Gurukant Desai
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

One More question no.7 from javabeat's mock exam no.5.
--------------------------------------------------------------------------
7.Following is the code for doGet() method of TestServlet. Which of the given statements about it are correct?

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");
--------------------------------------------------------------------------

Answer given are C,E where as I think A,E are correct
Reason:- From mikalai's note I found that "The ServletContext.getRequestDispatcher method takes a String argument describing a path within
the scope of the ServletContext. This path must be relative to the root of the ServletContext and begin
with a '/'."

Please reply whether I am right/wrong?
 
nav katoch
Ranch Hand
Posts: 246
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right. It should be A & E. If you use without '/' (ie for ServletContext) you will get compilation error.

Thanks, Naveen.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic