This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sharing variables between jsp:include files

 
Nathan Russell
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64701
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stefan Evans
Bartender
Posts: 1691
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic