Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How put/create variables in include page for use on all pages?

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to create some global variables that will be used on all my pages. I have a header.jsp page that goes on every page, so I'd like to put some variables in there that could then be used on the pages that include it. How do I do this?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the <%@ include file="blah.jspf" %> directive to include your fragment file (note that by convention, I use .jspf to indicate a JSP 'fragment').
<jsp:include> will not work for this purpose since that inclusion is done at run time (as opposed to the include directive which operates at translation time).
hth,
bear
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sweet! Thanks bear, I'll give it a try. Why didn't they make these both the same and just add an attribute like: setIncludeTime="compile" or soemthing like that?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An alternative way to have variables available to all pages in an application would be to have a bean with "application" scope.
Bill
 
David Hibbs
Ranch Hand
Posts: 374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
Use the <%@ include file="blah.jspf" %> directive to include your fragment file (note that by convention, I use .jspf to indicate a JSP 'fragment').

As I've said in other threads, please don't do this. It makes it very hard to maintain.
If you ever change how those variables are initialized, you'll have to make sure to touch every jsp that includes that page. Otherwise, they won't pick up the change.
Further, imagine if you miss a page and do something like rename a variable. 8 months later when you finally make a change to that page, it suddenly won't compile! You may figure it out, but someone else certainly will have to work for hours to figure it out.
If you do a <jsp:include>, you can have that page put values in the request or the session, either way. That way, when you make changes to your included file, they'll immediately affect the pages using it. And you at least won't have compile problems.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though my reply might be taken as am implicit approval of putting "global" vatiable in a JSP fragment, it was meant to illustrate the difference in run-time inclusion vs. translation-time inclusions.
Both William and David have made excellent points that, just as with global variables in Java programs, there are better ways to share info among pages.
Normally, I would have pointed that out along with my explanation, but I've recently been chided on the number of "why do you want to do it that way???" posts, and was taking the low road...
Bear
[ February 06, 2003: Message edited by: Bear Bibeault ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic