开发者

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

开发者 https://www.devze.com 2023-03-11 16:08 出处:网络
I have a file called Main.jsp, located at the absolute url path of \"http://Mywebpage.com/Open/This/Folder/Main.jsp\".

I have a file called Main.jsp, located at the absolute url path of "http://Mywebpage.com/Open/This/Folder/Main.jsp".

Inside Main.jsp, there is a jsp include:

<%@ include file="../../Top.jsp" %>

Now, inside the Top.jsp page, I have other jsp and javascript statements which reference files:

<%@ taglib uri="emonogram.tld" prefix="em" %>
...
<script type="text/javascript" src="HL.js"></script>

emonogram.tld and HL.js are stored in the same directory as Top.jsp, i.e. "http://Mywebpage.com/Open/".

I need Top.jsp to be flexible enough, such that any file can reference it, no matter where it is in the directory tree. The problem here is I'm getting errors because the files referenced in Top.jsp cannot be found. Why? The jsp include path will be the relative path of Main.jsp. So, Top.jsp will fail because, when I'm calling emonogram.tld, I want "http://Mywebpage.com/Open/emonogram.tld", but it is actually "http://Mywebpage.com/Open/This/Folder/emonogram.tld".

I tried looking at some jsp options such as getRequestURL, getServletPath, getRealPath, and getContextPath, but those methods don't seem to return what I want.

My current logic is to to retrieve the relative path of Top.jsp and prepend that to emonogram.tld and HL.js, respectively. But I don't know how to do that; I'm trying to figure that out.


Update: as per the answer of BalusC, I have tags installed and Tomcat 5.5. I followed the link and web.xml contains the appropriate info. I even updated to JSP 1.2 and nothing. If the ${} is just the equivalent of doing it inside <% %> tags, then it's not a make or break situation and I can worry about it after I get the original question working. But thank you.

I followed this link and tried all the methods star开发者_C百科ting with "get..." and none of them seemed to do the trick.

getContextPath() gives me /Open in BOTH Top.jsp and Main.jsp, even though Main.jsp is in /Open/This/Folder/. This problem is affecting loading "emonogram.tld", which is a tag library, not just javascript files, unfortunately.

Thank you again.

Update2: My apologies, big spelling error; I meant JSTL 1.2, NOT JSP 1.2. I am at Tomcat 5.5.28, JSP 2.0, and JSTL 1.2.

Thank you for your help and knowledge, by the way. It is the same, that is good, even though scriptlets are discouraged. With getcontextPath(), I'm expecting /Open/This/Folder/ for Main.jsp and /Open/ for Top.jsp but it returns /Open/ for both files, which is quite strange. I'll continue investigating and hopefully arrive at a soltuion, thank you again.


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:

  • How to use relative paths without including the context root name?
  • Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP

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".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号