In this article we will learn about passing parameters to applets using the param tag and retrieving the values of parameters using getParameter method.
Parameters specify extra information that can be passed to an applet from the HTML page. Parameters are specified using the HTML’s param tag.
Param Tag
The <param> tag is a sub tag of the <applet> tag. The <param> tag contains two attributes: name and value which are used to specify the name of the parameter and the value of the parameter respectively. For example, the param tags for passing name and age parameters looks as shown below:
<param name=”name” value=”Ramesh” />
<param name=”age” value=”25″ />
Now, these two parameters can be accessed in the applet program using the getParameter() method of the Applet class.
getParameter() Method
The getParameter() method of the Applet class can be used to retrieve the parameters passed from the HTML page. The syntax of getParameter() method is as follows:
String getParameter(String param-name)
Let’s look at a sample program which demonstrates the <param> HTML tag and the getParameter() method:
import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet
{
String n;
String a;
public void init()
{
n = getParameter("name");
a = getParameter("age");
}
public void paint(Graphics g)
{
g.drawString("Name is: " + n, 20, 20);
g.drawString("Age is: " + a, 20, 40);
}
}
/*
<applet code="MyApplet" height="300" width="500">
<param name="name" value="Ramesh" />
<param name="age" value="25" />
</applet>
*/
Output of the above program is as follows:
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
how to pass an integer value using param …
In the Java code (class) you can convert a string into integer using Integer.parseInt() method.
ok ok
this code is not working
Can you explain more? How are you executing the applet?
you might be using java version above 8.
Applets aren’t supported after Java 8 i.e Java 9 onwards, Applet is deprecated: https://docs.oracle.com/javase/9/docs/api/java/applet/package-summary.html
If you’re still willing to work with applet, install Java 8 and here’s the documentation of what you can do with applet:
https://docs.oracle.com/javase/9/docs/api/java/applet/Applet.html
Need to create a html file to execute this code
it is so help full to me thanks.