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(); 
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();;
Hierarchy (inheritance) diagram:
   +----BasicCounter           // defines n, addOne();
           +----CustomCounter  // defines show();
Same kind of diagram for 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:

<head><title>Applet One</title></head>
<body bgcolor=white>
<applet code="HelloWorld" width=100 height=100>
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: pwd
/nfs/paca/home/user2/dgerman/httpd/htdocs vi myfile cat myfile
    ***( this is my test )*** pwd
Then connect with telnet to your host on your port and
Connected to
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

print qq{Content-type: text/plain\n\n}; 
if ($ENV{'REQUEST_METHOD'} eq 'POST' && 
    open(MAIL, "| mail dgerman\")) {
  while (<STDIN>) {
    print MAIL; 
  print "";
} else {
  print "Error";   
2.2 the experiment

From school telnet to your web server and

Connected to
Escape character is '^]'.
POST /cgi-bin/lecture9/ HTTP/1.0
Content-type: application/octet-stream
Content-length: 10
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:

public class Client {
  public static void main(String[] args) {
    try {
      Socket con = new Socket("", 19904);
      PrintWriter out = new PrintWriter(con.getOutputStream(), true);
        "POST /cgi-bin/lecture15/ HTTP/1.0\r\n" + 
        "Content-type: application/octet-stream\r\n" + 
        "Content-length: " + args[0].length() + "\r\n\r\n" +
      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