1

I am a beginner in JSP and I want to build a template. what I want exactly is to display images and background.

I built the following HTML code in Notepad and it worked perfectly. However, it is not working in JSP page.

    <%@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">
          </head>
          <body BACKGROUND="background.png"/>
                 <table>
                        <tr>
                            <td>
                                 <img src="brownie.png"/>                
                            </td>
                        </tr>
                        <tr>
                            <td>Images
                            </td>
                        </tr>
                 </table>
           </body>
    </html>

I tried to put the full paths of the images, but it did not work too. Would you please tell me what I am missing.

Read my Heart
  • 25
  • 1
  • 1
  • 5

1 Answers1

3

You're using relative paths. That means that the images must be in the same path as the one used to execute the JSP. So if the URL used to execute this JSP is

http://localhost/someApp/foo/bar/baz.action

The images must be available from these URL:

http://localhost/someApp/foo/bar/background.png
http://localhost/someApp/foo/bar/brownie.png

You should probably use absolute paths instead, to make your template usable from any locations. But beware to avoid hardcoding the context root of the application (someApp) in the URLs:

<body BACKGROUND="<c:url value='/images/background.png'/>"/>

using the JSTL, or

<body BACKGROUND="${pageContext.request.contextPath}/images/background.png"/>
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255