How to call image path using JSP

I have my forlder images in my website folder. With full path as "web / images / logo1.jpg". My jsp page was located at web / WEB-INF / jsp. How can I view my images correctly?

+3


source to share


3 answers


When specifying an image path, we have two options: Provide an absolute path or a 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. the file inside webapps / ROOT / first.html can be accessed in the browser using http://example.com:8080/first .html .)

Or, if you have an example webapp; then webapps / example / first.html can be accessed using http://example.com:8080/example/first.html

So, if you have the following files: webapps / ROOT / jsp / first.jsp; and webapps / ROOT / images / logo1.jpg



Inside logo1.jsp, you can access the jpg like this:

<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 )

      

+1


source


you are passing in the relative url of the project, which you can pass using getContextRoot () as shown:



/ images / logo 1.jpg "/">

0


source


Why is your jsp in the WEB-INF directory? Something strange? I usually use something like / web / jsp for my jsp files. WEB-INF should be dedicated to web.xml etc. At least I think so :)

Anyway, anything in the network root could be /foldername/file.ext

In your case, you should be able to use

<img src="/images/logo1.jpg" />

      

-4


source







All Articles