Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between <jsp:include page = ... > and <%@ include file = ... >? [duplicate]

Tags:

java

jsp

Both tags include the content from one page in another.

So what is the exact difference between these two tags?

like image 310
Prasoon Avatar asked Oct 24 '11 18:10

Prasoon


People also ask

What is the difference between JSP include page and <%@ include file >?

What is the difference between <jsp:include page = ? > and <%@ include file = ? > <%@ include file=”filename” %> is the JSP include directive. At JSP page translation time, the content of the file given in the include directive is 'pasted' as it is, in the place where the JSP include directive is used.

What is the difference between JSP and tag file?

A tag file is a source file that contains a fragment of JSP code that is reusable as a custom tag. Tag files allow you to create custom tags using JSP syntax. Just as a JSP page gets translated into a servlet class and then compiled, a tag file gets translated into a tag handler and then compiled.

What is difference between include directive and include standard action?

1) Include directive includes the file at translation time (the phase of JSP life cycle where the JSP gets converted into the equivalent servlet) whereas the include action includes the file at runtime.

What is the difference between JSP forward action tag and include action tag?

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.


1 Answers

In one reusable piece of code I use the directive <%@include file="reuse.html"%> and in the second I use the standard action <jsp:include page="reuse.html" />.

Let the code in the reusable file be:

<html> <head>     <title>reusable</title>     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body>     <img src="candle.gif" height="100" width="50"/> <br />     <p><b>As the candle burns,so do I</b></p> </body> 

After running both the JSP files you see the same output and think if there was any difference between the directive and the action tag. But if you look at the generated servlet of the two JSP files, you will see the difference.

Here is what you will see when you use the directive:

out.write("<html>\r\n"); out.write("    <head>\r\n"); out.write("        <title>reusable</title>\r\n"); out.write("        <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"); out.write("    </head>\r\n"); out.write("    <body>\r\n"); out.write("        <img src=\"candle.gif\" height=\"100\" width=\"50\"/> <br />\r\n"); out.write("        <p><b>As the candle burns,so do I</b></p>\r\n"); out.write("    </body>\r\n"); out.write("</html>\r\n");   

And this is what you will see for the used standard action in the second JSP file :

org.apache.jasper.runtime.JspRuntimeLibrary.include(request, response, "reusable.html", out, false); 

So now you know that the include directive inserts the source of reuse.html at translation time, but the action tag inserts the response of reuse.html at runtime.

If you think about it, there is an extra performance hit with every action tag (<jsp:include>). It means you can guarantee you will always have the latest content, but it increases performance cost.

like image 158
Suhail Gupta Avatar answered Sep 17 '22 17:09

Suhail Gupta