CSCI A348/548
Lecture Notes Twenty-Two

Fall 2000


More Java Programming

There are a few things I need to catch up in terms of notes:

  1. two sets on the shopping cart (also to be found in Stein)

  2. two on the basics of Java programming (also to be found on the Fall '99 web site)

  3. one (last time) on applets, on customization of servlet to allow user specify a name

Today let's see if we can clarify a few more things on the ChaApplet.

Let's work with the HttpMessage class.

Go under your chat directory and create a lecture22 directory.

Let's make sure we can write, compile, and run Java programs here first.

burrowww.cs.indiana.edu% emacs A.java
burrowww.cs.indiana.edu% cat A.java
public class A {
    public static void main(String[] args) {
        System.out.println("Hello, this is A. ");
        B b = new B(); 
        b.greetings(); 
    }
}
burrowww.cs.indiana.edu% emacs B.java
burrowww.cs.indiana.edu% cat B.java
public class B {
    void greetings() {
        System.out.println("Hello, I am an object of type B."); 
    }
}
burrowww.cs.indiana.edu% javac A.java
burrowww.cs.indiana.edu% java A
Hello, this is A. 
Hello, I am an object of type B.
burrowww.cs.indiana.edu% 
If this doesn't work for you ask for help.

One thing that you may want to change would be to add the current directory to your CLASSPATH.

I changed my line in ~/.cshrc as follows:

setenv CLASSPATH   .:$TOMCAT_HOME/lib/servlet.jar
You will, of course, note the dot-colon, in front.

I also sourced my ~/.cshrc (or log out and log back in).

Once we know this works let's move on to experiments.

java.net.URL

A Uniform Resource Locator (URL) is a string that describes how to locate a resource on the Internet. In general a URL consists of the following components:

  1. protocol
  2. host name
  3. port number
  4. file name
  5. reference
Here's a complete example:
http://jupiter.cs.indiana.edu:12345/notice.html#READMEFIRST
(Highlight the URL with your mouse to render it with no colors).

The java.net.URL represents URLs and provides methods to construct and obtain components of the URL (its protocol, host name, port number, etc.) In addition, it provides methods that, after a URL has been created, uses the URL to retrieve the resource identified by the URL. It also supports lower-level methods such as opening a connection or input stream to the server that is managing the resource identified by the URL.

Here's documentation about it.

Now let's create a server side resource.

Go to your cgi-bin and create a resource.

burrowww.cs.indiana.edu% emacs javaOne
burrowww.cs.indiana.edu% cat javaOne
#!/usr/bin/perl

print "Content-type: text/html\n\nHello, and how are you doing?\n"; 
burrowww.cs.indiana.edu% ls -l javaOne
-rwxr-xr-x   1 dgerman  students       86 Nov  9 12:28 javaOne
burrowww.cs.indiana.edu% 
Then test it.

Now, just like we did in the experiments before the midterm, we try to access the resource from the command line.

But we write a Java program to do that for us:

burrowww.cs.indiana.edu% pwd
/nfs/paca/home/user1/dgerman/apache/jakarta-tomcat/webapps/ROOT/chat/lecture22
burrowww.cs.indiana.edu% emacs Test1.java
burrowww.cs.indiana.edu% cat Test1.java
import java.net.*;
import java.io.*; 

public class Test1 {
    public static void main(String[] args) {
      try { 
        URL servlet = 
            new URL("http://burrowww.cs.indiana.edu:10000/cgi-bin/javaOne");
        URLConnection con =
            servlet.openConnection(); 
        con.setUseCaches(false); 
        InputStream in = con.getInputStream(); 
        BufferedInputStream buf = new BufferedInputStream(in); 
        DataInputStream data = new DataInputStream(buf); 
        System.out.println(data.readLine()); 
      } catch (Exception e) { 
          // ignore for the moment... 
      } 
    } 
}
burrowww.cs.indiana.edu% 
The part in blue is the part we already understand.

Let's explain the remaining part.

openConnection() opens a connection to the location identified by this URL.

The object returned is of type java.net.URLConnection so we need to look that one up too.

Here's documentation about it.

The lines in brown will open the connection and set it up.

Once the connection is open the resource has been tapped into, and it replies.

So it sends back information and we need to get ready to read it.

The remaining lines are basic Java I/O that set up an input line so we can read Strings.

The whole thing works.

Let's look at something more complicated.

Let's do GET and POST.

First, let's set up a more powerful resource.

burrowww.cs.indiana.edu% emacs javaTwo
burrowww.cs.indiana.edu% cat javaTwo
#!/usr/bin/perl

if ($ENV{REQUEST_METHOD} eq 'GET') {
  $in = $ENV{QUERY_STRING}; 
} else { 
  read(STDIN, $in, $ENV{CONTENT_LENGTH}); 
} 

print "Content-type: text/html\n\n"; 

@in = split(/&/, $in); 

foreach $e (@in) {
    ($a, $b) = split(/=/, $e); 
    print "param $a has value $b "; 
} 

print "\n"; 


burrowww.cs.indiana.edu% ls -l java*
-rwxr-xr-x   1 dgerman  students       86 Nov  9 12:28 javaOne
-rwxr-xr-x   1 dgerman  students      311 Nov  9 12:57 javaTwo
burrowww.cs.indiana.edu% 
Then test it a few times:

  1. javaTwo?a=b&c=d
  2. javaTwo?x=y
  3. javaTwo
You can also test it with POST but that takes longer, with a form, though you should do it.

Now let's access the resource through a program.

burrowww.cs.indiana.edu% pwd
/nfs/paca/home/user1/dgerman/apache/jakarta-tomcat/webapps/ROOT/chat/lecture22
burrowww.cs.indiana.edu% emacs Test2.java
burrowww.cs.indiana.edu% cat Test2.java
import java.net.*;
import java.io.*; 
import java.util.*; 

public class Test2 {
    public static void main(String[] args) {
      try { 
        URL servlet = 
            new URL("http://burrowww.cs.indiana.edu:10000/cgi-bin/javaTwo");

        Properties props = new Properties(); 
        props.put("a", "b"); 
        props.put("c", "d"); 
        String queryString = "?" + toEncodedString(props); 
        System.out.println(queryString); 
        System.out.println(servlet.toExternalForm()); 
        servlet = new URL(servlet.toExternalForm() + queryString); 
        URLConnection con =
            servlet.openConnection(); 
        con.setUseCaches(false); 
        InputStream in = con.getInputStream(); 
        BufferedInputStream buf = new BufferedInputStream(in); 
        DataInputStream data = new DataInputStream(buf); 
        System.out.println(data.readLine()); 
      } catch (Exception e) { 
          // ignore for the moment... 
      } 
    } 

    private static String toEncodedString(Properties args) {
        StringBuffer buf = new StringBuffer();
        Enumeration names = args.propertyNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            String value = args.getProperty(name);
            buf.append(URLEncoder.encode(name) + "=" + 
                       URLEncoder.encode(value));
            if (names.hasMoreElements()) buf.append("&");
        }
        return buf.toString();
    }

}
burrowww.cs.indiana.edu% javac Test2.java
Note: Test2.java uses or overrides a deprecated API.  Recompile with "-deprecation" for details.
1 warning
burrowww.cs.indiana.edu% java Test2
?c=d&a=b
http://burrowww.cs.indiana.edu:10000/cgi-bin/javaTwo
param c has value d param a has value b 
burrowww.cs.indiana.edu% 
The color is supposed to highlight the various levels of new in the code.

This should open the door for you to understand HttpMessage completely.

Threads.

We start with a simple Echo program that has no threads.

burrowww.cs.indiana.edu%% emacs Test3.java
burrowww.cs.indiana.edu%% cat Test3.java  
import java.io.*;

public class Test3 {
  public static void main(String[] args) {
      System.out.println("Hello, this is Echo!"); 
      DataInputStream data = 
          new DataInputStream(new BufferedInputStream(System.in)); 
      try {
        String line = data.readLine(); 
        while (! (line.equals("bye"))) {
          System.out.println(":-)  You typed: " + line);
          line = data.readLine(); 
        } 
        System.out.println("Good-bye!");
      } catch (Exception e) { } 
  } 
}
burrowww.cs.indiana.edu%% javac Test3.java
Note: Test3.java uses or overrides a deprecated API.  Recompile with "-deprecation" for details.
1 warning
burrowww.cs.indiana.edu%% java Test3
Hello, this is Echo!
How are you doing, Echo?
:-)  You typed: How are you doing, Echo?
And so did you. 
:-)  You typed: And so did you. 
I know that...
:-)  You typed: I know that...
Well, I have to go now.
:-)  You typed: Well, I have to go now.
You don't seem to care...
:-)  You typed: You don't seem to care...
bye
Good-bye!
burrowww.cs.indiana.edu%% 
This illustrates a bit of terminal I/O and a loop.

Let's make the program do two things at the same time.

burrowww.cs.indiana.edu% emacs Test4.java
burrowww.cs.indiana.edu% cat Test4.java
import java.io.*;

public class Test4 {
  public static void main(String[] args) {
      System.out.println("Hello, this is Echo!"); 

      Thread thread = new Thread(new WhiteRabbit()); 
      thread.start();  

      DataInputStream data = 
          new DataInputStream(new BufferedInputStream(System.in)); 
      try {
        String line = data.readLine(); 
        while (! (line.equals("bye"))) {
          System.out.println(":-)  You typed: " + line);
          line = data.readLine(); 
        } 
        System.out.println("Good-bye!");
      } catch (Exception e) { } 
  } 
  
}

class WhiteRabbit implements Runnable { 
    public void run() {
        while (true) {
            System.out.println("                                         " +
                               new java.util.Date()); 
            System.out.println("                                         " +
                               "Oh, my goodness, I'm late, I'm late!...");

            try { 
                Thread.sleep(5000);
            } catch (Exception e) { } 

        }
    } 
} 

burrowww.cs.indiana.edu% javac Test4.java
Note: Test4.java uses or overrides a deprecated API.  Recompile with "-deprecation" for details.
1 warning
burrowww.cs.indiana.edu% 
We need to explain this, but first let's see it in action.

burrowww.cs.indiana.edu% java Test4
Hello, this is Echo!
                                         Thu Nov 09 13:38:50 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
I am here
:-)  You typed: I am here
                                         Thu Nov 09 13:38:55 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
And there is a                                          Thu Nov 09 13:39:01 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
rabb                                         Thu Nov 09 13:39:06 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
it apparently                                          Thu Nov 09 13:39:11 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
running arround!
:-)  You typed: And there is a rabbit apparently running arround!
N                                         Thu Nov 09 13:39:16 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
o
:-)  You typed: No
I see
:-)  You typed: I see
I think                                          Thu Nov 09 13:39:21 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
I can type at the same time the r^A                                         Thu Nov 09 13:39:26 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
^R
I think I can type at the same time the rabbit is                                         Thu Nov 09 13:39:31 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
 reporting the time
:-)  You typed: I think I can type at the same time the rabbit is reporting the time
OI                                         Thu Nov 09 13:39:36 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
^R
Oh boy, parr                                         Thu Nov 09 13:39:41 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
allel processing!
:-)  You typed: Oh boy, parrallel processing!
N                                         Thu Nov 09 13:39:46 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
ifty.
:-)  You typed: Nifty.
bye
Good-bye!
                                         Thu Nov 09 13:39:51 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
                                         Thu Nov 09 13:39:56 EST 2000
                                         Oh, my goodness, I'm late, I'm late!...
^Cburrowww.cs.indiana.edu% 
You need to run this and experience it to fully comprehend it.

Meanwhile we can tighten the code up a bit.

burrowww.cs.indiana.edu% emacs Test5.java
burrowww.cs.indiana.edu% pwd
/nfs/paca/home/user1/dgerman/apache/jakarta-tomcat/webapps/ROOT/chat/lecture22
burrowww.cs.indiana.edu% cat Test5.java
import java.io.*;

public class Test5 implements Runnable {
  public static void main(String[] args) {
      System.out.println("Hello, this is Echo!"); 

      Thread thread = new Thread(new Test5()); 
      thread.start();  

      DataInputStream data = 
          new DataInputStream(new BufferedInputStream(System.in)); 
      try {
        String line = data.readLine(); 
        while (! (line.equals("bye"))) {
          System.out.println(":-)  You typed: " + line);
          line = data.readLine(); 
        } 
        System.out.println("Good-bye!");
      } catch (Exception e) { } 
  } 

  public void run() {
      while (true) {
          System.out.println("                                         " +
                             new java.util.Date()); 
          System.out.println("                                         " +
                             "Oh, my goodness, I'm late, I'm late!...");

          try { 
              Thread.sleep(5000);
          } catch (Exception e) { } 

      }
  } 
  
}
burrowww.cs.indiana.edu% javac Test5.java
Note: Test5.java uses or overrides a deprecated API.  Recompile with "-deprecation" for details.
1 warning
Running this would produce something similar.

Your applet does the same thing. (Except, being an object, it has the run method already).

It needs to constantly retrieve information from the server, and it does that in an infinite loop.

If for some reason it times out it sends the request again.

Should it be sent some message that was posted, it shows it to the user, then sends a GET again.

If we were not to run this in parallel the user would never be able to type anything in the tex field.

Try implementing this without the thread, and you will see the white rabbit will not let you do anything.


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