CSCI A348/548
Lecture Notes Twenty-Three

Fall 2000


Even more Java.

Today we are going to study this example:

import java.util.*; 

class Test {
    public static void main(String[] args) {
	Monitor m1 = new Monitor("One  "); 
	Monitor m2 = new Monitor("Two  "); 
	Monitor m3 = new Monitor("Three"); 
	Performer p = new Performer();
	p.addObserver(m1); 
	p.addObserver(m2); 
	p.addObserver(m3); 

	p.doSomething(); 

    }
} 

class Monitor implements Observer {

    String name; 

    public Monitor(String name) { this.name = name; } 

    public void update (Observable o, Object arg) {
	System.out.println( name + ": " + ((Integer)arg).intValue() ); 
    } 

} 

class Performer extends Observable {

    int n = 0; 
    public void doSomething() {
	n += 1; 

	setChanged(); 
	notifyObservers(new Integer(n)); 
	clearChanged(); 

    }

} 
We'll develop it from scratch, modify it, relate it to the chat application.

Then we will develop:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
                     
public class Count extends HttpServlet {

  int n; 

  public void doGet(HttpServletRequest req, 
                    HttpServletResponse resp) 
              throws ServletException, 
                     IOException 
  { 
     n += 1; 

     resp.setContentType("text/html"); 
     resp.getWriter().println(

         "<html><head><title>Counting</title></head><body bgcolor=white>"
       + "Counter is now: <font color=blue size=+6>" 
       + n 
       + "</font></body></html>"

     ); 

  } 


}
We will compare this to:
#!/usr/bin/perl

$n += 1; 

print "Content-type: text/html\n\nCounter is:($n)
We will look at the main difference and decide which technology is more flexible.


Last updated on November 14, 2000, by Adrian German for A348/A548.