Java URL Encoding String Parameters
Java provide URLEncoder to encode parameter which could be send thorugh URL to the server. Important things to note that while you are doing encoding you should encode individual parameter separately then name or value not the entire URL and also not the stirng parameter separator character & nor paramter name-value charater =. Please have below sample Java program for URL encoding:
- URLEncoding.java:
import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; public class URLEncoding { public static void main(String[] args) throws UnsupportedEncodingException { String url = "http://techiworks.com/?s="; String quertyParamter = "encryption and decryption"; String encodeValue = URLEncoder.encode(quertyParamter, "UTF-8"); String decodeValue = URLDecoder.decode(encodeValue, "UTF-8"); String finalURL = url+encodeValue; System.out.println("URL encoded value:"+encodeValue); System.out.println("URL decoded value:"+decodeValue); System.out.println("Final encoded URL: "+finalURL); } }
- Output:
- For more information on URLEncoding please visit Oracle documentation here