54

What are the scoping rules for variables in a jsp page with pages added to them using tags?

My understanding is that an included page is essentially copied verbatim into the page, which would lead me to assume that if I've declared a variable in a Parent JSP that it would be available in the child ones.

However Eclipse complains about this (understandably because I could feasibly include the pages in any page or use them as stand alone. And when I try to start the tomcat server it fails to start.

I basically want to get a couple of variables from the session in the parent page and use them in the child pages. This doesn't work.

So I've struck ont he idea of getting them from the session in each of the child pages, however I was wondering if I could give them all the same variable names, or if I'd have to pick different variable names for them in each page so they didn't clash.

Also what about imports if I import log4net in the parent jss do I also have to import it in the child ones?

2
  • If the variables are in session, where is the problem? And BTW what do you mean by session of the parent page? Commented Jun 3, 2009 at 10:03
  • 1
    THe variables are integer variables and since I have toget them from the session then cast them to Integer then get the int value of them everyt ime I use them. also checking for nullity I was trying to save myself some pain by not having to do that by just declaring a local variable. Commented Jun 3, 2009 at 11:48

4 Answers 4

88

In JSP there are two ways of including other jsp pages.

<%@include file="include.jsp"%>

and

<jsp:include page="include.jsp" />

If you use the former, then any variable declared on the parent JSP will be in scope in the include.jsp (of course Eclipse will not see this as you surmised) as it is effectively copied in by the compiler.

If you use the second approach, the inclusion is done at runtime and the include page has its own scope.

Ditto for imports. Although it is safe to redundantly import them in the include page.

If I'm using the former I prefer to suffix them with .jspf to signify a JSP fragment. I can than turn off some of Eclipses warning in the fragment files. But in general I try to avoid using that method and prefer the second approach.

More information can be found in the docs here: Include directive and JSP include.

2
  • I am bemused by this example. Perhaps there are challenges with the Jasmper code? Using the <%@include ...> form; I get a runtime JSP compiler error when I try to write to a String inside the 'inclued' .JSP. I can accept that maybe I'm not allowed to pass values "backward" -- Why the error, based on the stated rules for @include?
    – will
    Commented Oct 9, 2013 at 12:19
  • 1
    Note that a space between <% and @ will cause an error, so don't try to write <% @include file="include.jsp" %>. Without space as shown in the answer will work fine.
    – CodeReaper
    Commented Feb 5, 2015 at 8:45
7

Use the following, if you want to use variable within the path of the page to be included:

<% pageContext.include("/cities/" + (String) request.getAttribute("country_code") + ".jsp"); %>
4

From an object-orientated point of view, i would recommend not relying on the scope of the variable in parent.jsp being included in the child.jsp. This is because when i include a fragment in a jsp i tend to want to reuse that fragment in many different places. For example if i have a child.jsp i may want to use it in parent1.jsp as well as parent2.jsp. In the case it is better not to variable inheritence.

1
  • 10
    What would you do instead?
    – Brimstedt
    Commented Nov 16, 2009 at 14:31
2

When you create a variable, you should set the scope to session, otherwise, the included page will not see it. Example :

<logic:iterate id="supportTmp" name="publicites" indexId="indexLots" scope="session">                                    
    <c:set var="support" value="${supportTmp}" scope="session"/>
    <c:choose>
    <c:when test="${publiciteMoniteur == true}">
    <jsp:include page="/jsp/bodies/suiviEnvoiPubliciteMoniteurLigne.jsp" />
    </c:when>
    <c:otherwise>
        <jsp:include page="/jsp/bodies/suiviEnvoiPubliciteDefautLigne.jsp" />
    </c:otherwise>
</c:choose>
</logic:iterate>
1
  • Are you sure, you don't mean a REQUEST scope? If you do, the jsp:included pages will see your variables. Session scope will create a value that will stick in the session for no good reason. Commented Jun 4, 2020 at 19:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.