| 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
|
|
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
|
|
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
|
|
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
|
|
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()
|
 |
 |
|
|
subject: Global Variables in a Java Web Application
|
|
|