Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't display images in JSP

Tags:

java

jsp

I can't display images in JSP (and can't access other things like external Javascript/jQuery through SRC). The following JSP page just contains one <img> tag that can not display the image located by the SRC.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="Temp.htm" method="post">
            <img src="/images/temp.jpg" alt="Not available">
        </form>
    </body>
</html>

The images are in /WEB-INF/jsp/images. I have also tried to change the image location and ensure all the times that the path given to SRC is correct still it didn't. The same is possible with the applications without the framework. It works there perfectly. What am I missing here?

like image 920
Bhavesh Avatar asked Feb 20 '12 18:02

Bhavesh


3 Answers

First, /WEB-INF is not accessible from the outside.

Second, if you use /images/temp.jpg, the browser will load images from the root of the web server, and not from the root of your web-app (which is deployed under a context path).

Use the JSTL <c:url> tag to generate absolute paths from the root of the web-app:

<img src="<c:url value='/images/temp.jpg'/>" alt=.../>
like image 99
JB Nizet Avatar answered Oct 04 '22 22:10

JB Nizet


You cannot access files that are located in /WEB-INF/ directly from the web. The folder is protected from web access.

Locate your image files somewhere else. /images/temp.jpg is a absolute path, however you likely need a relative one. Point your image path to image/temp.jpg and place the images in the /images/ folder directly located under your web root. E.g. WebContent/images or src/main/webapp/images (depending on your web root).

like image 34
Udo Held Avatar answered Oct 04 '22 22:10

Udo Held


Try put in the webapp directory, not WEB-INF. Obv. dont nest in the jsp dir.

like image 32
MikePatel Avatar answered Oct 04 '22 22:10

MikePatel