Title: Handle the passing of parameters into an applet from an HTML page.
Course: Java Programming (Oracle) (10055) > Section: Creating Applets, Events, and Other Topics

Passing parameters to an applet should include using the PARAM attribute of the APPLET tag

- to specify the parameter’s name and value
- to retrieve a parameter, using the applet’s getParameter( )

and confirming that returned values and that any conversions are valid.

Actual code incorporating passing parameters follows:

// Pass a parameter to an applet.
import java.awt.*;
import java.applet.*;

/*
<applet code="Param" width=300 height=80>
<param name=author value="Herb Schildt">
<param name=purpose value="Demonstrate Parameters">
<param name=version value=2>
</applet>
*/

public class Param extends Applet {
String author;
String purpose;
int ver;

public void start() {
String temp;

author = getParameter("author");
if(author == null) author = "not found";

purpose = getParameter("purpose");
if(purpose == null) purpose = "not found";

temp = getParameter("version");
try {
if(temp != null)
ver = Integer.parseInt(temp);
else
ver = 0;
} catch(NumberFormatException exc) {
ver = -1; // error code
}
}

public void paint(Graphics g) {
g.drawString("Purpose: " + purpose, 10, 20);
g.drawString("By: " + author, 10, 40);
g.drawString("Version: " + ver, 10, 60);
}
}