See All Titles |
![]() ![]() Running JPython AppletsMany 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 Classpath under LOCAL_MACHINE/Software/Microsoft/JavaVM/. The next JPython applet has the goal of displaying the message "Hello Python World". from java.applet import Applet class HelloPythonWorld(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__': import pawt 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 eval and exec commands in signed applets, which complies with the Java security definition. The following Web link takes you to the official home of JPython, specifically to the applets page: http://www.jpython.org/applets/
|
Index terms contained in this section<APPLET tag applets JPython, running CODE parameter commands eval exec embedding applets eval command exec command JPython running applets parameters CODE programming languages JPython running applets running JPython applets tags < APPLET testing applets |
© 2002, O'Reilly & Associates, Inc. |