| CIT
597 Networking Examples Fall 2007, David Matuszek |
The first four of these examples are taken, sometimes with minor modifications, from Advanced Java: Internet Applications, 2nd edition, by Art Gittleman.
This is an applet that causes the browser to go to a new page and display it. The URL of the new page is given as a parameter to the applet. Here is the most significant part of the code:
URL url = new URL(getParameter("url"));
getAppletContext().showDocument(url);
Note that this program only works in a browser, not in appletviewer, because
appletviewer does not have the ability to display a document. Attempting to run
it from Eclipse causes Eclipse to crash.
Source code: ShowURL.java, RunShowURL.html
This is an application that goes to the given URL (given as a parameter on the command line), and prints out what is returned by the server. The most significant part of the code is opening an input stream from the given URL.
URL url = new URL(args[0]);
input = new BufferedReader(new InputStreamReader(url.openStream()));
To execute this program from within Eclipse, select Run -> Run... -> Arguments and enter a URL into the Program Arguments field.
Source code: TryURL.java
This application sends a URL to the server, then prints out the HTTP header that is returned. Significant code is:
URLConnection c = url.openConnection();
. . .
key = c.getHeaderFieldKey(n);
value = c.getHeaderField(n);
Example output:
Status line: HTTP/1.1 200 OK Response headers: Date: Wed, 15 Aug 2007 16:35:10 GMT Server: Apache Last-Modified: Mon, 02 Jul 2007 19:15:02 GMT ETag: "19005f3-3b70-a5489980" Accept-Ranges: bytes Content-Length: 15216 Keep-Alive: timeout=5, max=200 Connection: Keep-Alive Content-Type: text/html
Source code: GetResponses.java
This application uses URLConnection methods to set the acceptable MIME type, and to display selected results. There are two arguments on the command line: the URL, and the MIME type. Significant code is:
URLConnection c = url.openConnection();
String mimeType = args[1];
c.setRequestProperty("accept", mimeType)
. . .
System.out.println("Content type: " + c.getContentType());
System.out.println("Content length: " + c.getContentLength());
System.out.println("Length using getHeaderField: " + c.getHeaderField("Content-length"));
Partial output:
Content type: text/html
Content length: 15216
Length using getHeaderField: 15216
This application is taken, with some modifications, from a program by Elliott Rusty Harold (see http://www.cafeaulait.org/slides/sd2000east/webclient/Web_Client_Programming_with_Java.html).
Just as in ShowURL, this application displays a web page, but this
time putting it in a JEditorPane. The setPage method can take, as a parameter,
either a String representation of a URL, or a URL object.
jep = new JEditorPane();
jep.setPage(initialPage);
jep.setPage(url);
In addition, SimpleWebBrowser can follow hyperlinks. To accomplish
this, the JEditorPane must be made non-editable, and a HyperlinkListener must
be added to it. This example uses an anonymous inner class for the HyperlinkListener,
like so:
jep.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent evt) {
if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
try {
jep.setPage(evt.getURL());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
});