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

80,037

Solution 1

<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

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

Solution 3

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

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>
Share:
80,037
SIGSTP
Author by

SIGSTP

Updated on May 20, 2021

Comments

  • SIGSTP
    SIGSTP almost 3 years

    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?

  • Gangnus
    Gangnus over 6 years
    And how can I access the param.myVar in the java code in the included jsp, please?
  • Alex
    Alex over 6 years
    The question was about JSTL. And it is better not to mix java code with JSTL.
  • Roshana Pitigala
    Roshana Pitigala about 6 years
    @SpringLearner setAttribute() of HttpSession or HttpServletRequest?
  • Francisco M
    Francisco M over 4 years
    It worked*. but in my case, I wasn't allowed to pass a list. The jsp complier raise an error saying which the params has to be static (value="some static value"). Is not to downvote it but it didn't solved my problem.
  • Francisco M
    Francisco M over 4 years
    Worked for me and give me the chance to pass any kind of dynamic objects. It don't pass really the parameters, but, create a variable in the page/backing class which can be used in the included jsp.