File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes How put/create variables in include page for use on all pages? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How put/create variables in include page for use on all pages?" Watch "How put/create variables in include page for use on all pages?" New topic
Author

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

Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
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

Joined: Jan 10, 2002
Posts: 61218
    
  66

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
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

Joined: Mar 22, 2000
Posts: 12785
    
    5
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

Joined: Dec 19, 2002
Posts: 374
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.


"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61218
    
  66

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How put/create variables in include page for use on all pages?