Difference between jsp:include and jsp:forward action- Example

JSP provides standard actions to do things without using Java inside the scriptlet. Two of such standard actions, which help JSP to interact with other server resources e.g. another JSP, Servlet or HTML files are, include and forward actions. The <jsp:forward> action enables you to forward an HTTP request to a static HTML file, a servlet, or another JSP. It has an attribute called page, which accepts the URL of another resource as shown below:

<jsp:forward page="URL" /> 

Let's say you have two JSP files, hello.jsp and header.jsp in your Java web application and if you want to forward an HTTP request from hello.jsp to header.jsp, you can add following lines into hello.jsp:

<jsp:forward page="header.jsp"></jsp:forward>

The path to the resource is relative, as request itself have knowledge of current URL.  Btw, what is the difference between include and forward action is one of the common JSP interview questions, and if you are preparing for Java or JEE developer interviews then you should be aware of many such frequently asked questions. One way to prepare well is to refer a good book like Java Programming Interviews exposed, which contains questions and answers from all important Java topics and frameworks, including Servlet and JSP.

Difference between JSP include and forward action

Worth noting is that the JSP that contains the <jsp:forward> action stops processing, clears its buffer, and forwards the request to the target resource. Note that the calling JSP should not write anything to the response prior to the <jsp:forward> action. Anything written would go away and not appear in final response which comes from the header.jsp now.  Remember, include action is different than include directive, which also includes response from another resource but at translation time, not at the request time.

You can also pass additional parameters to the target resource using the <jsp:param> tag.

<jsp:forward page="footer.jsp" > 
   <jsp:param name="copyright" value="2016" /> 
   <jsp:param name="credit" value="java" /> 

In this example, the footer.jsp can access the value of copyright and credit parameter by calling the request.getParameter("copyright").

On the other hand <jsp:include> action is used to load the content of another JSP, Servlet or HTML file into current JSP. <jsp:include> also execute the code and force a flush of the buffer in the output stream.
If a.jsp has the code like

<jsp:include page="header.jsp" flush="true" > 
  <jsp:param name="key" value="value" /> 

then the header.jsp execute and the output is placed in the a.jsp

Here is a full list of supported standard action by JSP, you can also read Head First JSP and Servlet to learn more about Java Server pages:

Difference between JSP Include and Forward Action

That's all about difference between <jsp:include> and <jsp:forward> action in JSP. In short include action is used to include contents of another Servlet, JSP or HTML files, while the forward action is used to forward the current HTTP request to another Servlet or JSP for further processing. They are exactly similar to include() and forward() method of RequestDispatcher and Servlet.

No comments:

Post a Comment