The material in this lesson is extremely important. However, there is simply too much material to be covered in detail during lecture periods. Therefore, students in Prof. Baldwin's Advanced Java Programming classes at ACC will be responsible for studying this material on their own, and bringing any questions regarding the material to class for discussion.
It has about eight variables that contain useful information about a connection.
It has about 37 methods that can be used to examine and manipulate an object of the class in a variety of different ways.
If you plan to use the URL class for the higher-lever capabilities that it offers, and you plan to write content handlers and protocol handlers, you will probably need to become very familiar with this class. In that case, you will probably want to get a copy of a good Java networking book, such as Java Network Programming, by Elliotte Rusty Howard, and study the use of this class in depth.
My objective here is simply to make you aware of the existence of the class and its many methods, and to provide some examples of how you can get and use an object of the class to obtain higher-level information about a connection.
The program then uses the URLConnection object to obtain and display some of the "higher level" aspects of the URL:
The program was tested using JDK 1.1.3 under Win95.
The output from the program is a display of:
http://www2.austin.cc.tx.us/baldwin/Test01.html Mon Jan 19 22:56:04 CST 1998 text/html |
A common way to get a URLConnection object is to invoke a method on a URL object that returns an object of a subclass of the URLConnection class. That is the case in the sample program for this lesson.
I will remove all exception handling code from the discussion of this program for brevity.
All of the code in the sample program for this lesson is contained in the main() method of the controlling class.
The first code fragment instantiates a URL object. This is essentially
the same code that we saw in the sample programs in the lesson on the URL
class, but you need to see it again here in order to understand the
code that follows it.
URL url = new URL( "http://www2.austin.cc.tx.us/baldwin/Test01.html"); |
URLConnection urlConnection = url.openConnection(); |
| http://www2.austin.cc.tx.us/baldwin/Test01.html
Mon Jan 19 22:56:04 CST 1998 text/html |
The content type is probably based on the file's extension.
This is the kind of information that you might need in order to make
more effective use of the file pointed to by the URL. Again, there
are many more methods which can be used to obtain other information about
the connection.
System.out.println(urlConnection.getURL()); Date lastModified = new Date( urlConnection.getLastModified()); System.out.println(lastModified); System.out.println(urlConnection.getContentType()); |
/*File Url004.java Copyright 1998, R.G.Baldwin
Revised 01/19/98
Illustrates connecting to a URL and creating a
URLConnection object.
Uses the URL object to obtain and display
the URL, the date last modified, and the content type.
Also uses the URLConnection object to obtain an
input stream object. Then uses this object to read and
display the file.
Computer must be online for this program to run properly.
Otherwise, it will throw an exception of type
UnknownHostException.
Tested using JDK 1.1.3 under Win95.
The output from the program is a display of:
the URL,
the date last modified,
the content type, and
the contents of the file named Test01.html in a text format
As of 01/19/98, the output(with line breaks manually
inserted) was:
http://www2.austin.cc.tx.us/baldwin/Test01.html
Mon Jan 19 22:56:04 CST 1998
text/html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
<META NAME="Author" CONTENT="">
<META NAME="GENERATOR" CONTENT="Mozilla/3.01Gold
(Win95; I) [Netscape]">
</HEAD>
<BODY>
<P><B><I>Richard G Baldwin (512) 223-4758,
<A HREF="mailto:baldwin@austin.cc.tx.us">
baldwin@austin.cc.tx.us</A>,
<A HREF="http://www2.austin.cc.tx.us/baldwin/">
http://www2.austin.cc.tx.us/baldwin/</A></I></B></P>
<H3 ALIGN=CENTER>
<A HREF="http://www2.austin.cc.tx.us/baldwin/">
Test File</A></H3>
<P>This test file is used to test certain network
programming applications.</P>
</BODY>
</HTML>
**********************************************************/
import java.net.*;
import java.io.*;
import java.util.*;
class Url004{
public static void main(String[] args){
String dataLine;
try{
//Get a URL object
URL url = new URL(
"http://www2.austin.cc.tx.us/baldwin/Test01.html");
//Open a connection to the URL and get a
// URLConnection object.
URLConnection urlConnection = url.openConnection();
//Use the connection to get and display the URL
System.out.println(urlConnection.getURL());
//Use the connection to get and display the date last
// modified.
Date lastModified = new Date(
urlConnection.getLastModified());
System.out.println(lastModified);
//Use the connection to get and display the content
// type.
System.out.println(urlConnection.getContentType());
//Use the connection to get an InputStream object.
// Use the InputStream object to instantiate a
// DataInputStream object.
BufferedReader htmlPage =
new BufferedReader(new InputStreamReader(
url.openStream()));
//Use the DataInputStream object to read and display
// the file one line at a time.
while((dataLine = htmlPage.readLine()) != null){
System.out.println(dataLine);
}//end while loop
}//end try
catch(UnknownHostException e){
System.out.println(e);
System.out.println(
"Must be online to run properly.");
}//end catch
catch(MalformedURLException e){System.out.println(e);}
catch(IOException e){System.out.println(e);}
}//end main
}//end class Url004
//=======================================================// |