Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2006

Tip: Looking for answers? Try searching our database.

Accessing resources in .war from JSP

Thread view: 
usenet-java@shat.net - 20 Jul 2006 17:35 GMT
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!
Alexander - 20 Jul 2006 18:49 GMT
> Hi all,
>
[quoted text clipped - 26 lines]
>
> 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
usenet-java@shat.net - 20 Jul 2006 20:07 GMT
> 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!
Juha Laiho - 22 Jul 2006 10:06 GMT
Alexander <nobody@example.com> said:
>> 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.
...
>You need to use one of these methods
>
[quoted text clipped - 6 lines]
>
>   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.
Signature

Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
        PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.