Lesson 2 : URL

URL means Uniform Resource Locator.
URLs are used to reference (to point) a resource on the web.
 

An URL is composed of differents parts. An URL is generally composed of the protocol type, host name (and optionaly its communication port used) and finaly the resource name.
Here a examples of URLs :
    http://jerome.jouvie.free.fr/Forum/index.php
    ftp://localhost:8765/

  Protocol Host name Communication port Resource name
First URL http jerome.jouvie.free.fr 80 /Forum/index.php
Second URL ftp localhost 8765  

Here are some particular URLs :
    http://jerome.jouvie.free.fr/index.php?pseudo=jouvieje&mail=jerome.jouvie@gmail.com
    http://jerome.jouvie.free.fr/Java/Network/Lessons/Lesson1.php#Socket

? is used to pass parameter to a page (the example pass the pseudo and an email through the web page) or make a query.
# is used to specify a mark on a page.
 

URLs are very usefull to access resources on the net.

In the previous lesson, we have seen how to communicates with two or more computers using socket.
If you want to access a resources on the web, you don't need a too complicated things. You can access directly to the resource using its URL.
 

To access a resource using its URl, start wil creatin the URL with one of the constructor of java.net.URL classe.
Use url.openStream() method to open an InputStream then read in it (using for example a BufferedReader or BufferedInputStream).
 

Socket are shown in Lesson 1.

First, you need to connect to the host of the resource on the port 80. It will create a socket on the client side (See Lesson 1).
To access a resource, you need to make a request to obtain the resource you want.
Use and send the HTTP request GET.
    GET resourceName HTTP/1.1
Replace resourceName by relative path of the file.
After this request, read in the socket to obtain datas of the resource requested.

Here is a little examples. Suppose you want to access to my first page (http://jerome.jouvie.free.fr/index.php).
The host name should be :
    jerome.jouvie.free.fr
and the HTTP request should be :
    GET /index.php HTTP/1.1
 

OPTIONS : request for informations on the communications options to know the server capabilities.
GET     : request to retreive informations from the server.
HEAD    : identical to GET but request only the head of the resource.
POST    : send datas through a ressource at the specified URL.
PUT     : send datas at a specified URL.
DELETE  : request that the server delete a resource.
TRACE   : ?
CONNECT : ?

For more informations about HTTP 1.1, see RFC 2616.
 

A path of a file can be represented in its absolute or relative form.
All the previous URLs are represented in their absolute form that is to say that the path contains its complete location.
Relative form does not contains the entire path of a file. The path is expressed relatively to a reference path (can be the current directory, the host name and so ... ).
So, /Forum/index.php is the path of the file index.php relatively to the host jerome.jouvie.free.fr.
 

Previous Lesson

Back

Next Lesson

Last modified on 01/07/2010
Copyright © 2004-2010 Jérôme JOUVIE - All rights reserved. http://jerome.jouvie.free.fr/