This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

flush() before RequestDispatcher

 
Anand Bhatt
Ranch Hand
Posts: 189
  • 0
  • Mark post as helpful
  • send pies
  • 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) ?
 
Jan Sterk
Ranch Hand
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Sagar Rohankar
Ranch Hand
Posts: 2905
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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: 139
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic