Programming Tips - Java: Encode a string so its safe to use in a URL

Date: 2013dec21 Language: Java Q. Java: Encode a string so its safe to use in a URL A.
import java.net.URLEncoder; import java.io.UnsupportedEncodingException; String encodeForUrl(final String param) { try { return URLEncoder.encode(param, "UTF-8"); } catch (UnsupportedEncodingException e) { return ""; } } // Example use: String url = "http://example.com?param=" + encodeForUrl(param);