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?
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
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.
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 ]
subject: How put/create variables in include page for use on all pages?