I have my images forlder under web folder. With the complete path as "web/images/logo1.jpg". My jsp page was located web/WEB-INF/jsp. How can I correcly view my images?
-
possible duplicate of [Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP](http://stackoverflow.com/questions/3655316/browser-cant-access-find-relative-resources-like-css-images-and-links-when-cal) – BalusC Jan 29 '13 at 13:29
3 Answers
While specifying path of images we have two options: Providing Absolute path or relative path. Assuming you have a webapp ROOT i.e. all your code is inside webapps/ROOT ( ROOT is the default webapp in TOMCAT i.e. file inside webapps/ROOT/first.html can be accessed on browser using http://example.com:8080/first.html. )
Or if you have a webapp example; then webapps/example/first.html can be accessed using http://example.com:8080/example/first.html
So, assuming you have following files webapps/ROOT/jsp/first.jsp ; and webapps/ROOT/images/logo1.jpg
Inside logo1.jsp you can access the jpg as follows:
<img src="/images/logo1.jpg" /> (absolute path; because it starts with a slash ; this slash is mapped to the starting directory)
<img src="../images/logo1.jpg" /> (relative path; because it DOES NOT starts with a slash ; you have to give path relative to location of your jsp )

- 10,568
- 11
- 59
- 98
you have pass the relative url of the project which you can pass using getContextRoot() as shown:
/images/logo1.jpg"/>

- 21
- 8
Why is your jsp located in the WEB-INF directory? Kinda odd? I'm usually using something like /web/jsp for my jsp files. WEB-INF should be dedicated to web.xml and suchies.. at least I think so :)
Anyways, anything in the web root may be accessed as /foldername/file.ext
In your case, you should be able to use
<img src="/images/logo1.jpg" />

- 136
- 1
- 8
-
1JSP's in WEB-INF is a good practice http://stackoverflow.com/questions/6825907/why-put-jsp-in-web-inf – Carlos Gavidia-Calderon Jan 29 '13 at 14:19