All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Encode URL In Java

Last Updated : Mar 11, 2024

How To Encode URL In Java

In this article we will show you the solution on how to encode url in java, Java URLEncoder is a class used to encode URLs (Uniform Resource Locators). To maintain trustworthiness and security, a URL is encoded.

The form parameters and their values are attached to the URL after the '?' symbol whenever a user uses the get method to access a specific site.

Nevertheless, special characters that aren't interpreted pose a problem when they are used in the values.

As a general rule, HTML handles the encoding part automatically by converting the unique characters into the ones that are allowed in order to handle all operations smoothly.

In spite of this, HTML doesn't always confirm the accurate encoding, so we can't rely entirely on HTML features, and use Java's URL Encoder class to encode URLs explicitly.

We will now discuss the idea of how to encode url in java with an example.

Step By Step Guide On How To Encode URL In Java :-

import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class Talkerscode UrlEncoder
{
    public static void main(String[] args) throws MalformedURLException, UnsupportedEncodingException
    {
        String baseUrl = "https://www.talkerscode.com/";
        // String to be encoded as requested by the user
        String query = " u@Talkerscode";
        System.out.println(" URL without any encoding : ") ;
        // creating an object of the URL class
        URL url = new URL(baseUrl + query) ;
        System.out.println( url ) ;
        // using the encode( ) method to encode the URL
        System.out.println( " URL after encoding : " ) ;
        url = new URL( baseUrl + URLEncoder.encode( query, "UTF-8" ) ) ;
        System.out.println( url ) ;
    }
  }
  1. As a first step, we import the required libraries - java.io.UnsupportedEncodingException and java.net.MalformedURLException, java.net.URL, and java.net.URLEncoder.
  2. The Talkerscode in our class The URL encoding method is specified by UrlEncoder.
  3. The primary function of this class throws an UnsupportedEncodingException and MalformedURLException.
  4. The basic URL of the website we are encoding and a string "query" that we want to encode are both defined at the outset.
  5. Using the original URL without encoding is the most efficient method.
  6. Our query is used to construct objects of the URL class based on the baseUrl.
  7. The URL object printed here is the URL object.
  8. A URI encoded with UTF-8 encoding is created by encoding the query string and then adding it to a base URL.
  9. The URL object is created immediately after the URL string has been encoded.
  10. In order to print the URL object, we encode the query string and print it now.

Conclusion :-

As a result, we have successfully learned how to encode url in java with an example.

The purpose of this article was to ensure that data can be transferred and interpreted correctly by encoding and decoding it.

The method can be used to encode and decode the values of URI query parameters as well as HTML form parameters.

I hope this article on how to encode url in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪