File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Sharing variables between jsp:include files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Sharing variables between jsp:include files" Watch "Sharing variables between jsp:include files" New topic

Sharing variables between jsp:include files

Nathan Russell
Ranch Hand

Joined: Aug 18, 2004
Posts: 75
A bit of a newbie question ? or maybe just a dumb question !!

I have a jsp file (index.jsp) - it is part of a site where the header and footer of every page are the same, so I have written commonHeader.jsp and commonFooter.jsp.
index.jsp (and all other content pages) have a structure similar to:

This works fine, but now I want to be a bit more advanced !!

Within commonHeader I want to declare and instantiate java variables, and to be able to use them in either the main content page (index.jsp in this case) or commonFooter.jsp

For example, within commonHeader, I might want to create a String object of the authenticated username; but display it within the main body page (index.jsp). As each main body page could potentially display and work with this variable, it makes sense to write the code only once in commonHeader

Or another example; create a Date object within commonHeader (Date timeStarted = new Date(); ), then create another Date object in commonFooter (Date timeFinished = new Date(); ), and then within commonFooter compare both objects and display the difference as the time taken for the server to process the request.

Make sense ?

I can create the variables no problem in commonHeader.jsp; but cannot reference them in the other pages as they are not declared and instantiated within those pages.

Any help with this would be very much appreciated

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

Originally posted by Nathan Russell:
<jsp:include page="commonFooter.jsp" flush="false"></jsp:include>

That really unusual notation. when there's no body content to the include tag, it's much more conventional to use:

This works fine, but now I want to be a bit more advanced !!

One way to be "most advanced" would be to jettison scripts and scriplets from your JSP in favor of EL and JSTL.

Within commonHeader I want to declare and instantiate java variables

Since the main JSP and each included JSP are each executed in their own contexts, there is no way to share scripting variables across them.

You could resort to the JSP include directive which will simply include the referenced resources in-place, or you could place the objects in request scope as scoped variables which will be available to all resources participating in the context of the main request.

[Asking smart questions] [About Bear] [Books by Bear]
Stefan Evans

Joined: Jul 06, 2005
Posts: 1580
Given this:
Date timeStarted = new Date();
you can write an equivalent expression as a useBean tag

You could do it quite simply with useBean tags, and using request scope.

In commonHeader.jsp:
<jsp:useBean id="timeStarted" class="java.util.Date" scope="request"/>

In commonFooter.jsp:
<jsp:useBean id="timeStarted" class="java.util.Date" scope="request"/>
<jsp:useBean id="timeFinished " class="java.util.Date" scope="request"/>

The header executes first, and creates a new javabean storing it in session scope. The standard constructor for java.util.Date is invoked - meaning the time is "now"
When the footer executes, the bean "timeStarted" is already in request scope, so it just uses the existing one - the one you created in the header. It then creates a new one Date for "timeFinished"

voila! you have your two times in your footer, and can do whatever calculations you like.
I agree. Here's the link:
subject: Sharing variables between jsp:include files
It's not a secret anymore!