Skip to content
Advertisement

How to extract the relative url from the absolute url in Java

I have this website:

JavaScript

and I want to extract the relative part out of it:

JavaScript

How do I do that?

EDIT: I was offered an answer to a question, but the problem there was to build the absolute url out of the relative which is not what I’m interested in.

Advertisement

Answer

You could use the getPath() method of the URL object:

JavaScript

Now, this only brings the actual path. If you need more information (the anchor or the parameters passed as get values), you need to call other accessors of the URL object:

JavaScript

A possible use to generate the relative URL without much code, based on Hiru’s answer:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement