Lesson 6.6 Applet Parameters
Before you get to work on the assignment for this lesson, you need to learn how to pass arguments to applets, using the HTML param tag. This will allow you to build more general purpose applets that are easy to modify.
When you want to customize the way a particular object behaves, you'll normally pass different arguments to the object's methods. You can, for instance, change the way that a Label object appears by calling its setText() method, and supplying different text each time you call the method. Arguments--the text you supply to your Label--allow you to customize the Label object's behavior.
The Applet class' getParameter() method allows you to change the way your applets behave by passing information from the HTML file that hosts the applet, into the applet itself. This allows you to write more general purpose applets--applets that can be customized for a variety of situations.
Here's how it works.
Sending Information with param
To pass information from your HTML pages to your applet, you use a new tag, called the param tag. The param tag must appear between the opening <applet> tag and its closing </applet> twin. Here's what this looks like:
<applet code="SomeApplet.class
width="300"
height="100">
<param ....>
<param ... >
<param ... >
</applet> |
Of course, you don't have to indent the param tags, as I've done here, but it does make them easier to interpret.
As with all HTML tags, the param tag is not case sensitive. [It is in xhtml, however]. You may write <param>, <Param>, or even <PaRaM>, and it won't make any difference when it comes time to send your information to the applet.
Param Attributes
Each param tag has two required attributes: the name attribute and the value attribute. Each attribute is a string. If the attribute contains spaces or punctuation, you should enclose the attribute in quotes.
Here are two param tags. The first passes a parameter named message, with the value "Hi from HTML land". The second passes a parameter named UserNo with the value 12345:
<applet code="TestParam" width="400" height="100">
<param name=message
value="Hi from HTML land">
<param name="UserNo"
value="12345">
</applet> |
Notice that the name attribute message is not enclosed in quotes, but you may use quotes if you like, as done for the NAME attribute UserNo. [Note, in xhtml, message must be in quotes.] The value attribute "Hi from HTML land" must be enclosed in quotes because it contains embedded spaces. Otherwise, when you retrieve the value, you'll only get the first word.
There are three important points you should note about the param HTML tag:
- Remember that HTML tags and attibutes are not case sensitive. In this example, we could have used Message or MESSAGE, and both of them would have worked fine.
- Second, all param parameters are passed from the HTML file into your applet as Strings. If you need a numeric value--such as UserNo in the example above--you'll have to retrieve it as a String and then convert it inside your applet.
- Third, spaces at the beginning or end of a value are trimmed off when the value is retrieved, but spaces between characters are retained.
Retrieving Information with getParameter()
Inside your Java applet, use the getParameter() method to retrieve a parameter from the HTML file. When you call getParameter(), you pass the NAME you want to find--for instance message or UserNo--and getParameter() returns the VALUE associated with that NAME.
Thus, inside TestParam.java (used in the example above) you would write:
| String s1 = getParameter("message"); |
and getParameter() will locate the name message and return the value "Hi from HTML land" to be stored in s1.
Numeric Parameters
Even though you might like to write this, unfortunately it doesn't work:
| int userNo = getParameter("UserNo"); |
The getParameter() returns a String, not an int. To correctly process userNo in this example, you have to write something similar to this, at a miniumum:
String s2 = getParameter("UserNo");
int userNo = Integer.parseInt(s2); |
This code doesn't take into account the fact that userNo might contain a "non-numeric" String. You'll learn how to handle that problem in the next lesson, "Debugging and Exceptions."
Parameter Problems
As with any parameter passing scheme, if things can go wrong, they certainly will go wrong. Knowing this, Java's getParameter() method was designed to expect problems; when you write:
String arg1 = getParameter("messesge");
// Note misspelled parameter name |
when the actual PARAM NAME is "message", the getParameter() method returns null.
Every time you use getParameter() you should check for this null value and supply a default if necessary. In the example shown here, for instance, you could replace the code shown above with this:
String s1 = getParameter("message");
if (s1 == null)
{
s1 = "NO MESSAGE PARAM PASSED";
} |
Something to Talk About
Want to try out some parameter tags? OK! Download, compile and run TestParam.java. Use the example shown above to write your HTML file.
Once you have the program running, add one additional parameter to your HTML file and use getParameter() to retrieve and display it in TestParam.java. What changes did you need to make?
This is the last section of this lesson.
|