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

18,299

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

Share:
18,299
user717236
Author by

user717236

Updated on June 04, 2022

Comments

  • user717236
    user717236 almost 2 years

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

  • user717236
    user717236 almost 13 years
    Hey, thank you very much for your help. Unfortunately, it didn't work. I don't think it recognizes Expression Language. Are there requirements to recognize Expression Language in JSP like a version of tomcat? Is request.getContextPath() the equivalent of ${pageContext.request.contextPath}? I went to this page and tried all the methods that start with "get" (download.oracle.com/javaee/6/api/javax/servlet/http/…) and I still have issues. I'll explain it further in an update to my initial question. Thank you.
  • BalusC
    BalusC almost 13 years
    You need to ensure that you're running at least a Servlet 2.4 compatible container (Tomcat 5.5 or newer and so on) and that web.xml is declared conform Servlet 2.4 version. See also the bottom of the JSTL wiki page: stackoverflow.com/tags/jstl/info
  • user717236
    user717236 almost 13 years
    Thank you. Yes, I tried with no luck. I will keep trying but is it something I can put off to solve this or is ${pageContext.request.contextPath} not the same as <% request.getcontextPath(); %> ???
  • BalusC
    BalusC almost 13 years
    As per your question update, you need at least JSP 2.0, not JSP 1.2. Servlet 2.4 comes hand in hand with JSP 2.0. If you really can't get EL in template text to work for some unclear reason (although it should work fine on Tomcat 5.5 with web.xml as Servlet 2.4, probably you've cluttered the /WEB-INF/lib with servletcontainer specific libraries of outdated versions, you should remove them), then you can use <%=request.getContextPath()%> to get the same by scriptlets. Using scriptlets is however discouraged.