Sending Parameters from a Web Page

The functionality of an applet can be customized with the use of parameters, settings stored in HTML markup that serve the same purpose as command-line arguments in applications.

Parameters are stored as part of the web page that contains an applet. They’re created using the HTML tag param and its two attributes: name and value. You can have more than one param tag with an applet, but all must be placed between the opening <applet> and closing </applet> tags. Here’s an applet tag that includes several parameters:

<applet code="ScrollingHeadline" height="50" width="400">
    <param name="headline1" value="Dewey defeats Truman">
    <param name="headline2" value="Stix nix hix pix">
    <param name="headline3" value="Man bites dog">
</applet>

This markup could be used with an applet that scrolls news headlines across a web page. Because news changes all the time, the only way to design a program like that is through the use of parameters.

The name attribute give a parameter a name and value assigns it a value.

Receiving Parameters in the Applet

You access parameters in an applet by. calling the getParameter(String) method of the applet—inherited from JApplet—with its name as the argument, as in this statement:

String display1 = getParameter("headline1");

The getParameter() method returns parameter values as strings, so they must be converted to other types as needed. If you want to use a parameter as an integer, you could use statements such as the following:

int speed;
String speedParam = getParameter("speed");
if (speedParam != null) {
    speed = Integer.parseInt(speedParam);
}


Caution

The Integer.parseInt(String) method requires the use of exceptions, a technique you learn about during Hour 18, “Handling Errors in a Program.”


This example sets the speed integer variable by using the speedParam string. When you try to retrieve a parameter with getParameter() that was not included on a web page with the param tag, it is sent as null, which is the value of an empty string.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.44.229