March 23, 2015

Invoke Java Applet from JSF page

I have a Java Applet that will handle some operations on the client machine.
This Applet should be called upon client's request from a JSF page.

Development steps:
1. Managed Bean

1.1 boolean value to enable/disable calling Java Applet

private boolean showApplet = false;

1.2 return the Java Applet HTML invoker

public String getAppletHtml() {
    if (showApplet) {
        return "<object type=\"application/x-java-applet\" id=\"the applet\""
                + "code=\"ics.jvm.setup.JVMSetupApplet.class\" codebase=\"./\""
                + "archive=\"./the-applet.jar\" width=\"0\" height=\"0\">" + "<param name=\"force\" value=\"true\" />";
    }
    return "";
}

1.3 enable/disable action method

public String showApplet() {
    showApplet = true;
    return null;
}

2. JSF page
2.1 Write the calling tags
Note: You have to set the escape attribute of your tag to false. Because as default it's set to true and the HTML related character are escaped.

<h:commandLink value="Reset Configuration" action="#{bean.showApplet}" style="color: white; text-decoration: blink;">
    <h:outputText value="#{bean.appletHtml}" escape="false" />
</h:commandLink>


No comments: