Variables on jsp pages with pages enabled

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

My understanding is that the included page is essentially copied to the page, which would make me assume that if I declared a variable in the parent jsp, that it would be available in the children.

However, Eclipse complains about this (understandably, because I can include pages on any page as much as possible or use them as independent ones. And I'm trying to start the tomcat server, which it does not start.

Basically I want to get a couple of variables from his session on the parent page and use them on the child pages. This does not work.

So, I hit his idea of ​​getting them from the session on each of the child pages, however I was wondering if I could give them all the same variable names, or if I would have to choose different variable names for them on each page so that they would not collide .

Also, regarding import, if I import log4net into parent jss, do I also need to import it into children?

+43
java java-ee jsp
Jun 03 '09 at 9:06
source share
4 answers

There are two ways in JSP to include other jsp pages.

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

and

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

If you use the first, any variable declared in the parent JSP will be included in include.jsp (of course, Eclipse will not see this as you expected), since it is effectively copied by the compiler.

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

The same goes for import. Although it’s safe to redundantly import them into the inclusion page.

If I use the first, I prefer to suffix them with .jspf to denote a JSP fragment. I can turn off some Eclipses warnings in fragment files. But in general, I try to avoid using this method and prefer the second approach.

More information can be found in the documents: Include directive and JSP includes .

+74
Jun 03 '09 at 9:13
source share

From an object-oriented point of view, I would recommend not relying on the scope of the variable in parent.jsp, which is included in child.jsp. This is because when I include a fragment in jsp, I tend to want to reuse this fragment in many different places. For example, if I have child.jsp, I can use it in parent1.jsp as well as parent2.jsp. In this case, it is better not to change the inheritance.

+4
Jun 03 '09 at 12:05
source share

Use the following if you want to use a variable in the path of the page to be included:

 <% pageContext.include("/cities/" + (String) request.getAttribute("country_code") + ".jsp"); %> 
+4
Mar 03 '10 at 8:41
source share

When you create a variable, you must set the scope for the 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> 
+2
04 Oct. 2018-11-11T00:
source share



All Articles