?
Solved

opening a relative URI from a Java applet

Posted on 2007-10-07
6
Medium Priority
?
987 Views
Last Modified: 2013-12-29
How can a Java applet load a file from the same directory in which its .jar file resides, without knowing the absolute URL?  I.e. what string should I pass to javax.xml.parsers.DocumentBuilder.parse(), documented here:

http://java.sun.com/j2se/1.4.2/docs/api/javax/xml/parsers/DocumentBuilder.html#parse(java.lang.String)

to tell it to load "myfile.svg" from the same directory from which the jar file was loaded?

In HTML I can use relative URLs, i.e. from "a.html" I can hyperlink to "b.html" without giving the full absolute URL, and the browser will know to use the "b.html" file in the same dir as the referring "a.html".   How do I get the equivalent of such relative addressing from inside a Java applet?

Thanks for help!
0
Comment
Question by:ilya239
  • 3
  • 3
6 Comments
 
LVL 8

Expert Comment

by:malfunction84
ID: 20030478
Since the applet is running client-side, it doesn't know anything about where it's being hosted unless you tell it.  You're going to have to pass the base path to the applet as a <param>.
0
 

Author Comment

by:ilya239
ID: 20030612
So, there is no such thing as a "relative URI" from within the applet?  In the HTML example, "a.html" does not know where it's loaded either, but the relative link in it to "b.html" is interpreted automatically to mean "the b.html in the same dir as the referring a.html".  There is nothing corresponding for applets?
0
 
LVL 8

Accepted Solution

by:
malfunction84 earned 2000 total points
ID: 20031125
absolute_URL = base_path + relative_URL

With HTML, the browser resolves relative URLs.  Since the browser knows the base path, it can resolve a relative URL into an absolute URL.

With Java applets, the applet does *not* know the base path because the JVM is actually separate from the browser.  While it's true that the applet is embedded into the webpage, there is a level of separation between them.  The only way to get custom information from the browser to the applet is via <param> elements.

However, I had forgotten about the Applet.getDocumentBase() method.
http://java.sun.com/j2se/1.4.2/docs/api/java/applet/Applet.html#getDocumentBase()
While the applet does not know the base path, it does know the URL of the page in which it has been embedded.  From this, you can determine the base path and resolve relative URLs into absolute URLs yourself.

In your Applet's source code, add this method:

private URL resolveAbsoluteURL(String relativeUrl) throws MalformedURLException {
    String baseUrl = this.getDocumentBase().toString();
    String basePath = baseUrl.substring(0, baseUrl.lastIndexOf("/"));
    // absolute url = base path + relative url
    return new URL(basePath + "/" + relativeUrl);
}
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ilya239
ID: 20031172
Thanks!  What happens when the web page with the applet is loaded from the local file system (i.e. using the File | Open file... command in Mozilla)?   In that situation, is it possible for the applet to load a file from the same directory as the webpage and the applet?   I understand that in general, an applet needs to be signed to be able to access the local file system.  But for the special case when the applet is loading a file from the same dir from which both the applet and its html page got loaded, is it different?

Thanks for help!
0
 
LVL 8

Expert Comment

by:malfunction84
ID: 20031183
I believe that will also work.  URLs can reference both local files ("file://...") and remote files ("http://...").
0
 

Author Comment

by:ilya239
ID: 20034729
Thanks a lot, that worked!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month13 days, 13 hours left to enroll

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question