Andreas Rozek Hints for Reading List of Recent Changes Guestbook Entry Contact the Author  Deutsche Version  HomePage Previous Topic Next Topic  First Page of Current Topic Next Page Previous Page

Applet_02

The <Applet> directive, which is used to embed an applet in an HTML document, also allows to specify "applet parameters" - similar to "command line arguments" for stand-alone applications. "Applet_02" shows how to read and parse these parameters.

Please, also consider my "Hints for Reading" and the "List of Recent Changes"!

Applet_02 snapshot

The example on the left side shows the parsing result for parameters of any primitive type. You may look into the source code to see how these parameters were read by the applet.

Applet Parameters

Applet parameters are named strings. They can be specified using the <param> directive as shown below:

  <applet ...>
    <param name="parameter_name" value="parameter_value">
    ...
  </applet>

An applet then uses the getParameter method to obtain the string associated with a particular name:

  String Value = getParameter(parameter_name);

If no parameter with the given name exists, getParameter returns null.

According to common applet conventions, the getParameterInfo method yields a description of any foreseen applet parameter specifying its name, type and meaning. The following example has been taken from "Applet_02"

  final static String[][]  ParameterInfo = {
    {"Boolean",   "boolean", "tests parsing of boolean values"},
    {"Character", "char",    "tests character parsing"},
    {"Byte",      "byte",    "tests parsing of bytes"},
    {"Short",     "short",   "tests parsing of short integers"},
    {"Integer",   "int",     "tests integer parsing"},
    {"Long",      "long",    "tests parsing of long integers"},
    {"Float",     "float",   "tests parsing of \"float\" values"},
    {"Double",    "double",  "tests parsing of \"double\" values"},
    {"String",    "String",  "tests string parsing"}
  };

  public String[][] getParameterInfo() {
    return ParameterInfo;
  };

Parameter Parsing

Applet parameters may contain strings only - if any other type is needed, the string has to be interpreted first. The "wrapper" classes Boolean, Integer, Long, Float and Double contain methods for that purpose. A typical approach (in this case: to get an argument of type short) may look as follows

  short Result = 0;              // pre-initialize variable with default value

  String Parameter = getParameter(Name);       // look for the given parameter
  if (Parameter != null) {                     // parse its value if it exists
    try {                                     // take care of illegal contents
      int Value = Integer.valueOf(Parameter).intValue();
      if ((Value >= -32768) && (Value <= 32767)) {
        Result = (short) Value;
      } else {
      /* complain about a value outside the range of a "short" */
      };
    } catch (NumberFormatException Signal) {
      /* complain about an illegal content */
    };
  };

Source Code

The source code of this applet is available for download:

Disclaimer

Please, consider also the author's Disclaimer!

http://www.Andreas-Rozek.de/Java/JavaKurs/Applet_02/Applet_02.html    (last Modification: 01.05.2002)