Share on Google+Share on Google+

Applet - Passing Parameter in Java Applet

In this java applet tutorial, you will learn the passing parameter in java applet.

Applet - Passing Parameter in Java Applet



Java applet has the feature of retrieving the parameter values passed from the html page. So, you can pass the parameters from your html page to the applet embedded in your page. The param tag(<parma name="" value=""></param>) is used to pass the parameters to an applet. For the illustration about the concept of applet and passing parameter in applet, a example is given below.

In this example, we will see what has to be done in the applet code to retrieve the value from parameters. Value of a parameter passed to an applet can be retrieved using getParameter() function. E.g. code:

String strParameter = this.getParameter("Message");

Printing the value:

Then in the function paint (Graphics g), we prints the parameter value to test the value passed from html page. Applet will display "Hello! Java Applet"  if no parameter is passed to the applet else it will display the value passed as parameter. In our case applet should display "Welcome in Passing parameter in java applet example." message.

Here is the code for the Java Program : 

import java.applet.*; 
import java.awt.*; 
public class appletParameter extends Applet {
private String strDefault = "Hello! Java Applet.";
public void paint(Graphics g) {
  String strParameter = 
 if (strParameter == null)
  strParameter = strDefault;
  g.drawString(strParameter, 50, 25);

Here is the code for the html program : 

Passing Parameter in Java Applet</TITLE>

This is the applet:<P>
<APPLET code="appletParameter.class" width="800" height="100">
<PARAM name="message" value="Welcome in Passing parameter in java applet example.">

There is the advantage that if need to change the output then you will have to change only the value of the param tag in html file not in java code.

Compile the program : 

Output after running the program : 
To run the program using appletviewer, go to command prompt and type appletviewer appletParameter.html Appletviewer will run the applet for you and and it should show output like
Welcome in Passing parameter in java applet example. Alternatively you can also run this example from your favorite java enabled browser.

Try the example online

Download this example.



Posted on: April 14, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Applet - Passing Parameter in Java Applet   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
Neeraj Sharma
August 16, 2011
Core Java

Simple & Good
January 5, 2012

Be careful of the capitalization in java. This program should not work as written, as the <applet> param name is "message" (lowercase m) while the java code is looking for a parameter named "Message" (capital M). Message and message are two different variable names.
April 4, 2012

this is most tuffest subject that we are in trouble.
June 30, 2013
Advance java Programing