简体   繁体   中英

Java: Returning a relative URL from a Java class?

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

${request.contextPath}/images/foo

您可能正在寻找上下文路径

您需要在ServletRequest上使用getContextPath()方法,然后附加其余路径:

String url = request.getContextPath() + "/images/test.png";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM