• Post Reply Bookmark Topic Watch Topic
  • New Topic

Global Variables in a Java Web Application

 
karl san
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Srinivas Jag
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to get the context path of a web app in javascripts.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65530
108
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hth,
bear
[ August 28, 2003: Message edited by: Bear Bibeault ]
 
Srinivas Jag
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!