| Author |
Forward does now give exception
|
Pratibha Malhotra
Ranch Hand
Joined: Dec 21, 2003
Posts: 199
|
|
Hi Servlet Specification says forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward. I have tried to write to output stream after forwarding my request to a jsp. But i am not getting exception. I hope i have perceived what specification says correctly.. public class TestServlet extends HttpServlet {public void service(HttpServletRequest objRequest,HttpServletResponse objResponse)throws ServletException,IOException {PrintWriter pw = objResponse.getWriter(); pw.println("<html>"); pw.println("<head>"); pw.println("</head>"); pw.println("<body>"); pw.println("<h3>hi</h3>"); objRequest.setAttribute("Name",new String("Pratibha")); String name = (String)objRequest.getAttribute("Name"); pw.println(name); String strNextScreen = "/test.jsp"; getServletConfig().getServletContext().getRequestDispatcher(strNextScreen).forward(objRequest, objResponse); pw.println(" --"); pw.println("</body>"); pw.println("</html>"); } }
|
~ Pratibha Malhotra<br /> <br />Sun Certified Java Programmer<br />SCEA 1.4 (In Progress)<br />~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />"Many of life's failures are people who did not realize how close they were to success when they gave up!!"
|
 |
Leandro Oliveira
Ranch Hand
Joined: Nov 07, 2002
Posts: 298
|
|
try flushing your response before forwarding... response.flushBuffer() notice that the method forward will throw IllegalStateException that may not be printed to the users screen..
|
 |
Pratibha Malhotra
Ranch Hand
Joined: Dec 21, 2003
Posts: 199
|
|
In continuation to my above post I tried to flush my buffer before forwarding my request to a jsp.. Quite strange but am still not getting Illegal State Exception. {PrintWriter pw = objResponse.getWriter(); pw.println("<html>"); pw.println("<head>"); pw.println("</head>"); pw.println("<body>"); pw.println("<h3>hi</h3>"); objRequest.setAttribute("Name",new String("Pratibha")); String name = (String)objRequest.getAttribute("Name"); pw.println(name); objResponse.flushBuffer(); String strNextScreen = "/include.jsp"; getServletConfig().getServletContext().getRequestDispatcher(strNextScreen).forward(objRequest, objResponse); pw.println(" --"); pw.println("</body>"); pw.println("</html>"); } also what does commit means in follwing statements
forward should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws an IllegalStateException. Uncommitted output in the response buffer is automatically cleared before the forward.
|
 |
Kaustubh Patil
Ranch Hand
Joined: Aug 13, 2001
Posts: 164
|
|
Commited means when it is actually written to the output stream. When using the buffer , the output may be buffered for sometime before it is written. when when you writer.out() may not mean that it actually is written to the outputstream immidiately. -Kaustubh.
|
Kaustubh. Mumbai, India.
|
 |
Leandro Oliveira
Ranch Hand
Joined: Nov 07, 2002
Posts: 298
|
|
|
if you are using tomcat look at stderr.log you may find the stack trace of the exception there...
|
 |
Leandro Oliveira
Ranch Hand
Joined: Nov 07, 2002
Posts: 298
|
|
|
Hey, it's written to localhost_log.(date).txt in tomcat 5.x!!! try looking there, you are going to see the stack trace for IllegalStateException
|
 |
Mouli Vidiyala
Greenhorn
Joined: Mar 25, 2004
Posts: 2
|
|
Hi Gunjan, As per the specification : If you are forwarding a request to another resource, you can't add anything to the response after calling forward method on RequestDispatcher object. But this is not true incase of including contents from another resource. You can still add conents to the response after calling include method on RequestDispatcher object. Eg : When using forward : PrintWriter pw = objResponse.getWriter(); pw.println("Hello !!!"); getServletConfig().getServletContext().getRequestDispatcher(strNextScreen).forward(objRequest, objResponse); # You can not write anything to print writer after calling forward When using include : PrintWriter pw = objResponse.getWriter(); pw.println("Hello !!!"); pw.println("Before Including contents); getServletConfig().getServletContext().getRequestDispatcher(strNextScreen).forward(objRequest, objResponse); pw.println("After Including contents); Choose appropriate method and try again. Let me know, if you have any questions. -Mouli.
|
 |
 |
|
|
subject: Forward does now give exception
|
|
|