java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to read an applet parameters?

package com.javacoderanch.example.applet;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;

public class AppletParameterExample extends Applet {
    private String name = "";

    public void init() {
        //
        // Here we read a parameter named name from the applet tag definition
        // in our html file.
        //
        name = getParameter("name");
    }

    public void paint(Graphics g) {
        g.setColor(Color.BLUE);
        g.drawString("Hello " + name + ", Welcome to the Applet World.", 0, 0);
    }
}

Now we have the applet code ready. To enable the web browser to execute the applet create the following html page.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Parameterized Applet</title>
</head>
<body>
    <applet code="com.javacoderanch.example.applet.AppletParameterExample" 
            height="150" width="350">
        <param name="name" value="Mr. Bean" />
    </applet>
</body>
</html>