In this tutorial you will learn about the JSP include Action Tag and its application with practical example.
JSP include Action Tag
This tag is used to include some external resources at runtime response. It may be JSP page, html file, or servlet. This tag includes resources at request time so it’s better for dynamic pages. It can be used to include both static or dynamic pages.
Now you will think that what is difference between JSP include directive and JSP include Action tag. Let ‘s see how they both are differ.
- JSP include directive include resources during translation phase but JSP include Action tag include resources during request phase.
- JSP include directive is better for static page but JSP include Action tag is better for dynamic page.
- JSP include directive includes original content in generated servlet but JSP include Action tag calls the include method.
The main advantage of this Tag is Code Reusability which means we can use a page many times like i have created header so i can use it in many pages, it saves time.
Syntax:
1 |
<jsp:include page="Relative URL" /> |
Syntax with Parameter:
1 2 3 |
<jsp:include page="relativeURL | <%= expression %>"> <jsp:paramname="parametername"value="parametervalue|<%=expression%>" /> </jsp:include> |
Example: Let’s understand it by an simple example.
#header.html: Create a header html file so that we can include it in our JSP file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style> #navigation ul li{ display: inline-block; padding: 0px 5px; } #navigation ul li a{ color:#fff; text-decoration: none; } #navigation { background: #4062b7; padding: 3px } </style> </head> <body> <div id="header"> <div class="shell"> <div id="navigation"> <ul> <li><a href="#" class="active">HOME</a></li> <li><a href="#">ABOUT</a></li> <li><a href="#">GALLERY</a></li> <li><a href="#">RESERVATION</a></li> <li><a href="#">LOCATION</a></li> <li><a href="#">CONTACT</a></li> </ul> </div> </div> </div> </body> </html> |
#index.jsp: here we will include above header file, check below code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>W3Adda Index Page</title> </head> <body> <jsp:include page="header.html" /> Today is: <%= java.util.Calendar.getInstance().getTime()%> </body> </html> |
Output: The output of this code will look like below image.