• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Request Dispatcher doubt

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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()?
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic