CS 051 Fall 2012

Lecture 35

JFileChooser

Our previous example hardcoded the names of the files that we wanted to use into the source code.
This makes it impossible for the user of the program to specify a file name, unless he rewrites
the code. There is a better way.

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.

URL's

We've looked at Streams that can read and write files. But we can access data from other
places too.

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 {

Networks

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?

http
This specifies the protocol that will be used in communication.
"http" means HyperText Transfer Protocol. This is the protocol used by Web servers.
More on protocols later.

www.pomona.edu
This identifies the name of the machine where the Web server is running.

80
This is the port number the Web server is running on. Think of this as
your SU Box number. One machine may run several servers, so we identify the specific server
we want by identifying its port. Common servers, like Web servers, have default port numbers.
The default port number for a Web server is 80. If a server is running at its default port,
we can omit the port number in the URL.

everything else
The rest is information that is passed to the Web server to identify the Web page we want to view.

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:

GET
The GET command takes an argument which is the name of the content we want
the Web server to return. It returns a long string, which is the Web page content.

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 www.cs.pomona.edu 80

The Web server responds:

Trying 134.173.66.118...
Connected to www.cs.pomona.edu.
Escape character is '^]'.

Now we request Prof. Bruce's home page:

GET /~kim/index.html

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.