Saturday, June 21, 2008

Diff between include directive & include action in JSP

Difference between include directive & include action in JSP

include directive - <%@ include file="fileName" %> - as is the case with all other directives in a JSP page, this include directive is also processed at the time when the JSP page is translated into its equivalent servlet. This directive simply causes the contents of the specified file to be pasted into the JSP page at the place where the page contains this directive. Irrespective of whether the included resource is a static resource or a JSP page - only the contents of the file are pasted (and not the processed results). This directive is normally used for including static resources only - like, banner, header, footer, etc. for the obvious reason.

One common issue with include directive (if it's used to include another JSP) is that the main JSP (which calls this directive to include another JSP) doesn't get re-compiled in the case when the included JSP changes but not the main JSP. Reason being, a JSP is re-compiled only if the contents of the JSP changes. Now in this case, the main JSP (after inclusion) remains the same. Once the include directive has been processed during the translation phase, the main JSP is unaware that part of its contents have come from another file. The entire translated file is treated as a single unit. Any changes to the main JSP will initiate the translation phase and then compilation phase again and hence the specified is included again. So, any modification in the included JSP will not get reflected unless the main JSP is also changed.

include action - <jsp:include page="relativeURL" /> - this include action is executed at run time and the specified 'page' is first executed and then the result of that 'page' is appended to the response object of the calling JSP at the point where the <jsp:include> tag occurs. Obviously if the specified page is a static resource then the contents of it are included as there won't be an executed result in that case. This action allows additional parameters to be passed via <jsp:param> child element of this include action element.

Remember that we can specify only a relative URL i.e., a resource which is in the same context as the calling JSP page. Since the resource is called by passing both the request and response objects, hence the called JSP can access any beans defined in the request using the <jsp:useBean> tag. But, this included resource can't set any HTTP headers. This is the reason why an included resource can't set cookies (as they are carried in the HTTP Headers). Such an attempt will throw an exception.

Difference between include directive & include action

  • The most evident difference is that include directive is processed at the translation time while the include action is processed at the run time.
  • include directive don't require a relative file path (it can be either relative or absolute) whereas the include action requires the page name to be relative to the context of the calling JSP.
  • include action always includes the contents of the specified file irrespective of whether the specified file is a static resource or a dynamic resource whereas include action executed a specified dynamic resource and only the result gets included in the response of the calling JSP. In case of static resource even the include action just includes the contents only as in that case the resource can't be executed.
  • include directive does not ask for any parameters to be passed, which is quite obvious because include directive doesn't execute the specified resource whereas we may specify additional parameters which may be used while execution of the specified resource in case of include action.
  • include directive can't access the objects created in the request scope (in fact, it doesn't need to access any again because it's not going to execute the resource) whereas the include action can access the objects created with the request scope.



Java Tutorial said...

Also check out tutorial on JSP include directive and JSP include action

Java Heap memory said...

Another problem with include directive is that you can not put any conditional code there which is based on certain parameters in request. see include directive vs include action in jsp for details.

Anonymous said...

Thank you very much.
This is very simple and valuable.

Anonymous said...

excellent explanation
thanks !!!