URL.URL

URL.URL

Class Overview | Class Members | This Package | All Packages

Syntax 1
public URL( String protocol, String host, int port, String file ) throws MalformedURLException
Parameters
protocol
the name of the protocol.
host
the name of the host.
port
the port number.
file
the host file.
Description
Creates a URL object from the specified protocol, host, port number, and file. Specifying a port number of -1 indicates that the URL should use the default port for the protocol.

If this is the first URL object being created with the specified protocol, a stream protocol handler object, an instance of class URLStreamHandler, is created for that protocol:

  1. If the application has previously set up an instance of URLStreamHandlerFactory as the stream handler factory, then the createURLStreamHandler method of that instance is called with the protocol string as an argument to create the stream protocol handler.
  2. If no URLStreamHandlerFactory has yet been set up, or if the factory's createURLStreamHandler method returns null, then the constructor finds the value of the system property:
      java.handler.protol.pkgs
    If the value of that system property is not null, it is interpreted as a list of packages separated by a vertical slash character '|'. The constructor tries to load the class named:
      <package>.<protocol>.Handler
    where <package> is replaced by the name of the package and <protocol> is replaced by the name of the protocol. If this class does not exist, or if the class exists but it is not a subclass of URLStreamHandler, then the next package in the list is tried.
  3. If the previous step fails to find a protocol handler, then the constructor tries to load the class named:
      sun.net.www.protocol.<protocol>.Handler
    If this class does not exist, or if the class exists but it is not a subclass of URLStreamHandler, then a MalformedURLException is thrown.

Exceptions
MalformedURLException if an unknown protocol is specified.
See Also
getProperty, setURLStreamHandlerFactory, URLStreamHandler, createURLStreamHandler



Syntax 2
public URL( String protocol, String host, String file ) throws MalformedURLException
Parameters
protocol
the protocol to use.
host
the host to connect to.
file
the file on that host.
Description
Creates an absolute URL from the specified protocol name, host name, and file name. The default port for the specified protocol is used.

This method is equivalent to calling the four-argument constructor with the arguments being protocol, host, -1, and file.

Exceptions
MalformedURLException if an unknown protocol is specified.
See Also
URL



Syntax 3
public URL( String spec ) throws MalformedURLException
Parameters
spec
the String to parse as a URL.
Description
Creates a URL object from the String representation.

This constructor is equivalent to a call to the two-argument constructor with a null first argument.

Exceptions
MalformedURLException If the string specifies an unknown protocol.
See Also
URL



Syntax 4
public URL( URL context, String spec ) throws MalformedURLException
Parameters
context
the context in which to parse the specification.
spec
a String representation of a URL.
Description
Creates a URL by parsing the specification spec within a specified context. If the context argument is not null and the spec argument is a partial URL specification, then any of the strings missing components are inherited from the context argument.

The specification given by the String argument is parsed to determine if it specifies a protocol. If the String contains an ASCII colon ':' character before the first occurrence of an ASCII slash character '/', then the characters before the colon comprise the protocol.

The constructor then searches for an appropriate stream protocol handler of type URLStreamHandler as outlined for:

The stream protocol handler's parseURL method is called to parse the remaining fields of the specification that override any defaults set by the context argument.

Exceptions
MalformedURLException if no protocol is specified, or an unknown protocol is found.
See Also
URL, URLStreamHandler, parseURL