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 :-)
<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.
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.
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.
Source code for this example includes:
On birch: the source code for the servlet. The user enters the value of the parameter for the search performed by the applet. Every time the user enters a new file, the applet performs a different search. On puma: the source code for the applet. The applet is similar to the one in the previous example. The difference is that it takes a parameter StudentName and performs search based on the value of the parameter. It displays the message "No results to your query" if the result set is empty.
Note that no HTML file for the applet is needed, since the applet is embedded into the page created by the servlet. Study the source code of the page to see how the applet parameter StudentName changes.