
In Java Examples, when using the GET method, parameter names and their values get submitted on the URL string after a question mark. Different parameter name/value pairs are separated by ampersands.
Usually, parameters are accessed from a request in an already decoded format (via request.getParameter()
), so no decoding is necessary.

However, occasionally certain situations arise where you need to decode a string that has been URL encoded (for instance, by the URLEncoder.encode(String s, String encoding)
method or the javascript escape()
function).
Java code:
package crunchify.com.tutorials; import java.net.URLDecoder; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; /** * @author crunchify.com * */ public class CrunchifyEncodeDecodeExample { public static void main(String[] args) throws Exception { String crunchifyValue1 = "This is a simple Example from Crunchify"; // Use StandardCharsets String ecodedValue1 = URLEncoder.encode(crunchifyValue1, StandardCharsets.UTF_8.name()); String decodedValue1 = URLDecoder.decode(ecodedValue1, StandardCharsets.UTF_8.name()); System.out.println("crunchifyValue1 after encoding => " + ecodedValue1); System.out.println("crunchifyValue1 after decoding (Original Value): => " + decodedValue1); String crunchifyValue2 = "Hello There, We started accepting Guest-Posts on Crunchify..."; // Or directly provide UTF-8 String encodedValue2 = URLEncoder.encode(crunchifyValue2, "UTF-8"); String decodedValue2 = URLDecoder.decode(encodedValue2, "UTF-8"); System.out.println("\ncrunchifyValue2 after encoding => " + encodedValue2); System.out.println("crunchifyValue2 after decoding (Original Value) => " + decodedValue2); } }
Other must read:
- Java Tutorial: How to Create RESTful Java Client using Apache HttpClient – Example
- How to use AJAX, jQuery in Spring Web MVC (.jsp) – Example
Console Output:
crunchifyValue1 after encoding => This+is+a+simple+Example+from+Crunchify crunchifyValue1 after decoding (Original Value): => This is a simple Example from Crunchify crunchifyValue2 after encoding => Hello+There%2C+We+started+accepting+Guest-Posts+on+Crunchify... crunchifyValue2 after decoding (Original Value) => Hello There, We started accepting Guest-Posts on Crunchify...
URL encoding is required much more often than URL decoding, since decoding usually takes place automatically during calls the request.getParameter()
.
However, it is good to know that URLDecoder.decode()
exists for the occasional situation where it is needed.
please can you explain the how url encoding and url decoding working in the java platform?
Hi santhoshkumar – URL Encoding means convert special characters from URL with the standard specs which is widely recognize by all systems.
The same goes to URL Decoding – You covert it back to special character once you get a encoded URL.
Hello , Your post is very good, but i couldn’t understand this part…
Can you explain a little more about how encoding url or form parameters we can prevent some attacks like Sql Injection?
If you prefer only post some links that explain this, would be very helpful too!
Sure. I’ll publish a new article on SQL injection soon. Stay tuned and happy coding.
Nice article.
The method encode(String) from the type URLEncoder is deprecated.
Thanks Raviprashanth – updated tutorial above and fixed.. This is correct method:
Good Job!
Still not perfect – try URLEncoder.encode(crunchifyValue1, StandardCharsets.UTF_8.name())
That’s right. Added that method too. Thanks Ben.