In the last tutorial we discussed JSP include action with parameters. Here we will see how to pass parameters when using JSP include directive.
Example
In this example we are passing three string parameters to the included JSP page.
index.jsp
<html> <head> <title>Passing Parameters to Include directive</title> </head> <body> <%@ include file="file1.jsp" %> <%! String country="India"; String state="UP"; String city="Agra"; %> <% session.setAttribute("co", country); session.setAttribute("st", state); session.setAttribute("ci", city); %> </body> </html>
Above, I have used declaration tag for initializing strings and scriptlet for setting up them in session object. As the use of sciptlet is disregarded a long back, alternatively you can use <c:set> JSTL tag for doing the same – The code would be like this –
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <c:set var="co" value="India" scope="session"/> <c:set var="st" value="UP" scope="session"/> <c:set var="ci" value="Agra" scope="session"/> <%@ include file="file1.jsp" %>
file1.jsp
<%=session.getAttribute("co") %> <%=session.getAttribute("st") %> <%=session.getAttribute("ci") %>
Output:
In the above example I have passed the parameters using session implicit object, however you can also pass them using request, page and application implicit objects.
Leave a Reply