Friday, October 24, 2008

Sharing Java Applet Sessions in Firefox

There is a known problem in JEP (Java Embedding Plugin) for Firefox in which new sessions (or no sessions at all) are used for every new Java applet request to a servlet. This can be problematic for applets that requests data or URL from a servlet requiring authentication or a login. To get around this problem, you can pass in the current HTLM document's cookie as a Java applet parameter, and then use the cookie in the HttpServletRequest's Cookie request parameter.

In your HTML applet code, just add the document's cookie as a parameter. (Note that I'm writing the HTML applet code in JavaScript to dynamically get the document's cookie.)

<script language="JavaScript" type="text/javascript">
<!--
   document.write("<embed");
   document.write("name='MyApplet'");
   document.write("type='application/x-java-applet;version=1.6'");
   document.write("code='org/puguasoft/examples/MyApplet.class'");
   document.write("codebase='/applets/'");
   document.write("browserCookie='" + document.cookie + "'");
   document.write("</embed>");
-->
</script>
Then in your servlet, use the browserCookie parameter value as the Cookie property in the HttpServletRequest.
import javax.swing.JApplet;
import java.net.URL;
import java.net.URLConnection;

public class MyApplet extends JApplet{

   public void doIt(URL url){
      URLConnection urlConnection = url.openConnection();
      String cookie = getParameter("browserCookie");
      urlConn.setRequestProperty("Cookie", cookie);
      urlConnection.connect();

      // Now you can do what you need with the URLConnection
      // (e.g. open up an InputStream to read the data)
      // using the same cookie session.


   }
}

For more information check out these postings:


Friday, October 10, 2008

Detecting Browser Java Version Using Javascript

Trying to detect a client's browser for the installed Java plugin version and whether or not it's installed, can be a real pain. Luckily,Eric Gerds has created a JavaScript library to do just that. The JavaScript can detect the version of the Java plugin, if any, on different browsers including FireFox, Internet Explorer, Safari, and Opera. In addtion, his script can also detect QuickTime, Flash, Windows Media Player, and many other browser plugins.

Check out the PluginDetect JavaScript: http://www.pinlady.net/PluginDetect/