CS 051 | Fall 2011 |
Java gives us a class to handle this called JFileChooser. The following code demonstrates
how this object is to be used in code to open a file:
   JFileChooser dialog = new JFileChooser();
   if(dialog.showOpenDialog(text) == JFileChooser.APPROVE_OPTION) {
     file = dialog.getSelectedFile();
     // code to process file is omitted
   }
Selecting a file to save to is similar, except that showSaveDialog(text) is called instead
of showOpenDialog(text).
The class example
TextEditor shows a full example that can load from and save to a file.
A java URL object will allow us to read web pages from over the Internet. A URL
object can be constructed using new URL (urlString) when urlString referes to
an absolute web address, or new URL(currentUrl, urlString) when we have a current URL
object, and a relative web address given by urlString.
The set up of the Stream to handle the URL is slightly different than that for
reading files:
   URL url = new URL(urlString);
   BufferedReader pageReader = new BufferedReader(new InputStreamReader(url.openStream()));
However, once we have the BufferedReader, the rest of the code is almost identical to
reading from a file.
The class example HTMLLinkFinderBufferedReader shows a full example.
Also in the class example is the second method for dealing with checked exceptions. In the code,
we used a separate, private helper method to read in the web page information. We didn't want
to handle the IOExceptions in the helper method. Instead, we preferred to handle them
in the method that called the helper method. Java allows us to say that if any exceptions occur
in a method, that method will explicitly throw them to the caller.
In our example, the method getWebPage(String urlString) does this in the method header
as follows:
   private String getWebPage(String urlString) throws MalformedURLException, IOException {
Now, let's talk a bit about how networking programs work. When we write a networking program
like the HTMLLinkFinder, our program is communicating with another program over the network.
Our program is a client. The other program is a server. In the case of
HTMLLinkFinder, we are communicating with a Web server.
Let's talk about how a Web client/server pair works a bit more. First, we'll look at URLs and
understand what they mean better. A simple URL may look like:
http://www.pomona.edu
A more complicated URL is:
http://www.pomona.edu:80/events/news/home.shtml
What are all these pieces?
To connect to a server, we need to identify the machine and the port to use. Once connected,
the protocol defines a set of commands that we can use to talk to the server. The protocol also
specifies the arguments that the command requires and the return values that it produces.
The HTTP protocol is very simple. It supports one command:
For example, if I am connected to www.cs.pomona.edu, I can get my home page by sending the
string "GET /~kcoogan/index.html" to the Web server.
The easiest way to get a feeling for protocols is to run a program called telnet from
a command line (like a UNIX or DOS shell, or Mac Terminal). [Unfortunately, many computers
no longer respond to telnet commands for security reasons.] With telnet, we can send commands
like the GET line above to the server just by typing them on the keyboard and hitting return.
The responses made by the server appear on the terminal.
Here is a sample session using telnet to download a page from a Web server:
First we connect to the Web server. Using telnet we must explicitly state what the port number is.
-> telnet portal.cs.pomona.edu 80
The Web server responds:
Trying 134.173.66.157... Connected to portal.cs.pomona.edu. Escape character is '^]'.
Now we request the home page for cs134:
GET /~cs134/f03/
The server responds with the Web page:
<html> <head> <TITLE>Home page for Kim B. Bruce</TITLE> </head> <body bgcolor="#eeffff"> <table> <tr> <td> <applet archive="objectdraw.jar" code=DrawBanner.class width="1000" height="200" > <img alt="" src="Banner2.jpg"> </applet> </td> </tr> </table> <TABLE WIDTH="100<TD COLSPAN="1"> <H2>Kim B. Bruce</H2> <H3>Reuben C. and Eleanor Winslow Professor of Computer Science <br> and Department Chair<br> <a href="http://www.cs.pomona.edu/">Department of Computer Science</a><BR> <a href="http://www.pomona.edu/">Pomona College</a> </H3> <address><a href="mailto:kim@cs.pomona.edu">kim@cs.pomona.edu</a> </address> </TD> <TD COLSPAN="1" ALIGN="LEFT"> <IMG SRC="KimWeb.jpg"> </TD> <!TD COLSPAN="1" ALIGN="RIGHT"> <!object data="pclogosm.gif" type="image/gif" > <!image src="pclogosm.gif" > <!/TD> </TR> </TABLE> <p> <!img src="line2.gif"> <hr> ... </body> </html> Connection closed by foreign host.
The HTTP protocol automatically closes the connection after returning the Web page.
If I wanted another Web page, I would need to create another connection to get the second page.