aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Request Dispatcher doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Request Dispatcher doubt" Watch "Request Dispatcher doubt" New topic
Author

Request Dispatcher doubt

Saurabh Chaubey
Ranch Hand

Joined: Oct 16, 2005
Posts: 101
Please answer the following question:

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.


What would be the correct answer
Steven Colley
Ranch Hand

Joined: Feb 18, 2005
Posts: 290
HI Saurabh ,

I guess it would be the option "C" (both).

but, what if we had done this:

// Target servlet:
protected void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
PrintWriter out = res.getWriter();
out.println("I am called by others!");
out.close(); <<------ this (OR out.flush() as well)
}

what would be the correct answer?


SCJP | SCWCD | SCBCD | SCWSD 5 | SCEA (I) 1.4 | SCEA 5 | IBM SOA 669
Richard Rex
Ranch Hand

Joined: Sep 19, 2005
Posts: 100
Hi Felipe,

I tried running the ones with out.close() for the Target Servlet and it did have the same output.

But I have one more question about the difference between out.close() and out.flush().

I modified the calling servlet by inserting some text for out.println() to see if it will go back and proceed...

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("/TargetServlet");
rd.include(req, res);
out.println("I'm back!");
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!");
out.close();
//out.flush();
}
---------------------------------------------------------------------------

Using out.close() in the Target Servlet causes this program NOT to display the inserted text "I'm back!" from the Calling Servlet, while out.flush() displays the inserted text.

Can anyone explain what's the difference between close() and flush()?


"If you fail to plan, you plan to fail."<br /> <br />Chad<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS (next)
Rodrigo Alvarez
Ranch Hand

Joined: Apr 10, 2006
Posts: 75
Hi all,

(sorry for the long post)

I've not tried to compile this code, but I think that the HttpServletRequest objects in the originating and "included" HttpServlet ARE BOTH A REFERENCE TO THE SAME JAVA OBJECT

=> res.getWriter() returns the same PrintWriter in both HttpServletRequest

=> if you close() this writer in one servlet, you won't be able to to add anything more in the originating servlet.


close() vs flush():
--------------------

API spec (http://java.sun.com/j2se/1.5.0/docs/api/) says:

close() : Closes this output stream and releases any system resources associated with this stream.

=> content is not yet sent to the client (this will be done automatically later), but you can't append anything anymore

flush() : Flushes this output stream and forces any buffered output bytes to be written out.

=> content is sent immediately to the client (does anybody knowns if it is then closed?) and any attempt to forward/include it will now throw an IllegalState Exception. I guess a subsequent write() would not generate an error, but it would not send any data to the client.

Hope this helps


It is a mistake to think you can solve any major problems just with potatoes.<br />--Douglas Adams
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Request Dispatcher doubt
 
Similar Threads
Mock exam question
RequestDispatcher.forward()
Include() Doubt
Problems with getRemoteUser()
Requested Dispatcher