Java URLEncoder is a utility class that supports for HTML form encoding. The form of HTML becomes more reliable and stable using the URLEncoder class utility of Java. Whenever the user calls a get method, the encoder appends the special characters, values, and parameters at the end of the URL, which makes the URL unauthenticated in a way. Also, the value makes use of special characters, which further uses only HTML to perform smooth handling of all the operations. It is not at all a good practice to rely completely on HTML when Java URLEncoder is existing with its defined utility class.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
public static String encode(String st, String enc1)throws UnsupportedEncodingException
The Syntax flow goes in a way where the parameters are as follows:
URLEncoder is a utility of a Java class that is used for any HTML encoding. Using HTML encoding every time, even for small and simplified method calling, is an unwanted activity when the presence of a Java utility class of URLEncoder works smoothly to enhance the type of activity for string conversion.
It Is considered one of the safest and reliable utility class when it comes to the string and its conversion from the character parsing and encoding, decoding the special string factors. Also, it makes use of the inbuilt feature, which is extensively used to convert the string into the necessary format and then some code of conduct or rules applied to that string at the time of encoding the string using URLEncoder, which is as follows:
An example will clarify the need for UTF format standard to be followed for encoding of the string, which means if we have some parameters or values which contains some values as special characters and spaces are demonstrated with an example:
Following are the examples of java urlencoder are given below:
This program is used to illustrate the URLEncoder utility of Java where the input string is given as the base url for the link and then a string query for retrieving the final string using UTF-8 as a conventional standard for encoding. Output is shown where one encoded string is without URL and the other with UTF-8 standard, which comprises the URL.
Code:
import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; public class UrlEncoderJava { public static void main(String[] args) throws MalformedURLException, UnsupportedEncodingException { String baseurl = "https://www.educba.com/?q="; String query = "u@educba for educba"; System.out.println("Without encoding URL :"); URL url = new URL(baseurl + query); System.out.println(url); System.out.println("URL after encoding :"); url = new URL(baseurl + URLEncoder.encode(query, "UTF-8")); System.out.println(url); } }
Output:
This program is used to represent the encoded string which makes use of the standard Charsets of the UTF_8 to the string and then provides the entire encoded string as shown in the output after converting the URL link with the defined standard and Encoder class of java.
Code:
import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.io.UnsupportedEncodingException; public class URLEncodingParsing { private static String encodingOfValue(String value) { try { return URLEncoder.encode(value, StandardCharsets.UTF_8.toString()); } catch (UnsupportedEncodingException ex) { throw new RuntimeException(ex.getCause()); } } public static void main(String[] args) { String baseUrl = "https://www.educba.com/search?q="; String query = "educba@Java@lang"; String encodedQuery = encodingOfValue(query); String completeUrl = baseUrl + encodedQuery; System.out.println(completeUrl); } }
Output:
URLEncoder in java is a utility class that provides aid for the HTML related forms to encode the special characters being provided for parsing. The UTF-8 standard recommended by W#C has enhanced the overall encoding method for encoding and conversion of the encoded string to the final string after parsing. Using this class for string encoding is a very reliable and secured form of coding; as always, making HTML is not preferred.
The above is the detailed content of Java URLEncoder. For more information, please follow other related articles on the PHP Chinese website!