• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What happens when both RequestDespatcher as well as HttpServletResponse are used

 
Moha Shaf
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried out a modified code sample of HeadFirst Servlets and JSP Chapter three Beer Application. Below is the code snippet for the doPost method :

public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException

{
String c = request.getParameter("color");
BeerExpert be = new BeerExpert();
List result = be.getBrands(c);

response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println(" Beer Selection Advice
");
out.println("
try : New Tummy Mummy Beer");

request.setAttribute("styles", result);

RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request,response);

}


This code prints out the content of whats in the result.jsp and does not render the content that has been written to PrintWriter object got from the HttpServletResponse. Can someone explain this behavior?
 
Abdul Rahman
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Moha,

As per the j2ee spec:
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.


Hope that explains why you are unable to view your out.println statements. It gets cleared before the result.jsp is called.
 
Moha Shaf
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic