ICT 4361 — Java Programming Exercise 7
This exercise familiarizes you with the basic methods of the networking
APIs in Java, and simple tasks such as reading the contents of a URL, and
writing on a socket. This will also exercise your knowledge of Exceptions.
What To Hand In:
Please hand in a listing for each program requested, formatted in an
Ensure your name, and the name of the file is available in a comment at the
top of the file.
Also, ensure that you have a sample of the output from the program.
If your program fails to compile, hand in your error listing as your output.
For each question asked, provide one or two sentences summarizing your
answer. Please be both complete and succinct.
For the last problem, you will want to download hw7.jar
This file contains the class files and source for a server process.
- Create a class called ReadURL which has the following features:
Note that there are exceptions which may be created as a result of these assigments, which you should handle in the constructor
- A private field of type URL which stores a URL
- A constructor which takes a String as a parameter, from which the URL is created
- A constructor which takes a URL as a parameter, from which the URL is assigned
- A constructor which takes a URI as a parameter, assigning to the URL with the toURL() method
Note that Exception handling is necessary in these methods as well.
- A method called getContent() which returns a String, and performs the following:
- Creates and assigns URLConnection by invoking openConnection() on the URL
- Creates an InputStream from the URLConnection
- Creates a BufferedReader on a new InputStreamReader built from the InputStream
- Reads one line from the BufferedReader and stores it as the return value
- Closes the BufferedReader
- Returns the contents of the line
- A method called getContentAsURI which returns a URI,
by calling getContent() and using the line of text to construct a URI for return.
- Create a main method that creates a new ReadURL
from the URL http://www.du.edu/~mschwart/ICT4361/hw7.problem1,
gets its content, and then prints the URL, and its content.
- Run the program
- Create a new class with just a main method to do the following:
- Creates a new ReadURL from the first parameter on the command line, or read from the user
- Uses that ReadURL object to get the contents at that location as a URI
- Converts that URI to a URL (using the toURL method)
- Creates another ReadURL object using that URL
- Uses the new ReadURL object to get the contents at that location as a String
- Prints the first URL, second URL, and the contents of the second URL
- Runs the program for the URL http://www.du.edu/~mschwart/ICT4361/hw7.problem2
- Create a new class called WriteURI with the following features:
- Exception handling is an important part of this exercise
- To run the SimpleServer from the command line in Windows, do the following:
- Open a command prompt window
- Ensure you can run Java by entering the command java -version
- Run the command line java -cp hw7.jar hw7.SimpleServer hw7server.uri
This will make the server run, placing its URI into the file hw7server.uri
- The file hw7.log contains the record of connections to the server
- You may use your ReadURL class to get the server host and port information, or you may
examine the file, and then code its contents into your program
- Note that hw7.jar contains the source for the server, as well as the class files, so
you may examine this if you wish