Passing parameters to another JSP file using <jsp:include> tag

JspIncludeJstl

Jsp Problem Overview


I have a JSP file and in that file I am including another JSP file:

<c:forEach var="instanceVar" items="${instanceList}">
    <c:set var="instance"><jsp:include page="instance.jsp"/></c:set>
    ...
</c:forEach


In the file instance.jsp I want to use a variable instanceVar. I want to do it using JSTL. Is there any way to do this?

Jsp Solutions


Solution 1 - Jsp

<c:forEach var="instanceVar" items="${instanceList}">
    <jsp:include page="instance.jsp">
        <jsp:param name="myVar" value="${instanceVar}"/>
    </jsp:include>
</c:forEach>

In the instance.jsp

<c:out value="${param.myVar}"/>

Solution 2 - Jsp

An alternative would be using setAttribute() and getAttribute()

Solution 3 - Jsp

Another alternative is using JSTL tag c:set and request scope.

<c:set var="instance" value="${your.value}" scope="request"/>
<jsp:include page="instance.jsp"/>

Solution 4 - Jsp

The solution that work for me is the following

<c:set var="instance" value="${semaforoData}" scope="request"/>
<jsp:include page="semaforo.jsp"/>

in the jsp file, the code is:

<c:forEach var='itemSemaforo' items='${semaforoData}' varStatus='loopSemaforo'>
Print data
</c:forEach>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSIGSTPView Question on Stackoverflow
Solution 1 - JspAlexView Answer on Stackoverflow
Solution 2 - JspSpringLearnerView Answer on Stackoverflow
Solution 3 - JspYuriy TumakhaView Answer on Stackoverflow
Solution 4 - JspJorge Santos NeillView Answer on Stackoverflow