Java Applet Parameter Passing जावा एप्लेट में पैरामीटर पास करना

Java Applets allow you to interact with HTML and pass user inputs directly using the <param> tag.

जावा एप्लेट HTML के साथ इंटरैक्ट कर सकता है और <param> टैग का उपयोग करके यूजर इनपुट को सीधे पास किया जा सकता है।

This is useful when you need to input values from forms or user interactions in a webpage.
यह तब उपयोगी होता है जब आपको वेबपेज में फॉर्म या यूजर इंटरैक्शन से इनपुट प्राप्त करना होता है।


🔷 Passing Parameters in Java Applet जावा एप्लेट में पैरामीटर पास करना

The <param> tag is used within <applet> to pass values to the Java program.
जावा प्रोग्राम को वैल्यू पास करने के लिए <applet> के अंदर <param> टैग का उपयोग किया जाता है।

✨ Attributes of <param>

<param> टैग के ऐट्रिब्यूट्स

  • name: The parameter name.
    name: वह नाम जिसे पैरामीटर के रूप में पास किया जाएगा।

  • value: The value of the parameter.
    value: वह वैल्यू जो name एट्रिब्यूट के अंतर्गत पास की जाएगी।

✅ Syntax: प्रारूप:

<param name="parameterName" value="parameterValue">

🔷 Accessing Parameters using getParameter() 

getParameter() के माध्यम से पैरामीटर एक्सेस करना

This method retrieves the value of the parameter inside the applet.
यह मेथड एप्लेट में पास किए गए पैरामीटर की वैल्यू प्राप्त करता है।

✅ Syntax: प्रारूप:

String value = getParameter("parameterName");

🧪 Example Program - Parameter Passing

✅ HTML File

<html>
<head><title>Applet Parameter Example</title></head>
<body>
<applet code="UserApplet.class" width="300" height="300">
  <param name="userName" value="Ashwani Verma">
</applet>
</body>
</html>

✅ Java File

import java.applet.*;
import java.awt.*;

public class UserApplet extends Applet {
    String username;

    public void start() {
        username = getParameter("userName");
    }

    public void paint(Graphics g) {
        g.drawString("Welcome, " + username, 30, 30);
    }
}

Output: The applet will display – “Welcome, Ashwani Verma”


📌 Final Notes अंतिम टिप्पणियाँ

  • Always use getParameter() carefully to avoid NullPointerException.
    getParameter() का उपयोग सावधानीपूर्वक करें जिससे NullPointerException न हो।

  • Use default values when parameters are not passed.
    जब पैरामीटर पास न हो, तब डिफ़ॉल्ट वैल्यू का प्रयोग करें।

  • Applets are deprecated in modern browsers, so prefer JavaFX or web-based Java in new projects.
    एप्लेट्स अब आधुनिक ब्राउज़र में deprecated हैं, इसलिए नए प्रोजेक्ट्स में JavaFX या web-based Java का उपयोग करें।

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध