| Author |
flush() before RequestDispatcher
|
Anand Bhatt
Ranch Hand
Joined: May 30, 2007
Posts: 187
|
|
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: 187
|
|
|
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: 2896
|
|
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 ]
|
 |
 |
|
|
subject: flush() before RequestDispatcher
|
|
|