0

I have some images stored in /images the problem is that i wish to return a url like so from a class.

/images/test.png

The problem is that in my development machine my url is in the form of

http://localhost/myApp

and in production its in the form of

http://www.mysite.com

so i can't just return /images as this doesn't work on the development machine

I suppose what i am trying to do in Java is the same as teh JSTL tag does

<c:url

So when i am in my development machine it would return

/myApp/images/test.png

and in production

/images/test.png

can anyone help?

Thanks in advance

Jama A.
  • 15,680
  • 10
  • 55
  • 88
Martin
  • 23,844
  • 55
  • 201
  • 327
  • Related: http://stackoverflow.com/questions/3655316/browser-cant-access-css-and-images-when-calling-a-servlet-which-forwards-to-a-j/3658735#3658735 – BalusC Feb 22 '12 at 17:26

2 Answers2

3
${request.contextPath}/images/foo

You're probably looking for the context path.

McDowell
  • 107,573
  • 31
  • 204
  • 267
1

You need to use getContextPath() method on the ServletRequest - and then append the rest of your path:

String url = request.getContextPath() + "/images/test.png";
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • Thanks, but its a standard java STATIC method hence i don't have request, is there a work around? – Martin Feb 22 '12 at 17:06
  • I don't think it's possible. Only the actual request knows what the context path is. You need to design your method in such a way as to give it access to the `request` or possibly pass the value of `request.getContextPath()` into that method as a "base path". – Aleks G Feb 22 '12 at 17:09