title: how to fetch base url from the given url using java
body: <p>I am trying to fetch base URL using java. I have used jtidy parser in my code to fetch the title. I am getting the title properly using jtidy, but I am not getting the base url from the given URL.</p>
  
  <p>I have some URL as input:</p>
  
  <pre><code>String s1 = "http://staff.unak.is/andy/GameProgramming0910/new_page_2.htm";
  String s2 = "http://www.complex.com/pop-culture/2011/04/10-hottest-women-in-fast-and-furious-movies";
  </code></pre>
  
  <p>From the first string, I want to fetch <code>"http://staff.unak.is/andy/GameProgramming0910/"</code> as a base URL and from the second string, I want <code>"http://www.complex.com/"</code> as a base URL.</p>
  
  <p>I am using code:</p>
  
  <pre><code>URL url = new URL(s1);
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  InputStream in = conn.getInputStream();
  Document doc = new Tidy().parseDOM(in, null);
  String titleText = doc.getElementsByTagName("title").item(0).getFirstChild()
  .getNodeValue();
  </code></pre>
  
  <p>I am getting <code>titletext</code>, but please can let me know how to get base URL from above given URL?</p>