Java: How to Encode or Decode URL String Or Form Parameter

Last updated
App Shah
Crunchify » Java and J2EE Tutorials » Java: How to Encode or Decode URL String Or Form Parameter
URL Encode Decode in Java

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.

How to Encode or Decode URL String Or Form Parameter

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:

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.

9 thoughts on “Java: How to Encode or Decode URL String Or Form Parameter”

    • 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.

      Reply
  1. 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!

    Reply

Leave a Comment