How to access parameters within Applet tag
Published: January 5, 2010 , Updated: January 4, 2010 , Author: mkyong
In this example, you will see how Applet use “Applet.getParameter()” method to read the parameters within Applet’s tag – “paramWidth”, “paramHeight” and “paramUrl”
<applet width=500 height=500 code="com.mkyong.applet.AppletExample.class"> <param name="paramWidth" value="300"> <param name="paramHeight" value="300"> <param name="paramUrl" value="http://www.mkyong.com"> </applet>
1. Create an Applet class
Create an Applet class, and get the Applet’s parameters with getParameter() method.
package com.mkyong.applet; import java.applet.*; import java.awt.Graphics; public class AppletExample extends Applet { String paramWidth; String paramHeight; String paramUrl; public void init() { paramWidth = this.getParameter("paramWidth"); paramHeight = this.getParameter("paramHeight"); paramUrl = this.getParameter("paramUrl"); } public void paint( Graphics g ) { StringBuffer sb = new StringBuffer() .append(" width : ").append(paramWidth) .append(" height : ").append(paramHeight) .append(" url : ").append(paramUrl); g.drawString(sb.toString(), 0,100); } }
2. Create a HTML page
<html> <head><title>Testing</title></head> <body> <applet width=500 height=500 code="com.mkyong.applet.AppletExample.class"> <param name="paramWidth" value="300"> <param name="paramHeight" value="300"> <param name="paramUrl" value="http://www.mkyong.com"> </applet> </body> </html>
3. Done
Run it and you will see the following output :
width : 300 height : 300 url : http://www.mkyong.com
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~