• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Include() Doubt

 
Nikhil Jain
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came acrss follwing question in the mock...

Will this not throw IllegalStateException...

The answer to this question is - C

Given the following code snippet, what would be the output you can expect to see on the web page? Mark

Please select one correct answer.



// Calling servlet:

public void doGet(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><BODY>");
out.println("I am calling others!");
RequestDispatcher rd= req.getRequestDispatcher("/MyServlet");
rd.include(req, res);
out.println("</BODY></HTML>");
out.close();
}

// Target servlet:

protected void doGet(HttpServletRequest req,
HttpServletResponse res) throws ServletException, IOException
{
PrintWriter out = res.getWriter();
out.println("I am called by others!");
}

A : "I am calling others!".
B : "I am called by others!".
C : Both "I am calling others!" and "I am called by others!".
D : An IllegalStateException is thrown.
E : An IOException is thrown.
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
rd.include(req,res) is similar to jsp:include (which does not need to reset existing buffer) and not jsp:forward. So why should it throw any kind of exception?.

Even if you do res.flush() and then call rd.include(req,res) even then no exception is raised.Had this been the forward() method here, then the IllegalStateException would have occurred.
[ September 03, 2006: Message edited by: Shivani Chandna ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic