Search code examples
jspjstl

How to pass data between JSP Pages using JSTL?


I have 3 JSP pages, let's call it First.jsp Second.jsp Third.jsp. I would like to move data from First.jsp to Third.jsp. I know you can move data from using <form action=".jsp" method="post"> as well as <c:redirect url=.jsp> <c:param name= value=> </c:redirect> however these will only work if both pages are connected to each other i.ex First.jsp and Second.jsp.

I've tried solution from this Link. However, I don't really get the first solution which is using <a ref/>. But i tried it and it doesn't work. The second solution is like what i mentioned above. The third and fourth solution is not using JSTL if i recall correctly.

In my code, I would like to move option selected in dropdown list in First.jsp to Third.jsp

Here's my code for the drop down list

<s:setDataSource var="ds" driver="com.mysql.jdbc.Driver"
                 url="jdbc:mysql://127.0.0.1:3306/project?useLegacyDatetimeCode=false&serverTimezone=Asia/Pontianak&useSSL=false"
                 user="root" password="root"/>

<s:query dataSource="${ds}" var="resultseta">
    SELECT DISTINCT departureto, departurefrom FROM schedule
</s:query>

<body>

        From:
        <SELECT class="drop" style="WIDTH: 143px" id="departfromcmb" name="departfromcmb">
            <option value="">Select your location</option>
            <c:forEach items="${resultseta.rows}" var="departfromrow">
                <option value="${departfromrow.departurefrom}"> ${departfromrow.departurefrom} </option> //i would like to pass this data to third.jsp
            </c:forEach>
        </SELECT> <br><br>

        To:
        <SELECT class="drop" style="WIDTH: 150px" id="departtocmb">
            <option value="">Select your destination</option>
            <c:forEach items="${resultseta.rows}" var="departtorow">
                <option> ${departtorow.departureto}</option> //i would like to pass this code to third.jsp
            </c:forEach>
        </SELECT> <br><br>

</body>

Solution

  • So, putting all the stuff from the comments together:

    Page 1

    On this page you have the form posting to page 2:

    <form action="page2.jsp" method="post>
      <input name="foo"/>
    </form>
    

    Page 2

    Here we set the posted form field as a session attribute:

    <c:set var="bar" value="${param.foo}" scope="session"/>
    

    Page 3

    Here we output the session attribute that was set on the previous page:

    <c:out value="${sessionScope.bar}"/>
    

    Please note that, apart from , plays an important role here. Session attributes are available through an implicit object.

    See, https://stackoverflow.com/tags/el/info