Contents
Class URLEncoder in Java
URLEncoder class defines a single static method that is used to convert a String to its URL-encoded form. Space is converted to + and nonalphanumeric characters other than underscore are output as two hexadecimal digits following a percentage sign(%).
This technique only works for 8-bit characters. This method is used to determine the canonical value of a URL specification so that the users use only characters from an externally portable subset of ASCII which can be correctly handled by computers around the globe.
The URLEncoder class defines a single static method that converts a String to its URL-encoded form. More precisely, the String is converted to a MIME type called x-www-form-urlencoded.
This is the format used when posting forms on the Web. The algorithm leaves letters, numbers, and the dash (-), underscore ( _ ), period (.), and asterisk (*) characters unchanged. Space characters are converted to plus signs (+). All other characters are encoded with a percent sign (%) followed by the character code represented as a two-digit hexadecimal number. For example, consider the following string:
tech-travel hub
This string gets encoded as:
tech-travel+%hub
The point of the URLEncoder class is to provide a way to canonicalize a string into an extremely portable subset of ASCII that can be handled properly by computers around the world.
The structure of the class URLEncoder is given as :
public class java.net.URLEncoder extends java.lang.Object{
//methods:
public static String encode(String s);
}
public static String encode(String s);
public static String encode(String s) method returns the contents of the String in the x-www-form-urlencoded format.
This method returns a URL-encoded string.
Parameter
s – The string to encode.
Apart from these methods, the URLEncoder class also has inherited methods from class- Object. They are as follows:
- clone()
- finalize()
- hashCode()
- notifyAll()
- wait()
- wait(long, int)
- equals(Object)
- getClass()
- notify()
- toString()
- wait(long)