clause
clause

Reputation: 35

JSP pages not showing picture even though it points to the correct directory?

I'm trying to show an image into my jsp page. I've made sure that the path to the particular img is correct. I've tried all the different method below. and everything already point to the folder but on the page, it just showing a small x and not showing the picture.

<img src="<%=request.getContextPath()%>/poster/Capture.JPG" alt="capture_test">

<img src="poster/Capture.JPG" alt="capture_test">

<img src="WebContent/poster/Capture.JPG" alt="capture_test">
I've moved the folder one level deeper and it still does not work. 

Any idea what am I doing wrong? Or is there a certain step to show it?

ps - edit

This is the file folder structure.

root is movie folder

movie -> build
      -> poster
      -> src
      -> WebContent
          -> META-INF
          -> WEB-INF
          -> all the different jsp files

Upvotes: 1

Views: 153

Answers (2)

Teddy
Teddy

Reputation: 4253

  1. Open the page in Chrome
  2. Press F12 to open developer tools
  3. Check the console tab
  4. Chrome will show you the full and final URL where it is expecting the image to be

Upvotes: 1

Serge Ballesta
Serge Ballesta

Reputation: 149185

You have to understand the difference between a local file system path, and the way the servlet container will translate an URL.

Your JSP will be transmitted to a client browser as an HTML page. The browser will find an <img> tag and will send another request to the server with the given URL.

The servlet container will then look for a servlet mapped to the url, or to a file under the root of the war and not at or below /WEB-INF.

So in your use case, the folder poster must reside under /WebContent, and the correct img src will be :

<img src="<%=request.getContextPath()%>/poster/Capture.JPG" alt="capture_test">

or

<img src="<c:url value="/poster/Capture.JPG"/>" alt="capture_test">

or

<c:url value="/poster/Capture.JPG" var="url"/>
<img src="${url}" alt="capture_test">`

Upvotes: 1

Related Questions