CSCI A348/A548

Lecture Notes 15

Fall 1999


Java, applets, clients and servers, a feedback applet with Java.

1. Using Java applets (Chapter 11)

Java Basics (pages 645-659)

2. Writing Java applets and applications

See the on-line resources for a complete, outstanding tutorial on Java.

Java development cycle:

Empty file does not compile: need class definitions.

Empty class compiles but doesn't run: needs special main method.

Simplest program:

class Empty {
  public static void main(String[] args) {
 
  } 
}
It compiles, runs, but doesn't do anything.

The "Hello, world!" program.

class Hello {
  public static void main(String[] args) {
    System.out.println("Hello, world!"); 
  } 
} 
It compiles, runs, and prints the message.

Who's System? How about out?

Basic objects: a BasicCounter class.

class BasicCounter {
  int n = 0; 
  void addOne() {
    n += 1; 
    System.out.println("addOne: n is now " + n); 
  } 
} 
 
class Tester {
  public static void main(String[] args) {
    BasicCounter c = new BasicCounter(); 
    c.addOne(); 
    c.addOne(); 
  } 
} 
A CustomCounter:
class CustomCounter extends BasicCounter {
  void show() {
    System.out.println("show: n is currently " + n); 
  } 
  public static void main(String[] args) {
    CustomCounter c = new CustomCounter();
    c.show();
    c.addOne();
    c.addOne();
    c.show(); 
    c.addOne(); 
    c.show();
    c.show(); 
  } 
}
Hierarchy (inheritance) diagram:
java.lang.Object
   |
   +----BasicCounter           // defines n, addOne();
           |
           +----CustomCounter  // defines show();
Same kind of diagram for java.applet.Applet:
java.lang.Object
   |
   +----java.awt.Component
           |
           +----java.awt.Container
                   |
                   +----java.awt.Panel
                           |
                           +----java.applet.Applet
Here's a simple applet:
import java.applet.Applet;
import java.awt.Graphics;
            
public class HelloWorld extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello world!", 50, 25);
    }
}
(How and when is the paint() method called?)

To see this applet we need an .html front end:

<html>
<head><title>Applet One</title></head>
<body bgcolor=white>
<applet code="HelloWorld" width=100 height=100>
</applet>
</body> 
</html>
Then we look at it with the appletviewer or a web browser.

Networking Experiments

1. telnet to the time of day service

telnet to a machine, on port 13.

2. telnet to your web server

First prepare a file:

tucotuco.cs.indiana.edu% pwd
/nfs/paca/home/user2/dgerman/httpd/htdocs
tucotuco.cs.indiana.edu% vi myfile
tucotuco.cs.indiana.edu% cat myfile
 
    ***( this is my test )***

tucotuco.cs.indiana.edu% pwd
/nfs/paca/home/user2/dgerman/httpd/htdocs
Then connect with telnet to your host on your port and
Trying 129.79.251.110...
Connected to tucotuco.cs.indiana.edu.
Escape character is '^]'.
GET /myfile HTTP/1.0
 
HTTP/1.1 200 OK
Date: Thu, 01 Oct 1998 17:18:06 GMT
Server: Apache/1.3.1 (Unix)
2. POST to a script of yours with telnet

We have a script that can be used to mail a comment to the webmaster.

2.1 the script

#!/usr/bin/perl
 
print qq{Content-type: text/plain\n\n}; 
 
if ($ENV{'REQUEST_METHOD'} eq 'POST' && 
    open(MAIL, "| mail dgerman\@indiana.edu")) {
  while (<STDIN>) {
    print MAIL; 
  } 
  close(MAIL);      
  print "Mailto.pl";
} else {
  print "Error";   
}
2.2 the experiment

From school telnet to your web server and


Trying 129.79.251.110...
Connected to tucotuco.cs.indiana.edu.
Escape character is '^]'.
POST /cgi-bin/lecture9/mailto.pl HTTP/1.0
Content-type: application/octet-stream
Content-length: 10
 
Greetings! 
HTTP/1.1 200 OK
Date: Thu, 01 Oct 1998 18:03:22 GMT 
3. the feedback applet

It's a combination of three things:

But first let's write our 'telnet'.

3.1 a network client

This should essentially be able to send a stream of bytes (a stream of characters) over the network, to a server. Here's the code for one such network client:

import java.io.*;
import java.net.*;
      
public class Client {
  public static void main(String[] args) {
    try {
      Socket con = new Socket("tucotuco.cs.indiana.edu", 19904);
      PrintWriter out = new PrintWriter(con.getOutputStream(), true);
      
      out.println(
        "POST /cgi-bin/lecture15/mailto.pl HTTP/1.0\r\n" + 
        "Content-type: application/octet-stream\r\n" + 
        "Content-length: " + args[0].length() + "\r\n\r\n" +
        args[0]
      );
      out.flush();
      System.out.println("Sent: (" + args[0].length() + ") " + args[0]);    
    } catch (Exception e) {
      System.out.println("E: " + e);
    }
  }     
}


Last updated: October 19, 1999 by Adrian German