2 Minutes To Code (β Version)

JSP - 2 minute tutorial

Include action - How to include content output from another webpage dynamically

Include action is pretty exciting, it has no counterpart in servlet specification. (Notice that it is different from jsp include directive which is discussed in the next page.)

Suppose you have 2 jsp pages, landingPage.jsp & includedPage.jsp. Now, you can tell the webserver to process includedPage.jsp and add its output to the output of landingPage.jsp whenever client makes the request for landingPage.jsp.

The code to do this is shown below and uses the tag < jsp:include >.
<html>
	<body>

		Let's inlcude the output of another page.
		<br><br>
		
		<jsp:include page="includedPage.jsp" />
		<br><br>
		
		
	</body>
</html>
You can also pass some parameters to the includedPage.jsp using the tag <jsp:param> shown in the source code of includedPage.jsp shown below.

Source code of landingPage.jsp

<html>
	<body>

		Let's inlcude the output of another page.
		<br><br>
		
		<jsp:include page="includedPage.jsp">
			<jsp:param name="zone" value="jsp" />
		</jsp:include>
		<br><br>
		
	</body>
</html>

Source code of includedPage.jsp

<html>
	<body>

		This text is from the included page
		<br>
		Zone is : <%= request.getParameter("zone") %<
		
	</body>
</html>
Once you deploy landingPage.jsp and includedPage.jsp shown above in oyur webserver and make a request for the url landingPage.jsp, the webapge shown below will be generated :
Notice a couple of things here. First, unlike jsp forward action discussed in the previous page, the output of the landingPage.jsp too is included in final output.

Secondly, the includedPage.jsp should be a valid jsp page on its own because webserver execute it standalone and then just includes its output in the page that calls the include action on it.