Home> Java> javaTutorial> body text

Java URLEncoder

PHPz
Release: 2024-08-30 16:10:02
Original
712 people have browsed it

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
Copy after login

The Syntax flow goes in a way where the parameters are as follows:

  • String st:This argument provides the string as an input that needs to be passed to the function String encode.
  • String enc1:This argument provides the string enc, which is used as a methodology or, say, a procedure for encoding to be used with the string c as an argument for its working.
  • Throws:throws keyword behaves in a similar fashion. It is a function that will be used for catching all the exceptions that may hinder the utility class of URLEncoder of Java. It mandatorily needs if the specified encoding is not used.

How Java URLEncoder Class Works?

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:

  • All the alphanumeric characters and certain special characters being used in the utility class such as ‘*’ ,’ _’,’ –‘ and ‘.’ remains unchanged and un-notified.
  • All extra spaces need to get converted into ‘+’ signs.
  • All the remaining characters within the string or other characters get encoded by individual parsing the encoded string one by one, or it can be more than one byte for encoding any string as per the scheme specified. Then further, these bytes of string get converted into a three-character string with some string format of the form possessing the %xy, where xy represents the encoding string of characters with the hexadecimal format.
  • UTF format is mostly a recommended standard sanctioned by W3C for any kind of encoding strategies related to the string.

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:

  • u@educba for learning:If the encoded value or the character is @, then it is used by UTF-8, which is the most conventional way for accepting the encoded string type, @ symbol will get converted into 40%, and the remaining values with spaces will get converted into + signs which will produce a string as an output, and it will appear somewhat like:
  • u%40educba+for+learning:Methods provided by the class as a utility for this includes only one method to serve the purpose, and that method is defined as encode().
  • encode()is the only method supporting this Java Utility class. As its name suggests, it is the method used for encoding purpose and then returns an encoded string for any specified string. Also, it does not provide the flexibility to serve the encoding procedure smoothly with its operations. Therefore, it has created another method released in the next version, which can even catch hold of all the exceptions and then that encoding feature is the desired output for the utility class.

Examples of Java URLEncoder

Following are the examples of java urlencoder are given below:

Example #1

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); } }
Copy after login

Output:

Java URLEncoder

Example #2

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); } }
Copy after login

Output:

Java URLEncoder

Conclusion

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!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!