Latest web development tutorials

Java URL Processing

URL (Uniform Resource Locator) Chinese called a Uniform Resource Locator, it is sometimes known as a Web page address. He expressed as a resource on the Internet, such as Web or FTP address.

This chapter we will introduce Java is treated as a URL. URL can be divided into the following sections.

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

protocols (protocol) can be HTTP, HTTPS, FTP, and File. port is the port number. path for the file path and file name.

URL Examples HTTP protocol as follows:

http://www.w3cschool.cc/index.html?language=cn#j2se

Examples of the above URL does not specify a port, because the HTTP protocol default port number is 80.


URL class methods

In the java.net package defines the URL class that used to deal with content related to the URL. To create and use the URL class, the following were introduced.

java.net.URL provides a way to build wealth URL, and can access resources through java.net.URL.

No. Method Description
1 public URL (String protocol, String host , int port, String file) throws MalformedURLException.
Create a URL with the given parameters (protocol, host name, port number, file name).
2 public URL (String protocol, String host , String file) throws MalformedURLException
Using the specified protocol, host name, file name creation URL, port protocol default port.
3 public URL (String url) throws MalformedURLException
Create a URL string by the given URL
4 public URL (URL context, String url ) throws MalformedURLException
Use base address and create a relative URL

URL class contains many methods for accessing the various parts of the URL, the specific methods and described as follows:

No. Method Description
1 public String getPath ()
Returns URL path section.
2 public String getQuery ()
Returns URL query part.
3 public String getAuthority ()
Gets the authority part of this URL.
4 public int getPort ()
Returns URL port section
5 public int getDefaultPort ()
Returns the default port number for the protocol.
6 public String getProtocol ()
Returns the URL protocol
7 public String getHost ()
Returns the URL of the host
8 public String getFile ()
Returns the URL of the file name
9 public String getRef ()
Being the anchor of this URL (also called "reference").
10 public URLConnection openConnection () throws IOException
Open a URL connection, and run the client to access the resource.

Examples

The above examples demonstrate the use of java.net URL class to get the various parts of the URL parameters:

// 文件名 : URLDemo.java

import java.net.*;
import java.io.*;

public class URLDemo
{
   public static void main(String [] args)
   {
      try
      {
         URL url = new URL("../../www.w3cschool.cc/index.html-language=cn#j2se.html");
         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();
      }
   }
}

The above examples compiled results are as follows:

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

URLConnections class methods

openConnection () returns a java.net.URLConnection.

E.g:

  • URL If you connect the HTTP protocol, openConnection () method returns HttpURLConnection object.

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

  • and many more...

URLConnection methods listed below:

No. Method Description
1 Object getContent ()
URL link to retrieve content
2 Object getContent (Class [] classes)
URL link to retrieve content
3 String getContentEncoding ()
Returns content-encoding header field value.
4 int getContentLength ()
Return content-length header field value
5 String getContentType ()
Returns the content-type header field value
6 int getLastModified ()
Returns the last-modified header field value.
7 long getExpiration ()
Back expires header field values.
8 long getIfModifiedSince ()
IfModifiedSince returned object field values.
9 public void setDoInput (boolean input)
URL connection can be used for input and / or output. If you intend to use the URL connection for input, then DoInput flag is set to true; if you do not intend to use is set to false. The default value is true.
10 public void setDoOutput (boolean output)
URL connection can be used for input and / or output. If you intend to use the URL connection for output, then DoOutput flag is set to true; if you do not intend to use is set to false. The default value is false.
11 public InputStream getInputStream () throws IOException
Returns an input stream URL for reading the resource
12 public OutputStream getOutputStream () throws IOException
Returns the URL of the output stream for writing resources.
13 public URL getURL ()
Returns URL URLConnection objects connected

Examples

The following example uses the HTTP protocol URL. openConnection return HttpURLConnection object.

// 文件名 : URLConnDemo.java

import java.net.*;
import java.io.*;
public class URLConnDemo
{
   public static void main(String [] args)
   {
      try
      {
         URL url = new URL("../../www.w3cschool.cc/index.html");
         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();
      }
   }
}

The above examples compiled results are as follows:

$ java URLConnDemo

.....a complete HTML content of home page of amrood.com.....