JSP provides an action element <jsp:include> which allows you to include the output or contents of another web page within the current web page at any specific location.
Syntax of <jsp:include>
This is a shortcut syntax of <jsp:include> action element, which includes the web page pointed by the URL.
<jsp:include page="URL" />
This is a another syntax of<jsp:include> action element which does the same thing as previous syntax but it's just a longer version.
<jsp:include page="URL" >
</jsp:include>
Advertisement
An example of using the <jsp:include> action
In the upcoming example, we are going to include the content of a web page Sample.jsp within another web page JspInclude1.jsp,
using the shortcut syntax of <jsp:include> action, such as -
<jsp:include page="URL" />
Sample.jsp
<html>
<head>
<title>JSP page</title>
</head>
<body>
<h2>Hello there. I hope you are having a pleasant day today. </h2>
</body>
</html>
Next, in JspInclude1.jsp web page, we are going to include the output of the web page Sample.jsp at a specified location.
JspInclude1.jsp
<html>
<head>
<title>Using JSP include Action</title>
</head>
<body>
We are going to include another webpage in this webpage.
<jsp:include page ="Sample.jsp"/>
<br/>
Never give up!
</body>
</html>
Executing the JspInclude1.jsp web page not only shows its own output but also includes the output of Sample.jsp web page, exactly
where we placed the <jsp:include> action element within JspInclude1.jsp web page.