Applet - Portlet Communication

From time to time there is the need to send data from a portlet to an applet and back. For example if you want to access a USB - Stick. Here is how you do it:

Sending data from a portlet to an applet

This way is rather easy: You instantiate the applet, you pass the information using applet parameters and read those parameters in the applet.

Here is a snippet showing you how to instantiate your applet:

<applet id="yourApplet"
archive="yourApplet.jar, someHelperJar.jar"
<param name="parameter" value="#{bean.parameterValue}" />


code: This is your class extending java.applet.Applet, your applet main class.
archive: This is a list of jars that contain your applet (packed as a jar) and all jars your applet might need.
codebase: The path to access your portlet and by having this, to access the 
applet jar.
param: This is the way to send data to your portlet: add applet parameter and 
point them to values in your bean.

You can access those parameters in the init() method of your applet like this:

public void init() {
        final String  inputStr = getParameter("parameter");    

Sending data from an applet to a portlet

To let the applet communicate with the portlet you can use javascript:

import java.applet.*;
import java.net.*;

public class myApplet extends Applet{
  public void init(){
    long var = 5;
    try {
        (new URL("javascript:process(\"" + var+"\")"));
    catch (MalformedURLException me) { }

This will call a javascript function on your portlet page with the parameter "var". You can use these parameters to set values in (hidden) text fields and then click a (hidden) button. This allows you to return processed data to the portlet and to tell the portlet what to do.

Just for the sake of completeness ... this is how you specify hidden input fields and buttons in ICEFaces:

Hidden InputField:
<ice:inputHidden value="test"></ice:inputHidden>

Hidden Button:

<ice:commandButton visible="false" value="Click Me"></ice:commandButton> 

If you liked this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)
If you have any questions, feel free to leave a comment.