Earlier we have shared how to include a page to another JSP page using include directive and include action tag. We have also discussed the difference between include directive and include action tag. In this post we will see how to pass parameters to included page while using jsp include action tag (<JSP:Include>). In order to pass the parameters we need to use the <jsp:param> action tag.
Example
In this example we are including a JSP page (file.jsp) to the main JSP page (index.jsp) using <jsp:include> action tag. To pass the parameters from index to file page we are using <jsp:param> action tag. We are passing three parameters firstname, middlename and lastname, for each of these parameters we need to specify the corresponding parameter name and parameter value. The same we can extract at the included page using expression language or expression tag & implicit object.
index.jsp
<html> <head> <title>JSP include with parameters example</title> </head> <body> <jsp:include page="file.jsp" > <jsp:param name="firstname" value="Chaitanya" /> <jsp:param name="middlename" value="Pratap" /> <jsp:param name="lastname" value="Singh" /> </jsp:include> </body> </html>
file.jsp
${param.firstname}<br> ${param.middlename}<br> ${param.lastname}
Output:
As you can see that the included page displayed the parameters values passed from the main page. For displaying the parameters on the file.jsp page, we have used the expression language (${}). However you can also use the following code which is using the JSP expression tag and request implicit object.
<%= request.getParameter("firstname")%> <%= request.getParameter("middlename")%> <%= request.getParameter("lastname")%>
That’s all we have for this topic. In the coming post we will discuss how to pass parameters and their values to another page using include directive. Meanwhile if you have any questions, ask the same in the comment section below.
Migadde Denis says
This was really help full please. thank u so much