Retrieving relative url path of file inside a include file, jsp

爷,独闯天下 提交于 2019-11-30 15:53:30
BalusC

The <script src> is relative to the current request URL (as you see in browser address bar), not to the server side location of the JSP file. It's namely the webbrowser who needs to load the script, not the webserver.

So, if the current request URL is

http://Mywebpage.com/Open/This/Folder/Main.jsp

and the JS file is actually located in

http://Mywebpage.com/HL.js

then you need to reference it as

<script type="text/javascript" src="/HL.js"></script>

The leading slash will make it relative to the domain root.

However, if your webapp is not deployed on the domain root per se, but on a context path, such as /Open in your (oversimplified) example, and your JS file is actually located in

http://Mywebpage.com/Open/HL.js

then you need to prepend the URL with HttpServletRequest#getContextPath().

<script type="text/javascript" src="${pageContext.request.contextPath}/HL.js"></script>

This will end up as (rightclick page in browser, do View Source to see it)

<script type="text/javascript" src="/Open/HL.js"></script>

See also:


Update: as per your update, please note that this does not apply on TLD files since they are been resolved on the server side. Normally, you should drop TLD files in /WEB-INF folder and reference it by uri="/WEB-INF/filename.tld".

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!