aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes flush() before RequestDispatcher Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "flush() before RequestDispatcher" Watch "flush() before RequestDispatcher" New topic
Author

flush() before RequestDispatcher

Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
System.out.println("Inside ContextListenerServlet--> ");
res.setContentType("text/html");
PrintWriter out= res.getWriter();
out.println("Test for context-listener<br>");
out.println("<br>");
Dog dogName= (Dog)getServletContext().getAttribute("dog");
out.println("Dog name from context----->"+dogName.getName());
out.flush(); (1)
RequestDispatcher rd= req.getRequestDispatcher("jsp/errorpage.jsp");
rd.forward(req, res);

}

1> Will this code rus successfully?
2> Will it throw some IllegalStateException ,if not why?
3> What if I use InputStream's flush() method instead of Printwriter's flush() method at(1) ?
Jan Sterk
Ranch Hand

Joined: Jun 06, 2008
Posts: 139
Anand, why don't you try it? Just copy and paste it in an editor..


SCJP 1.4 (81%)<br />SCWCD 5 (95%)
Anand Bhatt
Ranch Hand

Joined: May 30, 2007
Posts: 189
Yes Dear I tried it! The reason for putting this here to clear my doubt regarding the two methods Printwriter's flush() method and Inputstream,s method. Why its throw an Exception for the later case and work finw for the first case? I think question is clear to you now. Right?
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Originally posted by Anand Bhatt:
Yes Dear I tried it! The reason for putting this here to clear my doubt regarding the two methods Printwriter's flush() method and Inputstream,s method. Why its throw an Exception for the later case and work finw for the first case? I think question is clear to you now. Right?


No sir, still not clear to me.

my doubt regarding the two methods Printwriter's flush() method and Inputstream,s method. Why its throw an Exception for the later case and work finw for the first case?


The two class PrintWriter and IntputStream class are different , thus there flush() method also !!


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Jan Sterk
Ranch Hand

Joined: Jun 06, 2008
Posts: 139
OK then.

The rule is: if you flush the OutputStream of the response, and then do a forward (or send an error btw), you'll get an IllegalStateException. The PrintWriter is the decorated OutputStream.

So are you sure it's not exactly the other way round? That there's no ISE if you use the InputStream's flush instead of the PrintWriter's?

[edit] The ServletInputStream has no flush() method!!
[ July 01, 2008: Message edited by: Jan Sterk ]
[ July 01, 2008: Message edited by: Jan Sterk ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: flush() before RequestDispatcher