Assume you have an image file located at image/some.gif. To access this from a JSP within your portlet, do this:
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %> <portlet:defineObjects/> <img src="<%= renderRequest.getContextPath() %>/image/some.gif" />
The context path obtained from the render request provides the proper context.
Of course, you can create a similar < img > tag from within the portlet itself (I prefer letting the JSP build the user interface).
I've tried that, and nothing...
In the source code of the HTML page there is:
But I put that image everywhere, and nothing...
I even created a File, just to see it would read, and it does...
Strangely with this:
java.io.File file = new java.io.File(renderRequest.getContextPath()+"/images/dodemo.gif");
I got C:\tester\images\dodemo.gif!
Do you have a running Portlet? (A war file with the right structure, depolyable and outputting some text?
If yes try this:
1. create the image folder and copy images
your app * | + - - + - - WEB-INF (your web- folder, you should have already) | + - index.html | + - - + - - images (NEW) + myImage.jpg (NEW)
2. link your image
Finally got it running.
The problem was that my ecllipse project got the directory of the images in the wrong place, so it didn't exported it to the war file.
I tried to put the files by hand but, nothing, because it shoul be inside the war.
So many time lost with this, how stupid of me?!
Than you for your help!
Now you know why I don't trust Eclipse (or any IDE) to do my builds or packaging. I always use Maven 2, or Ant, and I have yet to be surprised. But I am constantly helping people with strange problems where code works for me but not for them only to find out later that they were running/building/packaging within Eclipse or NetBeans.
Be that as it may, I am glad you got it running!