How to have relative paths refer to the context root

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to have relative paths refer to the context root

Jesse Altman
Hello,

I am running Tomcat 8.5.15 on Windows 10.

I have a folder called "myapp" with my web application files inside of it.
Inside ${tomcat_base}/conf/Catalina/localhost I have my configuration file
"other.xml" with the following entry:

<Context docBase="{full path to the myapp folder}">
</Context>

The index.html file lives inside myapp/client with the following text:

<a href="file1.txt">text file 1</a>
<a href="/file2.txt">text file 2</a>

file1.txt also lives inside myapp/client
file2.txt lives inside myapp

The html file is then accessed by going to http://locahost:8080/other/client.
When clicking the "text file 1" link it correctly finds the file under
http://localhost:8080/other/client/.  When clicking on the "text file 2"
link unfortunately it looks for it under http://localhost:8080/, and it is
not found.  I was hoping since the path to file2.txt starts with a '/' it
would look for it under the context root http://localhost:8080/other/, but
unfortunately it seems to look for it under the base webapps folder.  Is
there a way to have a path refer to the context root of my application
without specifically adding the context root in there?

Thank you,

Jesse
Reply | Threaded
Open this post in threaded view
|

Re: How to have relative paths refer to the context root

Christopher Schultz-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Jesse,

On 5/17/17 3:33 PM, Jesse Altman wrote:

> Hello,
>
> I am running Tomcat 8.5.15 on Windows 10.
>
> I have a folder called "myapp" with my web application files inside
> of it. Inside ${tomcat_base}/conf/Catalina/localhost I have my
> configuration file "other.xml" with the following entry:
>
> <Context docBase="{full path to the myapp folder}"> </Context>
>
> The index.html file lives inside myapp/client with the following
> text:
>
> <a href="file1.txt">text file 1</a> <a href="/file2.txt">text file
> 2</a>
>
> file1.txt also lives inside myapp/client file2.txt lives inside
> myapp
>
> The html file is then accessed by going to
> http://locahost:8080/other/client. When clicking the "text file 1"
> link it correctly finds the file under
> http://localhost:8080/other/client/.  When clicking on the "text
> file 2" link unfortunately it looks for it under
> http://localhost:8080/, and it is not found.  I was hoping since
> the path to file2.txt starts with a '/' it would look for it under
> the context root http://localhost:8080/other/, but unfortunately it
> seems to look for it under the base webapps folder.  Is there a way
> to have a path refer to the context root of my application without
> specifically adding the context root in there?

This is not possible without a dynamic resource. So you can do it e.g.
with JSP:

<a href="${pageContext.request.contextPath}/file2.txt">text file 2</a>

(You should really encode the above URL through response.encodeURL but
I wanted the example to be clean.)

But if you know that index.html lives at the root of the context path,
then simply:

<a href="file2.txt">text file 2</a>

is all you need.

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlkdLIwACgkQHPApP6U8
pFjktw//RzPReTkvXPEJPa9ciA7m/aP0pG5vXawJ6kWrhZvvbpFQ4xcDYdt9MyrO
Yre29U0fQY32fsHMp9q9qUVBPhtt2rq3l2a4UUwpqDV9lfw/znojuJeTZ/OWeLq3
cyjgfvpXgAODHwgiOPx6k7XDli7bUNkiMuz2V5tbDoq7lSBN0DAVL+zzlQ1APEo2
hH80pyDiUY0Y/fxbV2SHMsupmIc9y4wswlqgAUcI430KFtMz6svuUhja4kztCxa0
DWta6igFzr3hGu6U8q3AtPoF+I5VfZyQbjVdc15eh+iEPjKHYfq3yXeCtdknFlu0
CDUCty/G/crI6miwTsoUWtfHZ6rtHLRz2tRNRdraUMDtzZ8lb64295JjXky2Q4LN
3qcmEOE3fEmGJBJLdWSfSk/SEjyigmOtkTayxGbJUysfSUOojIBj8Wm7WNVEVu0e
EtKfXa3/Y4gxPtAfIl3/oibT8aGOuSMDtOxTXMa17cNnqaR4DxclpjBBV2s+SVRC
uH854X0QpiVFNCmAsrS1l0cIt7knsIL8n1JL1pkOyfrGT0RzY6igp7+on06ZDxaB
BxcoCRq59ayxtDTZIVrol2iSpMkZccBpGcDX2/R+KfMasMfYK3DHu4+vMSF7u1nX
i5dFNC7CgaAKwRllJvssAZBGHEhvMhL9g8ZlJ5PdpsJeQUUZ+m8=
=4k93
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]