Accessing resources in .war from JSP

U

usenet-java

Hi all,

I have a web app deployed as a .war file. There are some images in the
root directory of the .war, and they're accessible just fine through
the browser, e.g.

http://localhost:8080/myApp/test.jpg

However, I'm running into a bit of trouble trying to load these images
from within the JSP code, in order to manipulate them. It works fine
with an absolute path to the file:

Image sourceImage =
Toolkit.getDefaultToolkit().getImage("c:\\myApp\\test.jpg");

...but this isn't portable, and I would much prefer to use relative
paths instead of hardcoding a particular filesystem location. After
Googling a bit, I've tried getting at the files a few different ways...

Image sourceImage = Toolkit.getDefaultToolkit().getImage("test.jpg");
Image sourceImage =
Toolkit.getDefaultToolkit().getImage(getClass().getResource("test.jpg"));
Image sourceImage =
Toolkit.getDefaultToolkit().getImage(request.getContextPath() +
"test.jpg");

No go on any of these. Could anyone please shed some light on the
proper way to load a file inside of the .war using a relative path?

Thanks!
 
A

Alexander

Hi all,

I have a web app deployed as a .war file. There are some images in the
root directory of the .war, and they're accessible just fine through
the browser, e.g.

http://localhost:8080/myApp/test.jpg

However, I'm running into a bit of trouble trying to load these images
from within the JSP code, in order to manipulate them. It works fine
with an absolute path to the file:

Image sourceImage =
Toolkit.getDefaultToolkit().getImage("c:\\myApp\\test.jpg");

..but this isn't portable, and I would much prefer to use relative
paths instead of hardcoding a particular filesystem location. After
Googling a bit, I've tried getting at the files a few different ways...

Image sourceImage = Toolkit.getDefaultToolkit().getImage("test.jpg");
Image sourceImage =
Toolkit.getDefaultToolkit().getImage(getClass().getResource("test.jpg"));
Image sourceImage =
Toolkit.getDefaultToolkit().getImage(request.getContextPath() +
"test.jpg");

No go on any of these. Could anyone please shed some light on the
proper way to load a file inside of the .war using a relative path?

Thanks!

You need to use one of these methods

getRealPath()
getResource()
getResourceAsStream()

of ServletContext to access the file. From a JSP you can access the
servlet context via the "application" variable.

application.getRealPath("/WEB-INF/test.jpg") for example

Alexander
 
U

usenet-java

Alexander said:
of ServletContext to access the file. From a JSP you can access the
servlet context via the "application" variable.

application.getRealPath("/WEB-INF/test.jpg") for example

You're a saint. Thanks for the help!
 
J

Juha Laiho

Alexander said:
....
You need to use one of these methods

getRealPath()
getResource()
getResourceAsStream()

of ServletContext to access the file. From a JSP you can access the
servlet context via the "application" variable.

application.getRealPath("/WEB-INF/test.jpg") for example

Note though, that it is also possible to deploy as an unexploded .war file,
in which case the getRealPath() may return null. The getResource() and
getResourceAsStream() will always return the resource data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top