Java URL Processing
w3hello.com logo
Java Basics
Java Introduction
Java Installation
Java Operators
Java Variable Types
Java Loops
Java For Loop
Java While Loop
Java do while loop
Java Continue
Java JButton
Java JTextArea
Java Logical Operators
switch statement in java
Java JPasswordField
Java Year
Java Constructor
Java Basic Syntax
Java JComboBox
Java JToolBar
Java if Statements
Java JMenuBar, JMenu and JMenuItem
Java JDialog
Java JToggleButton
Java JFrame
Java Zip Exceptions
Java DIP - Enhancing Image Brightness
Java JFileChooser
Java Concurrency ExecutorService Interface
Java throw exception
LinkedHashMap in Java
Java Digital Watch
Java instanceof
Java AWT Button
Java For Complete Beginners - loops and arrays
Array vs ArrayList in Java
java.util.Calendar
Java JColorChooser
Java URL Processing
Guide to Java Exception Hierarchy
Java Concurrency ConcurrentMap Interface
Synchronization in Java
Java Internationalization
Java Fetch Image from Oracle Database
Unicode System in Java
Java ActionListener
java.lang.reflect - Interfaces
Java Thread Pool | Thread Pooling in Java
Java Graphics in Applet
Java Convert int to long
Java JSpinner
Java Primitive data type
Java Numeric Literals with Underscore
Inter-thread communication in Java
The Tetris game in Java Swing
Java Applet Basics
Java Convert Binary to Decimal
Static keyword in Java
Java Convert Hex to Decimal
Java JRadioButton
Java XML Overview
Java Concurrency Quick Guide
Java Month enum
Types of Exceptions
Reentrant Monitor in Java
Java Loop Control
Java Access Modifiers
WebSocket in Jetty
Transaction Management in Java JDBC
Java SimpleDateFormat
Java 7 JDBC Improvement
Java FlowLayout
Java AWT Scrollbar
Java Decision Making
Java Database Connectivity with Oracle
Java ZoneId
Java else-if Statements
Java DIP - Laplacian Operator
Top Java Books
Java Date Time - Custom Date Format Patterns
Java static and this Keyword
Java JList
Java Concurrency Lock Interface
Java LocalDate
Creating a thread in Java
Jetty's HTTPClient
Java Package
Displaying image in Applet
Java Overriding
Java AWT Choice
Java SAX Parser Overview
Java Save File in Oracle Database
Java Modifier
Java JDesktopPane
Java Inheritance & Polymorphism
Discuss Java Concurrency
this keyword in java
Java KeyListener
Java Online Training
Java iText
History of Java
Java Basics
Previous : Java JColorChooser Next : Guide to Java Exception Hierarchy

Java URL Processing


URL stands for Uniform Resource Locator and represents a resource on the World Wide Web, such as a Web page or FTP directory.

This section shows you how to write Java programs that communicate with a URL. A URL can be broken down into parts, as follows −

protocol://host:port/path?query#ref

Examples of protocols include HTTP, HTTPS, FTP, and File. The path is also referred to as the filename, and the host is also called the authority.

The following is a URL to a web page whose protocol is HTTP −

https://www.amrood.com/index.htm?language=en#j2se

Notice that this URL does not specify a port, in which case the default port for the protocol is used. With HTTP, the default port is 80.

URL Class Methods

The java.net.URL class represents a URL and has a complete set of methods to manipulate URL in Java.

The URL class has several constructors for creating URLs, including the following −

Sr.No. Method & Description
1

public URL(String protocol, String host, int port, String file) throws MalformedURLException

Creates a URL by putting together the given parts.

2

public URL(String protocol, String host, String file) throws MalformedURLException

Identical to the previous constructor, except that the default port for the given protocol is used.

3

public URL(String url) throws MalformedURLException

Creates a URL from the given String.

4

public URL(URL context, String url) throws MalformedURLException

Creates a URL by parsing together the URL and String arguments.

The URL class contains many methods for accessing the various parts of the URL being represented. Some of the methods in the URL class include the following −

Sr.No. Method & Description
1

public String getPath()

Returns the path of the URL.

2

public String getQuery()

Returns the query part of the URL.

3

public String getAuthority()

Returns the authority of the URL.

4

public int getPort()

Returns the port of the URL.

5

public int getDefaultPort()

Returns the default port for the protocol of the URL.

6

public String getProtocol()

Returns the protocol of the URL.

7

public String getHost()

Returns the host of the URL.

8

public String getHost()

Returns the host of the URL.

9

public String getFile()

Returns the filename of the URL.

10

public String getRef()

Returns the reference part of the URL.

11

public URLConnection openConnection() throws IOException

Opens a connection to the URL, allowing a client to communicate with the resource.

Example

The following URLDemo program demonstrates the various parts of a URL. A URL is entered on the command line, and the URLDemo program outputs each part of the given URL.

Live Demo
// File Name : URLDemo.java
import java.net.*;
import java.io.*;

public class URLDemo {

   public static void main(String [] args) {
      try {
         URL url = new
URL("https://www.amrood.com/index.htm?language=en#j2se");
         
         System.out.println("URL is " + url.toString());
         System.out.println("protocol is " + url.getProtocol());
         System.out.println("authority is " + url.getAuthority());
         System.out.println("file name is " + url.getFile());
         System.out.println("host is " + url.getHost());
         System.out.println("path is " + url.getPath());
         System.out.println("port is " + url.getPort());
         System.out.println("default port is " +
url.getDefaultPort());
         System.out.println("query is " + url.getQuery());
         System.out.println("ref is " + url.getRef());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

A sample run of the this program will produce the following result −

Output

URL is https://www.amrood.com/index.htm?language=en#j2se
protocol is http
authority is www.amrood.com
file name is /index.htm?language=en
host is www.amrood.com
path is /index.htm
port is -1
default port is 80
query is language=en
ref is j2se

URLConnections Class Methods

The openConnection() method returns a java.net.URLConnection, an abstract class whose subclasses represent the various types of URL connections.

For example −

  • If you connect to a URL whose protocol is HTTP, the openConnection() method returns an HttpURLConnection object.

  • If you connect to a URL that represents a JAR file, the openConnection() method returns a JarURLConnection object, etc.

The URLConnection class has many methods for setting or determining information about the connection, including the following −

Sr.No. Method & Description
1

Object getContent()

Retrieves the contents of this URL connection.

2

Object getContent(Class[] classes)

Retrieves the contents of this URL connection.

3

String getContentEncoding()

Returns the value of the content-encoding header field.

4

int getContentLength()

Returns the value of the content-length header field.

5

String getContentType()

Returns the value of the content-type header field.

6

int getLastModified()

Returns the value of the last-modified header field.

7

long getExpiration()

Returns the value of the expired header field.

8

long getIfModifiedSince()

Returns the value of this object's ifModifiedSince field.

9

public void setDoInput(boolean input)

Passes in true to denote that the connection will be used for input. The default value is true because clients typically read from a URLConnection.

10

public void setDoOutput(boolean output)

Passes in true to denote that the connection will be used for output. The default value is false because many types of URLs do not support being written to.

11

public InputStream getInputStream() throws IOException

Returns the input stream of the URL connection for reading from the resource.

12

public OutputStream getOutputStream() throws IOException

Returns the output stream of the URL connection for writing to the resource.

13

public URL getURL()

Returns the URL that this URLConnection object is connected to.

Example

The following URLConnectionDemo program connects to a URL entered from the command line.

If the URL represents an HTTP resource, the connection is cast to HttpURLConnection, and the data in the resource is read one line at a time.

// File Name : URLConnDemo.java
import java.net.*;
import java.io.*;

public class URLConnDemo {

   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.amrood.com");
         URLConnection urlConnection = url.openConnection();
         HttpURLConnection connection = null;
         if(urlConnection instanceof HttpURLConnection) {
            connection = (HttpURLConnection) urlConnection;
         }else {
            System.out.println("Please enter an HTTP URL.");
            return;
         }
         
         BufferedReader in = new BufferedReader(
            new InputStreamReader(connection.getInputStream()));
         String urlString = "";
         String current;
         
         while((current = in.readLine()) != null) {
            urlString += current;
         }
         System.out.println(urlString);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

A sample run of this program will produce the following result −

Output

$ java URLConnDemo

.....a complete HTML content of home page of amrood.com.....
Previous : Java JColorChooser Next : Guide to Java Exception Hierarchy
© Copyright 2018 W3Hello Publishing Limited. All rights reserved.
Contact us | Sitemap