Java Applet Parameter Passing
Java applet allows us to pass user defined parameters using <applet > Tag.
What is PARAM Tag
- Param tag is used to handle parameters passed to an html document.
- Param tag has 2 parameters - Name and value. where Name is the user defined name like variables and value is the value to be stored into the name.
- Param tag is written within the applet tag in comment section.
- When applet is loading the getParameter() method is used inside the init() method to access the parameters of param tag.
- The parameters are in string type, we need to typecast them if values are other than string type.
- We can write any number of param tags inside applet tag get parameters of different type.
Syntax
/* <applet code="AppletName.class" width=300 height=300>
<param name="param1" value="value1">
<param name="param2" value ="value2">
...
...
</applet> */
Example
Program to display message using param tag
/* <applet code="Test.class" width=300 height=300>
<param name="param1" value="Hello. I'm Applet">
</applet> */
import java.awt.*;
import java.applet.*;
public class Test extends Applet
{
String s=null;
public void init()
{
s = getParameter("val");
}
public void paint(Graphics g)
{
g.drawString(s, 15, 140);
}
}
Output