0

The construct new URL(new URL(new URL("http://localhost:4567"), "abc"), "def") produces (imho incorrectly) this url: http://localhost:4567/def

While the construct new URL(new URL(new URL("http://localhost:4567"), "abc/"), "def") produces the correct (wanted by me) url: http://localhost:4567/abc/def

The difference is a trailing slash in abc constructor argument.

Is this intended behavior or this is a bug that should be fixed in URL class?
After all the idea is not to worry about slashes when you use some helper class for URL construction.

5
  • Claiming that a well tested class in the SDK has a bug is usually quite a bold statement. Commented Oct 26, 2015 at 17:13
  • I don't claim anything. Just asking for explanation of that behavior. Commented Oct 26, 2015 at 17:25
  • Well, you might want to go through the URL class sources to see if there's anything helpful. Most likely not a bug, even if it doesn't work the way you want it to. Commented Oct 26, 2015 at 17:31
  • @Kayaman Before going to the source code, it's always better to read the javadoc first, since that describes the intended behavior. The result seen here is exactly the behavior described in the javadoc. Commented Oct 26, 2015 at 18:16
  • 1
    @Andreas Going to the source shows you the javadoc as well, in addition to any possible hidden comments in the code ;) Commented Oct 26, 2015 at 18:18

1 Answer 1

3

Quoting javadoc of new URL(URL context, String spec):

Otherwise, the path is treated as a relative path and is appended to the context path, as described in RFC2396.

See section 5 "Relative URI References" of the RFC2396 spec, specifically section 5.2 "Resolving Relative References to Absolute Form", item 6a:

All but the last segment of the base URI's path component is copied to the buffer. In other words, any characters after the last (right-most) slash character, if any, are excluded.

Explanation

On a web page, the "Base URI" is the page address, e.g. http://example.com/path/to/page.html. A relative link, e.g. <a href="page2.html">, must be interpreted as a sibling to the base URI, so page.html is removed, and page2.html is added, resulting in http://example.com/path/to/page2.html, as intended.

The Java URL class implements this logic, and that is why you get what you see, and it is entirely the way it is supposed to work.

It is by design, i.e. not a bug.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.