Week 10: Java Applets and Thread Example


Java Applets Overview

  1. The name of the class file
  2. The location of the class file
  3. How the applet sits on the Web page (size, location, and so on)




Basic Applet Concept


import java.awt.*;
import java.applet.*;

public class ByeWorld extends Applet
	// Override the paint method
	public void paint(Graphics g)
		Font f = new Font("System", Font.BOLD, 18);
		g.drawString("This is the final Intro to Java class", 25, 50);

... Compile the .java file into .class file, then in the same directory, have a 
... html file the contains at least the following line: (More detail later on)

<APPLET CODE="ByeWorld.class" WIDTH=100 HEIGHT=100> </APPLET>




Security Basics

  1. Open HTML files as local files from the browsers,
  2. Download the HTML file from the net and then run with it
  1. Applets can never run any local executable program
  2. Applets can only communciate with the originating host (the host where the applet is downloaded)
  3. On local file system can be accessed by the applet
  4. Applet can only find out Java version, the name and version of the OS, file separator and line separator. Applet cannot find out user's name, email, etc.
    (JavaScript on the other hand can find out browser's history information, brower's version, etc)
  5. Applets can only pop up windows with warning message (saying something like "Untrusted Java Applet Window")
  6. Applets is said to be running in the "sandbox"
  1. All program code is interpreted by the Java Virtual Machine, security is enforced within the java interpreter (as well as JIT compiler is you can converting into native code)
  2. A security manager (just anothe Java class) checks all sensitive operations (operations defined in some interfaces) in the Java run-time library
  3. Applets can be signed to identify their origin (supported by IE 4.0, Netscape 4.0 ?)
    (This is what Active X is using)



Life Cycle of an Applet

There are four conceptal "phases" in the life cycle (init, start, stop, destroy)







HTML Tag for Java

<TITLE>Week 10</TITLE>

<H1>Java Test</H1>

<APPLET CODE="ByeWorld.class" WIDTH=100 HEIGHT=100></APPLET>


<!-- If your applet is called ByeWorld.class and is in directory myDir and myDir is
     one level down from your current web page, then the following HTML code will be
     able to use to call the applet -->

<APPLET CODE="ByeWorld.class" CODEBASE="myDir" WIDTH=100 HEIGHT=100></APPLET>

<!-- If you have a jar file need to be loaded, you can use the ARCHIVE ...
     think about a jar files as a compressed file that will contains a bunch of 
     files for your applet -->
<APPLET CODE="Hi.class" ARCHIVE="lotus.jar, sinn/mySrc.jar" WIDTH=888 HEIGHT=188></APPLET>

<!-- For non java enabled browser, we can use the same old ALT tag to display information/error message -->

<APPLET CODE="hi.class" WIDTH=888 HEIGHT=188 ALT="Your browser is not java enabled, please download a new one"></APPLET>



JAR Files

For normal loading method of applet:
<APPLET CODE="hi.class" WIDTH = 400 HEIGHT=300></APPLET>

We can use the following to load all the class files at once:
<APPLET CODE="hi.class" ARCHIVE="hi2.jar" WIDTH=300 HEIGHT=500></APPLET>

In order to make a JAR file, use the jar tool under \jdk\bin

jar <options> File1 File2 ...

For example: jar cf hi2.jar *.java logo.gif

// c - create new archive, f - use 2nd parameter as JAR file name




URL myUrl = hi.class.getResource("copyright.txt");
InputStream in = url.openStream();
... or, put everything in one line ...
InputStream in = hi.class.getResourceAsStream("copyright.txt");



Passing Information Into Applet

Applets have the ability to use parameters that are embedded into the HTML file. This is done by using the PARAM tag

<APPLET CODE="Hi.class" WIDTH=300, HEIGHT=200>
<PARM NAME=hidear VALUE="This is Week10">

.... // Then, in the java code, we have ...

import java.applet.*;
import java.awt.*;

public class Hi extends Applet
	public void paint(Graphics g)
		String inputString = getParameter("hidear");
		g.drawString("Finally ..." + inputString, 25, 50);




Lab 1 (Applet)

Use the lab exercise from Week 9 and then convert that into an applet. Use Week10.java as the main class for this week and name the HTML file as week10.html




Lab 2 - A Threaded Server Java Application

A Thread is a single sequential flow of control within a program. In Java, thread is in the user level (vs operating system level). Threads can be created easily in order to provide base for concurrent applications.

In this lab, we will use the java classes from Week 7 and create a threaded echo server that will be able to accept multiple clients.