Image not displayed on JSP page

user1701556 picture user1701556 · Oct 9, 2012 · Viewed 23.2k times · Source

I have a simple JSP page that has one image that is part of the JSP page but that image is not displayed when I view the webpage. I can view the webpage but it does not include the image.

I have my img folder located in several places because I am trying to fix this problem. Inside the img folder is where I have img.jpg image that should be displayed inside JSP page.

war/img/img.jpg

war/WEB-INF/jsp/img/img.jpg

I am also not able to view img.jpg directly from my web server. For example: //localhost:8080/register/img/img.jpg I get HTTP Status 404 -

Here is my web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <display-name>SpringExampleSanjeev</display-name>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
        <url-pattern>/img/*</url-pattern>
    </servlet-mapping>

    <!-- Enable these types of content(files) to be viewed from webpage  -->
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>
</web-app>

Here is snippet of my JSP page

   <img border="0" src="/img/img.jpg" alt="header banner" width="304" height="228">
   <img src="<c:url value="/img/img.jpg" />" alt="TestDisplay"/>
   <img src="<c:url value='/img/img.jpg'/>" />
   <img alt="OS usage" src="/img/img.jpg">
   <img border="0" src="/WEB-INF/jsp/img/img.jpg" alt="header banner" width="304" height="228">
   <img border="0" src="/register/WEB-INF/jsp/img/img.jpg" alt="header banner" width="304" height="228">
   <img src="img.jpg">
   <img src="/img/img.jpg">
   <img src="/jsp/img/img.jpg">
   <img src="/register/jsp/img/img.jpg">
   <img src="/WEB-INF/jsp/img/img.jpg">
   <img src="/register/WEB-INF/jsp/img/img.jpg">

I am not sure what I am doing wrong. Putting an simple image on JSP page should not be this hard.

Answer

R Shah picture R Shah · Oct 9, 2012

Please try with this.

images folder parallel to WEB-INF. (images folder should NOT be inside WEB-INF).

In JSP,

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