How to get Parent URL from the given http request?

Consider there is a web page A. Assume that there are certain URLs in web page A. When the user clicks a particular URL in Web page A, the request will be process in the web server and the corresponding web page will be displayed in the UI. While processing the request in a Servlet, suppose if you want to know the URL of the web page A, which consisted of the URL, you can use the following code to get it.

URL url = new URL(req.getHeader("Referer"));

System.out.println(url.getPath());

The parent url will be present in the HTTP header, with the header name as 'Referrer'.

Search