Reputation: 8030
I'm trying to load an image dynamicaly in a JSP and I'm trying to do something like:
<img src="<%= book.img %>">
where book.img contains a string (an absolute path). How can I fix the problem?
The error I receive is the following:
Bad value for attribute src on element img: DOUBLE_WHITESPACE in PATH.
Upvotes: 3
Views: 38130
Reputation: 2834
I tried a zillion different solutions mostly found in SO where this question is asked 3 or 4 other times. Many, many answers tell you what to write in your HTML but don't bother to tell you where that means your image file should reside. The only thing that worked for me was this:
Put your image file directly into your 'WebContent' directory. Then your HTML in your jsp file is:
<img src="${pageContext.request.contextPath}/myImageFileName.png">
By extrapolation I'd say that if you put the image file into a subdir such as WebContent/Images then you'd just add that subdir to the src, you can figure out how. But I didn't test that.
Upvotes: 0
Reputation: 94429
book.img
should contain an absolute url to the image on the server.
So if your images are stored in:
Webcontent/resources/images/
and you had an image:
close-button.png
book.img
should = /resources/images/close-button.png
Then in your JSP use JSTL to create the URL:
<img src="<c:url value="${book.img}"/>"/>
c:url
will prefix the domain and context to the absolute url.
Another way without JSTL is:
<img src="${pageContext.request.contextPath}${book.img}"/>
Upvotes: 2