Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Forward does now give exception

 
Pratibha Malhotra
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>");
}
}
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you are using tomcat look at stderr.log you may find the stack trace of the exception there...
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic