title: How to handle ampersand in Java String and send a URL as a query parameter value?
body: <p>I am trying to call a URL from Java code in the following way:</p>
  
  <pre><code>userId = "Ankur";
  template = "HelloAnkur";
  value= "ParamValue";
  String urlString = "https://graph.facebook.com/" + userId + "/notifications?template=" +
      template + "&amp;href=processThis.jsp?param=" + value + "&amp;access_token=abc123";
  </code></pre>
  
  <p>I have the following problems in this:</p>
  
  <ol>
  <li>When I do <code>println(urlString)</code>, I see that the <code>urlString</code> only has the value upto and before the first ampersand (<code>&amp;</code>). That is, it looks as: <code>https://graph.facebook.com/Ankur/notifications?template=HelloAnkur</code> and rest of it all (which should have been <code>&amp;href=processThis.jsp?param=ParamValue&amp;access_toke=abc123</code>) gets cut off. Why is that and how can I get and keep the full value in <code>urlString</code>? Does <code>&amp;</code> needs to be escaped in a Java String, and if yes, how to do it?</li>
  <li>Notice that I am trying to pass a (relative) URL as a parameter value in this query (the value of <code>href</code> as <code>processThis.jsp?param=ParamValue</code>. How can I pass this type of value of <code>href</code> without mixing it up with the query of this URL (<code>urlString</code>), which only has three parameters <code>template</code>, <code>href</code> and <code>access_token</code>? That is, how can I hide or escape <code>?</code> and <code>=</code>? Further, what would I need to do if <code>value</code> was <code>Param Value</code> (with a space)?</li>
  <li>Notice that the <code>template</code> has the value <code>HelloAnkur</code> (with no space). But if I wanted it to have space, as in <code>Hello Ankur</code>, how would I do it? Should I write it as <code>Hello%20Ankur</code> or <code>Hello Ankur</code> would be fine?</li>
  <li>I need the solution in such a way that <code>URL url = new URL(urlString)</code> can be created, or <code>url</code> can be created via <code>URI</code>. Please describe your answer up to this point as creating a safe URL is not straight forward in Java.</li>
  </ol>
  
  <p>Thanks!</p>