• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

What happens when both RequestDespatcher as well as HttpServletResponse are used

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the explanation.
 
It's just a flesh wound! Or a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic