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
Saturday, June 21, 2008
Diff between include directive & include action in JSP
Subscribe to:
Post Comments (Atom)
4 comments:
Also check out tutorial on JSP include directive and JSP include action
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.
Thank you very much.
This is very simple and valuable.
----
ahmed
excellent explanation
thanks !!!
Post a Comment