Java URL

Share

Java URL

In Java, the URL class is part of the java.net package and is used to represent a Uniform Resource Locator (URL). A URL is a reference or address used to locate resources on the internet or within a local network. URLs typically consist of a protocol, hostname (or IP address), port, path, and query parameters.

Here’s how to use the URL class in Java to work with URLs:

  1. Import the URL class:

    First, make sure to import the java.net.URL class at the beginning of your Java source file:

    java
    import java.net.URL; import java.net.MalformedURLException;
  2. Create a URL object:

    You can create a URL object by passing a URL string to its constructor. Handle the MalformedURLException in case the URL string is malformed:

    java
    try { String urlString = "https://www.example.com"; URL url = new URL(urlString); } catch (MalformedURLException e) { e.printStackTrace(); }

    Replace "https://www.example.com" with the URL you want to work with.

  3. Access URL Components:

    You can access various components of the URL using methods provided by the URL class. Here are some common methods:

    • getProtocol(): Returns the protocol (e.g., “http”, “https”).
    • getHost(): Returns the host or hostname (e.g., “www.example.com“).
    • getPort(): Returns the port number (-1 if not specified).
    • getPath(): Returns the path portion of the URL.
    • getQuery(): Returns the query string (if present).
    • getFile(): Returns the file portion of the URL, including the path and query.

    Example:

    java
    String protocol = url.getProtocol(); String host = url.getHost(); int port = url.getPort(); String path = url.getPath(); String query = url.getQuery();
  4. Open a Connection:

    You can use the openConnection() method of the URL object to open a connection to the resource specified by the URL. This is often used for reading from or writing to a URL. Here’s an example using HttpURLConnection:

    java
    import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; try { URL url = new URL("https://www.example.com"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } else { System.out.println("HTTP Error: " + responseCode); } connection.disconnect(); } catch (Exception e) { e.printStackTrace(); }

    In this example, we open an HTTP connection to the specified URL and read its content.

The URL class in Java is a powerful tool for working with URLs, allowing you to parse, construct, and connect to resources on the internet or local network. Depending on your use case, you may need to handle different protocols and perform various operations with the URL.

Demo Day 1 Video:

 
You can find more information about Java in this Java Docs Link

 

Conclusion:

Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on Java Training here – Java Blogs

You can check out our Best in Class Java Training details here – Java Training

💬 Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeeks


Share

Leave a Reply

Your email address will not be published. Required fields are marked *