How To Run JPython Applets?

Many people like Java because it makes easy the task of distributing interactive and dynamic pieces of code through the Web by using applets. An applet is a program written using the Java programming language, which can be included in an HTML page with the <APPLET> tag. This tag needs to reference a class file that is not part of the HTML page on which it is embedded. Applets do this with the CODE parameter, which tells the browser where to look for the compiled .class file. When your browser receives a request to load an applet from a site, it downloads the applet and uses your Java Virtual Machine to execute it.

Before you start testing your applets, make sure that you are using a browser that supports Java jdk1.1. The list of browsers that are currently jdk1.1-compliant include Microsoft’s Internet Explorer 4.0 or later, and Netscape’s Navigator 4.06 or later.

Okay. Now you also need to make sure that you don’t have your class path variable pointing to any directories with JPython .class files. If you are running JVM on UNIX, you need to check out your CLASSPATH environment variable. If you are running a Win32 virtual machine, you need to check out the registry entry Class path under LOCAL _MACHINE /Software /Microsoft /JavaVM/.

The next JPython applet has the goal of displaying the message “Hello Python World”. fromjava.applet import Applet

classHelloPythonWorld(Applet):

def paint(self,gc):
gc.drawString(“Hello Python World”,12,14)

If you want to test the applet to run it as a script too, add a few more lines to the end of the applet file. These lines will allow you to interactively test the applet functionality.

if_name_==’_main_’:
importpawt
pawt.test(HelloPythonWorld())

If you want to embed this applet in your Web page, you just need to inform the right values for the applet tag, such as

<applet code=”HelloPythonWorld”archive=”HelloPythonWorld.jar”
width = 50 height = 100>

JPython applets need to carry the whole set of JPython libraries, which adds about 150KB to the final size of your applet. Another important consideration is that you can only use evaland execcommands in signed applets, which complies with the Java security definition.