Forum Moderators: phranque

Message Too Old, No Replies

find base URL for the web application in a jsp file?

         

stanza

9:41 am on Sep 11, 2005 (gmt 0)

10+ Year Member



I am using Tomcat JSP server.

Web applications are usually deployed in a *.war file. Let's say I deploy abc.war, then jsp pages I put into this web application will end up as something like:
http://example.com/abc/hello.jsp

But if I later want to change the path name to xyz, then I just need to rename abc.war to xyz.war, and deploy it to Tomcat. Then my page will automatically change to
http://example.com/xyz/hello.jsp

My question is:

Within a jsp page or a servlet, is it possible to programmatically retrieve the base URL of the web application? In the first case it is
http://example.com/abc/
and in the second case it is
http:/example.com/xyz/

The base URL will make it easier to create link to pages or images. For example, I can always write the reference URL to be getBaseURL() + "/jpg/banner.jpg", instead of relying on some relative URL like "../../jpg/banner.jpg" or hard-coding the absolute path to be "/abc/jpg/banner.jpg"

[edited by: jdMorgan at 2:29 pm (utc) on Sep. 11, 2005]
[edit reason] Example.com [/edit]