• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSP can't find image

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

1. Within a Spring 4 web application, running on Tomcat 7, I have a simple welcome JSP (relevant code below) which fails to find an image! Wondering if someone can point out (silly?) mistake I'm doing..?
-----section of jsp Code----
<html>
<head>
<title> XXX Services Ltd. </title>
</head>
<body>
img src="../img/logo.jpg" alt="logo"
<h3>We are XXX Services Ltd </h3>
</body>
</html>
-----section of JSP Code----

2. Attached is my directory structure. All images are stored under 'img' and all JSPs under 'jsp' , and both of these directories under 'WebContent'

3. As per below web.xml, the 'whoweare.JSP' is correctly loaded, but it can't find logo image.


4. Surely, relative to root, if the jsp has been found correctly....then..relative to jsp, the images have to be found at: '../img/' ?

5. I'm running the applicatoin from Eclipse (as against manually deploying under 'tomcat/webapps') and here is the deployed applicaton directory under 'webapps':
/Dropbox/JavaPrograms/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps$ find MCSL/
MCSL/
MCSL/horiz_menu.js
MCSL/img
MCSL/img/logo.jpg

...
MCSL/jsp
MCSL/jsp/whoweare.jsp

...
MCSL/META-INF
MCSL/META-INF/MANIFEST.MF
MCSL/META-INF/maven
MCSL/META-INF/maven/MCSL
MCSL/META-INF/maven/MCSL/MCSL
MCSL/META-INF/maven/MCSL/MCSL/pom.properties
MCSL/META-INF/maven/MCSL/MCSL/pom.xml
MCSL/WEB-INF
MCSL/WEB-INF/classes
MCSL/WEB-INF/classes/....
MCSL/WEB-INF/lib
MCSL/WEB-INF/lib/aopalliance-1.0.jar
MCSL/WEB-INF/lib/commons-logging-1.1.3.jar
MCSL/WEB-INF/lib/spring-core-4.0.5.RELEASE.jar
MCSL/WEB-INF/lib/spring-web-4.0.5.RELEASE.jar
MCSL/WEB-INF/lib/spring-webmvc-4.0.5.RELEASE.jar
...
MCSL/WEB-INF/spring-dispatcher-servlet.xml
MCSL/WEB-INF/web.xml


Many Thanks.
Screenshot-from-2014-06-06-21-47-29.png
[Thumbnail for Screenshot-from-2014-06-06-21-47-29.png]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see the JspFaq for the answer.

Short version: using "../" in a JSP page is almost guaranteed to cause issues.
 
maddy sidhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cheers! Will take a look. Did a quick test with below but didn't work:

<% String strContextPath = request.getContextPath();
System.out.println("ContextPath:-->"+ strContextPath);%> <!-- Prints correct root path-->
.....
<img src="<%=strContextPath%>/img/logo.jpg" alt="logo"/>
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use the <c:url> JSTL tag. (And stop putting Java code into JSPs. It's not 2001 anymore!)
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. You can test the path by typing it directly into the browser's address bar. When the image appears, you'll know that path is right.
 
maddy sidhan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bear.

The issue resolved now after 1) I include the <mvc:resources.../> in spring dispatcher XML and 2) keep reference image in the JSP as

I should have read the Spring documentation properly. Spring dispatcher handles static resources for your via above tag.

Thanks for your help
 
She'll be back. I'm just gonna wait here. With this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic