Hi, guys, What's the difference between the responses of include() and forward()? I have two servlets. In servlet one has code: ... out.println("***");//output dispatcher.include(req,res); out.println("###");//output ... In servlet two: ... out.println("***"); //no output dispatcher.forward(req,res); out.println("###");//no output ... Question is Why servlet one printed *** .. ### but servlet two didn't print *** or ### Thanks,
SCJP2, SCWCD
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
include does not flush the buffer ..the way forward does. So thats why u see that result
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
There quite a few differences. Sure, both "forward" and "include" are just fancy ways of performing a Java method call to another servlet, but
You can add to the response after an include - after a forward, the response has been committed and closed: any attempt to write to it results in an IllegalStateException.
Along similar lines, an include is always possible - a forward is possible only if output has not been committed yet, otherwise an IllegalStateException is thrown.
An included resource cannot set response headers - a forwarded resource can.
The HttpServletRequest path information for an include reflects the original request - the information for a forward reflects the forwarded request.
- Peter
Sathya Srinivasan
Ranch Hand
Joined: Jan 29, 2002
Posts: 379
posted
0
I would like to consider include() and forward() method like this. Assume that you have a jsp called a.jsp and two servlets called AServlet and BServlet. Also, assume that a.jsp calls AServlet. Consider the first case where AServlet uses include() to call BServlet. In this case, the container first does whatever is there in AServlet before calling BServlet, then goes to BServlet and does everything there, comes back to AServlet and finishes whatever is remaining. // do AServlet before calling BServlet // do whatever is there in BServlet // come back to AServlet where it left off and finish it. // AServlet commits the response. In the second case, the last step does not happen. a.jsp calls AServlet, AServlet does something, it forwards the control to BServlet, BServlet does something, and commits the response.
Originally posted by Sathya Srinivasan: In the second case, the last step does not happen. [...]
Careful - this is easily misunderstood. The last step does happen, the only difference is that it cannot affect the response anymore as after a forward the response is closed. Before you mutter "pedantic bartender", I'm willing to bet that there are quite a few applications out there that are silently throwing exceptions because the developers thought that a forwarded request somehow doesn't return. This can easily slow the application down because throwing an exception is pretty expensive. - Peter
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
The HttpServletRequest path information for an include reflects the original request - the information for a forward reflects the forwarded request. - Peter say a.jsp is incluing b.jsp then path info is http//.../a.jsp say a.jsp is forwarding b.jsp then path info is http//.../b.jsp....is this what u meant???
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
posted
0
Yes. - Peter
subject: output of servlets with include() or forward()