title: Is there a right way to build a URL?
body: <p>In much of the code I work with there is horrible stuff like:</p>
  
  <pre><code>String url = "../Somewhere/SomeServlet?method=AMethod&amp;id="+object.getSomething()+ "&amp;aParam="+object.getSomethingElse());
  </code></pre>
  
  <p>or - even worse:</p>
  
  <pre><code>String url = "Somewhere/Here/Something.jsp?path="+aFile.toString().replace("\\","/")+ "&amp;aParam="+object.getSomethingElse());
  </code></pre>
  
  <p>Is there a right way to:</p>
  
  <ol>
  <li>Create a new URL (or is it a URI).</li>
  <li>Add correctly escaped parameters to it.</li>
  <li>Add well-formed file paths in those params.</li>
  <li>Resolve it to a String.</li>
  </ol>
  
  <p>Essentially - it is too easy to just build the string than it is to do it properly. Is there a way to do it <em>properly</em> that is as easy as just building the string?</p>
  
  <p><strong>Added</strong></p>
  
  <p>For clarity - and after a little thought - I suppose I am looking for something like:</p>
  
  <pre><code>String s = new MyThing()
      .setPlace("Somewhere/Something.jsp")
      .addParameter(aName,aValue)
      .addParameter(aName,aFile)
      .toString();
  </code></pre>
  
  <p>so that it will deal with all of the unpleasantness of escaping and adding "?"/"&amp;" and changing "\" to "/" instead of using "\" for files etc.</p>
  
  <p>If I have to write one myself (i.e. if Apache is not an option) are there real Java techniques for correctly escaping the various parts. I mean things like escaping " " in parameters as "." while escaping " " in other places a "%20".</p>