URLEncoder class in Java

java.net.URLEncoder

Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. For more information about HTML form encoding, consult the HTML specification.

When encoding a String, the following rules apply:

1. The alphanumeric characters "a" through"z", "A" through"Z" and "0" through "9" remain the same.

2. The special characters ".","-", "*", and"_" remain the same.

3. The space character "   " is converted into a plus sign "+".

3. All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string"%xy", where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used.


Methods

1. URLEncoder.encode(String s, Charset charset)

String java.net.URLEncoder.encode(String s, Charset charset)

This method takes two arguments. This method translates a string into an application/x-www-form-urlencoded format using a specific Charset. This method uses the supplied charset to obtain the bytes for unsafe characters.

Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilities.

Parameters: Two parameters are required for this method.

s: String to be translated.

charset: the given charset.

Returns: the translated String.

Throws:

1. NullPointerException - if s or charset is null.

Approach 1: When no exception

Java

package com.URLEncoder;

import java.net.URLEncoder;
import java.nio.charset.Charset;

public class URLEncoderencode {
    public static void main(String[] args) {

        String s = "hello";
        System.out.println(URLEncoder.encode(s, Charset.defaultCharset()));
    }
}

Output:

hello


Approach 2: NullPointerException 

Java

package com.URLEncoder;

import java.net.URLEncoder;
import java.nio.charset.Charset;

public class URLEncoderencode {
    public static void main(String[] args) {

        String s = null;
        System.out.println(URLEncoder.encode(s, Charset.defaultCharset()));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null at java.base/java.net.URLEncoder.encode(URLEncoder.java:224) at com.URLEncoder.URLEncoderencode.main(URLEncoderencode.java:10)


2. URLEncoder.encode(String s, String enc)

String java.net.URLEncoder.encode(String s, String enc) throws UnsupportedEncodingException

This method takes two arguments. This method translates a string into an application/x-www-form-urlencoded format using a specific encoding scheme.

Parameters: Two parameters are required for this method.

s: String to be translated.

enc: The name of a supported character encoding.

Returns: the translated String.

Throws:

1. UnsupportedEncodingException - If the named encoding is not supported.

Approach 1: When no exception

Java

package com.URLEncoder;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class URLEncoderencode2 {
    public static void main(String[] args) throws UnsupportedEncodingException {

        String s = "hello", enc = "UTF-8";
        System.out.println(URLEncoder.encode(s, enc));
    }
}

Output:

hello


Approach 2: UnsupportedEncodingException

Java

package com.URLEncoder;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class URLEncoderencode2 {
    public static void main(String[] args) throws UnsupportedEncodingException {

        String s = "hello", enc = "UTF199191991";
        System.out.println(URLEncoder.encode(s, enc));
    }
}

Output:

Exception in thread "main" java.io.UnsupportedEncodingException: UTF199191991 at java.base/java.net.URLEncoder.encode(URLEncoder.java:198) at com.URLEncoder.URLEncoderencode2.main(URLEncoderencode2.java:10)


No comments:

Post a Comment