go to  ForumEasy.com   
JavaPro  
 
 
   Home  |  MyForum  |  FAQ  |  Archive    You are not logged in. [Login] or [Register]  
Forum Home » Java Server Pages (JSP) » RequestDispatcher.include vs. RequestDispatcher.forward
Email To Friend  |   Set Alert To This Topic Rewarding Points Availabe: 0 (What's this) New Topic  |   Post Reply
Author Topic: RequestDispatcher.include vs. RequestDispatcher.forward
WebSpider
member
offline   
 
posts: 147
joined: 06/29/2006
from: Seattle, WA
  posted on: 09/17/2012 02:39:34 PM    Edit  |   Quote  |   Report 
RequestDispatcher.include vs. RequestDispatcher.forward
    if(failed){

        request.setAttribute("request_attr", "request_attr_value");
        session.setAttribute("session_attr", "session_attr_value");
        request.getRequestDispatcher("service_B.jsp").include(request, response); 
        ...
        request.getRequestDispatcher("service_C.jsp").include(request, response); 

    }else{
        ...
    }


or

    if(failed){

        request.setAttribute("request_attr", "request_attr_value");
        session.setAttribute("session_attr", "session_attr_value");
        request.getRequestDispatcher("service_B.jsp").forward(request, response); 

        return;

    }else{
        ...
    }



The key difference between the two is the fact that the include method leaves the output stream open, whereas the forward method will close the output stream after it has been invoked.

With include, you can continue processing any stuff which can be put into output stream or even inject another jsp page (service_C.jsp); With forward, however, you had better stop (return;) unless the remaining task is just background stuff, otherwise, IllegalStateException would be thrown.

 Profile | Reply Points Earned: 0
WebSpider
member
offline   
 
posts: 147
joined: 06/29/2006
from: Seattle, WA
  posted on: 09/17/2012 02:51:07 PM    Edit  |   Quote  |   Report 
jsp:include vs. jsp:forward
<jsp:include page="servcie_B.jsp" > 
<jsp param name="name1" value="value1" /> 
<jsp pram name="name2" value="value2" /> 
</jsp:include> 


or

<jsp:forward page="service_B.jsp" flush="true" > 
<jsp param name="name1" value="value1" /> 
<jsp pram name="name2" value="value2" /> 
</jsp:forward> 


Those are counterparts of RequestDispatcher.include vs. RequestDispatcher.forward and therefore have the same implications.

 Profile | Reply Points Earned: 0

 
Powered by ForumEasy © 2003-2005, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.