From an included JSP, how do we access a variable declared in another included JSP (same parent)

StackOverflow https://stackoverflow.com/questions/5872146

  •  28-10-2019
  •  | 
  •  

Question

I have a parent JSP with code that looks like

<jsp:include page='a.jsp' flush='true'/>
<jsp:include page='b.jsp' flush='true'/>
<jsp:include page='c.jsp' flush='true'/>

a.jsp has a Java object which I need to access in c.jsp

Is there a way to do this without moving any code from a.jsp to the parent jsp?

Here is how the a.jsp looks like:

<%@ page import="com.xxx.yyy.myClass" %>
<%
    // Some processing here
%>
<table width="100%" cellspacing="0" class="scrollableTable">
    <thead>
        <tr>
        <%
            // Some processing here
            w_myObject = myAPI.getmyObject(param1, param2);
            // Some processing here
        %>
        </tr>
        <!-- Display contents of w_myObject in subsequent rows of this table, here -->
    </thead>
</table>

And I want to access w_myObject in c.jsp

Was it helpful?

Solution

This is all to do with scopes. If your Object is in Request scope then of course it will have access. Or if it is in Session scope it will have access. However, if it is in PageContext scope I believe it will be lost, as each jsp include creates its own scope.

So what I'm trying to say is put the Object in request scope and it will be visible across all JSPs.

**a.jsp**
request.setAttribute("myObjectKey", w_myObject);

**c.jsp**
w_myObject = (TypeOfMyObject)request.getAttribute("myObjectKey");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top