Entry
Java: How to pass parameter arguments to Java Applets?
Sep 7th, 2009 04:45
Knud van Eeden, Joe Bloggs,
----------------------------------------------------------------------
--- Knud van Eeden --- 25 May 2002 - 12:04 pm ---------------------
Java: How to pass parameter arguments to Java Applets?
---
1. To set the parameters, use the
<PARAM>
tag in your
<APPLET>
tag in your HTML file,
2. To get the parameters, use the
getParameter()
method in your Java file.
---
---
As your applets become more complex, one of your programming goals will
be to make the applets as generic as possible.
In other words, you want to make it very easy for end-users and
programmers to use the applets to support many different tasks.
One way to make your applets more generic is to let the applet get
information from the calling HTML file.
Assume, for example, that you have created an applet that scrolls a
company's name across the screen.
To make your applet more generic, you should let the applet get the
text it is to scroll from the HTML entry.
In this way, to support another company's logo, you need only to update
the HTML entry, as opposed to the Java applet itself (that is, having
to change that applet and then to recompile it).
To specify parameters within an HTML file, you use the PARAM attribute.
The format of the PARAM entry is as follows:
PARAM
NAME="yourparametername"
VALUE="yourparametervalue"
---
The following HTML entry, for example, uses the PARAM attribute to
specify two parameters: one that contains the company's name, and a
second that contains some extra information about the company.
--- cut here ---------------------------------------------------------
<HTML>
<TITLE
Applet: Parameter: Passing: Example
>
</TITLE>
<APPLET
CODE="AppletParameterPassingExample"
WIDTH="300"
HEIGHT="200"
>
<PARAM
NAME="companyname"
VALUE="SUN MicroSystems California USA"
>
<PARAM
NAME="companyinfo"
VALUE="the creators of the Java language"
>
</APPLET>
</HTML>
--- cut here ---------------------------------------------------------
---
Within your Java applet, you use the 'getParameter' function to get an
HTML parameter's value.
---
For example, the following statement uses the 'getParameter' function
to assign the company's name to a variable called 'companynameS':
String companynameS = getParameter( "companyname" )
If, for some reason, the HTML file does not specify a value for the
parameter, the 'getParameter' function returns the null value, which
your applet can test for as shown:
String companynameS = getParameter( "companyname" )
if ( companynameS == null ) {
companynameS = "unknown companyname";
}
---
The following program, 'AppletParameterPassingExample', uses the
'getParameter' function to get the value of several HTML parameters:
--- cut here ---------------------------------------------------------
// save as 'AppletParameterPassingExample.java'
//
import java.awt.*;
import java.applet.*;
//
public class AppletParameterPassingExample extends Applet {
//
String companynameS;
String companyinfoS;
//
public void PROCAppletParameterPassingExample() { // constructor
companynameS = getParameter( "companyname" );
if ( companynameS == null ) {
companynameS = "unknown companyname";
}
companyinfoS = getParameter( "companyinfo" );
if ( companyinfoS == null ) {
companyinfoS = "no company information given";
}
}
//
public void paint( Graphics g ) {
PROCAppletParameterPassingExample();
g.drawString( "company name =" + " " + companynameS, 5, 15 + 0 *
30 );
g.drawString( "company information =" + " " + companyinfoS, 5, 15 + 1
* 30 );
}
}
//
// create the following file, and save it as
// 'AppletParameterPassingExample.htm'
// Then run this HTML file in your browser.
/*
<HTML>
<TITLE
Applet: Parameter: Passing: Example
>
</TITLE>
<APPLET
CODE="AppletParameterPassingExample"
WIDTH="500"
HEIGHT="200"
>
<PARAM
NAME="companyname"
VALUE="SUN MicroSystems California USA"
>
<PARAM
NAME="companyinfo"
VALUE="the creators of the Java language"
>
</APPLET>
</HTML>
*/
--- cut here ---------------------------------------------------------
---
When you run this program, by loading the HTML file in your
browser, you should see:
company name = "SUN MicroSystems California USA"
company information = "the creators of the Java language"
---
---
Book: see also:
---
[book: source: Jamsa, Kris - Java Now! - ISBN 1-884133-30-4 - p.
126 'Using the PARAM attribute']
----------------------------------------------------------------------