This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Sharing variables between jsp:include files" Watch "Sharing variables between jsp:include files" New topic
Author

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

Nathan
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

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] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1016
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.
 
 
subject: Sharing variables between jsp:include files
 
Similar Threads
jsp:include tag not processed in Tomcat 4.0.4 and Apache2 combination
RequestDispatcher include..
please solve this EL
Significance of Flush in <jsp:include ?
struts & tiles - passing a file name using tiles