Using servlets together with applets

All examples on this page require Java 1.4. They also require the Java plug-in for the browser. The applets are embedded in the page using <EMBED> tag which works in Netscape browsers, but not in IE.

The examples work on Netscape browsers on puma. They don't work on Wellesley Macs because the Macs don't have Java 1.4. If you want to run them on a machine other than puma or on a browser other than Netscape, please proceed at your own risk :-)

Invoking applets in web pages (a brief overview)

In order to include an applet into an HTML page, one can use <APPLET> tag or an <EMBED> tag. There is also an <OBJECT> tag used in IE, but we are not going to cover it here.

<APPLET> tag and why we can't use it.

Here is an example of using the <APPLET> tag:

<APPLET 
CODE = "McKoiApplet.class" 
CODEBASE = "http://cs.wellesley.edu/~ecom/download/"
ARCHIVE = "mckoidb.jar" 
WIDTH = 800 HEIGHT = 300>
</APPLET>

Here CODE specifies the class of the applet (required), CODEBASE gives the HTTP address of the directory where the class is located (can be omitted if the HTML file is in the same directory as the applet class), ARCHIVE points to the .jar files used by the applet, and WIDTH and HEIGHT specify the space occupied by the applet on the web page (required by some browsers, it's better not to omit these parameters).

Note that the CODEBASE parameter makes it possible to access the applet remotely. In our example the applet will be invoked from a servlet running on birch.

Passing parameters to an applet. It is possible to pass parameters to an applet via a PARAM tag:


<APPLET 
CODE = "McKoiAppletParam.class" 
CODEBASE = "http://cs.wellesley.edu/~ecom/download/"
ARCHIVE = "mckoidb.jar" 
WIDTH = 800 HEIGHT = 300>
<PARAM NAME="StudentName" VALUE = "Mary">
</APPLET>

The parameter can be retrieved in the applet by the method getParameter():


String	searchFor = getParameter("StudentName");

Why we can't use the <APPLET> tag. The <APPLET> tag invokes the default Java run-time environment for the browser. Since the example requires Java 1.4 for the database driver, which is higher than the default for most browsers, we can't use this tag.

Using <EMBED> tag.

The <EMBED> tag allows you to specify the version of Java that you are using. You also need to set the browser's Java plug-in settings to be able to access the right version of Java. On puma this is done automatically, you don't need to worry about it.

Below is an example of the <EMBED> tag which passes a parameter called StudentName to the applet:


<EMBED type="application/x-java-applet;version=1.4"
CODE = "McKoiAppletParam.class"
CODEBASE = "http://cs.wellesley.edu/~ecom/download/"
ARCHIVE = "mckoidb.jar"
WIDTH = 800 HEIGHT = 300 
StudentName = "Mary">
</EMBED>

The parameter is retrieved in the applet the same way as for the example with the <APPLET> tag.

<EMBED> tag can be used only for Netscape browsers. The tag <OBJECT> is used for IE. If you need more info on embedding applets in web pages, check out the section on using tags with applets at java.sun.com.

Examples of using applets with servlets.

We use <EMBED> tags and applet parameter-passing to write pages which incorporate both servlets and applets. Note that applets and servlets are on different servers: applets are on puma, and servlets are on birch (notice at the URLs when you run the examples!).

It's important to keep in mind that writing a part of an application using servlets, and another one using applets, is not the best software engineering decision. However, these examples demonstrate that it is possible to do it when necessary. A common case of incorporating applets with servlets is when servlets are used for form processing, and applets are used for graphics.


This page has been created and is maintained by Elena Machkasova
Comments and suggestions are welcome at emachkas@wellesley.edu

Spring Semester 2002