• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Forward does now give exception

 
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>");
}
}
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic