Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher not throwing IllegalStateException

 
Garlapati Ravi
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,




I am supposed to get an IllegalStateException, nothing happening so . could any one explain what could be the reason.

http://www.oreilly.com/catalog/headservletsjsp/errata/headservletsjsp.confirmed
was stating that write() and flush() are answer(HFSJ Page 209 Q1). but for none of them i am getting an exceptioin. surprisingly request forwareded to respective JSP.

Requesting to clarify.
[ May 04, 2008: Message edited by: Garla Ravi ]
 
S Sravs
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why don't you try example of HFSJ 1.4 pageno.205

when output stream of "response" object is flushed then you get illegalstateexpection


Sravanthi
 
al langley
Ranch Hand
Posts: 35
 
S Sravs
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi try this and check in stdout_20080504(todays date)

package com.rkr.sample;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
public class Sample extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException
{
try
{


System.out.println("in side doGet");
PrintWriter pw = res.getWriter();
pw.write("ss");
pw.flush();
pw.close();

RequestDispatcher disp = getServletContext().getRequestDispatcher("/SuperServlet1");
disp.forward(req,res);
}
catch (Exception e)
{
System.out.println(e);
}


}
}

you will see:
in side doGet
java.lang.IllegalStateException: Cannot forward after response has been committed

[ May 04, 2008: Message edited by: Vijaya Sravanthi ]
[ May 04, 2008: Message edited by: Vijaya Sravanthi ]
 
Maya Dolas
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
checkout the logs, i guess you will surely see the IllegalStateException there. check the localhost logs in the logs folder. I tried out the same example few days ago with no IllegalStateException. However when in checked the logs I did see it.
 
Garlapati Ravi
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks maya, it's logging in log file.

still i dont understand one thing, how come request is forwarded to the requested page, inspite of throwing exception.

another confusion, it's not thowing any exception if i comment flush() method. my question is wether it should behave same for write() method as well ?
http://www.oreilly.com/catalog/headservletsjsp/errata/headservletsjsp.confirmed
in this it was saying write() method also throws the exception, is that correct ?
[ May 05, 2008: Message edited by: Garla Ravi ]
 
Sudhindra Gs
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the same problem. No exception either in the browser or in the logs.

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter pw = response.getWriter();
pw.println("<br>In the doGet Method");
//response.flushBuffer();
//response.resetBuffer();
//response.sendRedirect("http://www.google.com");
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}

this is my code.

1. With this code, no exception and browser will show the contents of index.jsp
2. If I uncomment, flushBuffer();, the browser will show the message printed on printwriter
3. If I uncomment resetBuffer();, the browser will again show the contents of index.jsp
4. Well, I am getting IllegalStateException only if I include both redirect and forward!!!

Plzz. help :)
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sudhindra, when you post a mock exam question, please QuoteYourSources i.e. the site/book from where you got the question.

Also UseCodeTags when you post a source code and UseRealWords, try to refrain from words like "plz"

As the code stands right now, an exception might not be thrown as only one line of text is written to the response which might be buffered. If you flush the buffer before the request dispatching, you'll get an exception in the logs...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic