• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

flush() before RequestDispatcher

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) ?
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anand, why don't you try it? Just copy and paste it in an editor..
 
Anand Bhatt
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 !!
 
Jan Sterk
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic