This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
Hi,ranchs! I use the requestdispatcher() method to dispatch a jsp page,but I find the result between "forward" and "include" is no diffence. And I couldn't find out the result of its description in JDK's API. Thank you for you!
forward Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response. ... 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.
include Includes the content of a resource (servlet, JSP page, HTML file) in the response. In essence, this method enables programmatic server-side includes. The ServletResponse object has its path elements and parameters remain unchanged from the caller's. The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.
So... If you have a "response part" that returns nothing, then if you forward or include the next part then the results will be the same. If you have a "response part" that sends a small amount of data (typically less than 8kb), if you forward then that data will be erased, if you include the results will be contatenated (added together) If you have a "response part" that sends a large amount of data (greater than 8kb), if you forward an exception will be thrown since you are "committed" to this response, if you include then the results will be concatenated. include allows you to build pages from multiple bits, eg including common headers and footers in multiple pages from the same source file. forward allows you to process in one page then "throw off" to a separate page for presentation, where the presentation is completely separate to the processing that went before hand, like when using Model 2 (or MVC) architecture. Dave.
Joined: Dec 13, 2001
Thank you , David.
subject: What's the different between "forward" and "include"?