Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variables in jsp pages with "included" pages

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?

like image 304
Omar Kooheji Avatar asked Jun 03 '09 09:06

Omar Kooheji


People also ask

What is JSP include page?

The jsp:include action tag is used to include the content of another resource it may be jsp, html or servlet. The jsp include action tag includes the resource at request time so it is better for dynamic pages because there might be changes in future.

When a JSP page is compiled what is turned into?

If the servlet is older, the web container translates the JSP page into a servlet class and compiles the class. During development, one of the advantages of JSP pages over servlets is that the build process is performed automatically.

What are the types of elements with Java Server Pages JSP )?

There are three types of JSP elements you can use: directive, action, and scripting.

How does JSP include work?

The include directive provides static inclusion. It adds the content of the resource specified by its file attribute, which could be HTML, JSP, or any other resource at translation time. Any change you make in the file to be included after JSP is translated will not be picked up by the include directive.


Video Answer


2 Answers

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.

like image 134
Kris Avatar answered Oct 03 '22 20:10

Kris


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"); %> 
like image 31
Dagvadorj Avatar answered Oct 03 '22 21:10

Dagvadorj