Domanda

I have a JSP which simply shows and image as header. I will change this into a .tag file for custom tag development. I am doing this in eclipse and my project structure is -

enter image description here

The jsp i am trying to run on the server is Header.jsp under jsp folder. The problem is that the image is not displayed, even when I use the fully qualified path of the image. Insted, i see a red cross. How do I fix this ?

When I use this file as a .tag file referenced by another jsp, the contents of tag do not appear in that jsp.

JSP code -

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib prefix="myTags" tagdir="/WEB-INF/tags"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<img src="/images/java_logo.gif"><br>

</body>
</html>
È stato utile?

Soluzione

The URL you specify here is absolute for your site, not the web app.

<img src="/images/java_logo.gif">

So try to use (with WEBAPPROOT replaced by the correct name)

<img src="/WEBAPPROOT/images/java_logo.gif">

or make it relative:

<img src="../images/java_logo.gif">

Altri suggerimenti

If you're in JSP or Facelets, it is way better to use HttpServletRequest#getContextPath:

<img src="${request.contextPath}/images/java_logo.gif" />

If you happen to use JSTL:

<img src="<c:url value="images/java_logo.gif" />" />

In this way, you avoid using relative paths and/or guessing what would be your current web application path (this is in case you change the name to display the app or something similar). For example, if you happen to have this structure:

- WebContent
  - images
    + java_logo.gif
  - jsp
    + Header.jsp
    - anotherFolder
      + Another.jsp

If you want to add java_logo.gif in Another.jsp, you just need to do this:

<img src="${request.contextPath}/images/java_logo.gif" />

Unlike the relative path:

<img src="../../images/java_logo.gif" />
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top