Include action tag is used for including another resource to the current JSP page. The included resource can be a static page in HTML, JSP page or Servlet. We can also pass parameters and their values to the resource which we are including. Below I have shared two examples of <jsp:include>
, one which includes a page without passing any parameters and in second example we are passing few parameters to the page which is being included.
Syntax:
1) Include along with parameters.
<jsp:include page="Relative_URL_Of_Page"> <jsp:param ... /> <jsp:param ... /> <jsp:param ... /> ... <jsp:param ... /> </jsp:include>
2) Include of another resource without sharing parameters.
<jsp:include page="Relative_URL_of_Page" />
Relative_URL_of_Page would be the page name if the page resides in the same directory where the current JSP resides.
Example 1: <jsp:include> without parameters
In this example we will use <jsp:include>
action tag without parameters. As a result the page will be included in the current JSP page as it is:
index.jsp
<html> <head> <title>JSP Include example</title> </head> <body> <b>index.jsp Page</b><br> <jsp:include page="Page2.jsp" /> </body> </html>
Page2.jsp
<b>Page2.jsp</b><br> <i> This is the content of Page2.jsp page</i>
Output:
Content of Page2.jsp
has been appended in the index.jsp.
![IncludeAction-output](https://beginnersbook.com/wp-content/uploads/2013/11/IncludeAction-output.png)
Example 2: <jsp:include> with parameters
index.jsp
I’m using <jsp:include>
action here along with <jsp:param>
for passing parameters to the page which we are going to include.
<html> <head> <title>JSP Include example with parameters</title> </head> <body> <h2>This is index.jsp Page</h2> <jsp:include page="display.jsp"> <jsp:param name="userid" value="Chaitanya" /> <jsp:param name="password" value="Chaitanya" /> <jsp:param name="name" value="Chaitanya Pratap Singh" /> <jsp:param name="age" value="27" /> </jsp:include> </body> </html>
display.jsp
<html> <head> <title>Display Page</title> </head> <body> <h2>Hello this is a display.jsp Page</h2> UserID: <%=request.getParameter("userid") %><br> Password is: <%=request.getParameter("password") %><br> User Name: <%=request.getParameter("name") %><br> Age: <%=request.getParameter("age") %> </body> </html>
Output:
As you can see that the content of display.jsp
has been included in index.jsp
. Also, the parameters we have passed are displaying correctly in the included page.
![include-action-with-parameters-output](https://beginnersbook.com/wp-content/uploads/2013/11/include-action-with-parameters-output.png)
Let us know if you have any doubts and questions regarding the topic. We will be happy to assist you!!
Leave a Reply