This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JSP and the fly likes Global Variables in a Java Web Application Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "Global Variables in a Java Web Application" Watch "Global Variables in a Java Web Application" New topic
Author

Global Variables in a Java Web Application

karl san
Greenhorn

Joined: Aug 27, 2003
Posts: 4
Hi,
I am using absolute paths in my web application. So i am using the function request.getContextPath() to get the absolute path. I am required to use this function many time in the same page and across pages. So instead of calling the function again and again, i am thinking of declaring a global variable in my startup page and use the same variable across pages. I am not sure whether this is the best way to do it. Please advice.
Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56214
    
  13

Welcome to the Ranch, Sandeep SV!
You'll find this forum a great place to seek help on JSP pages, and there aren't many rules you'll have to worry about, but one is that proper names are required. Please take a look at the JavaRanch Naming Policy and change your display name to match it.
Thanks!
bear
JSP Bartender


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Srinivas Jag
Greenhorn

Joined: Jun 30, 2003
Posts: 9
Since request.getContextPath() returns the URI of the resource, in this case, your JSP page, it would be different for every page.
What you could do in the case of multiple usages within a single page is to assign it to a variable at the start of the page.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56214
    
  13

Since request.getContextPath() returns the URI of the resource

That is not correct. getContextPath() only returns the portion of the URI that identifies the web app context. That is, something like '/xyz' for web app 'xyz'.
Sandeep, my advice would be to just continue to call the function directly. Storing it in a global variable will not save you much typing or gain any appreciable performance, and will probably only serve to confuse others who look at your pages.
bear
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56214
    
  13

Btw, if the notation <%= request.getContextPath() %> sticks in your craw, you could investigate incorporating the use of JSTL in your pages. In that case you could use the alternative notation of

if that makes things look better to you.
hth,
bear
[ August 28, 2003: Message edited by: Bear Bibeault ]
karl san
Greenhorn

Joined: Aug 27, 2003
Posts: 4
Is there a way to get the context path of a web app in javascripts.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56214
    
  13


hth,
bear
[ August 28, 2003: Message edited by: Bear Bibeault ]
Srinivas Jag
Greenhorn

Joined: Jun 30, 2003
Posts: 9
Originally posted by Bear Bibeault:
[QB]
That is not correct. getContextPath() only returns the portion of the URI that identifies the web app context. That is, something like '/xyz' for web app 'xyz'.
Sorry..mixed it up with getRequestURI()
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Global Variables in a Java Web Application
 
Similar Threads
creating a common jsp for many JSPs
Making web pages elements Independent of Resolution of the screen?
JSF and JavaScript
Static variables in JS
Dont declare global variables in Servlet