Monday, April 16, 2012

Java 7: Reduce Applet Loading time using JNLP

Prior to Java 7, the JNLP file has to be downloaded first from the network before the applet can be launched. But with Java 7, the JNLP file can be embedded directly into the HTML code which reduces the amount of time the applet loading time. Following are the steps to embed a JNLP file:
  1. Create a JNLP file for your applet.
  2. Encode the contents of the JNLP file using the Base64 scheme.
  3. When deploying the applet in a web page, specify the 'jnlp_embedded parameter' with it's value set to the Base64 encoded JNLP string.
Example:
<script>
    var attributes = {} ;
    <!-- Base64 encoded string truncated below for readability -->
    var parameters = {jnlp_href: 'xyz-applet.jnlp',
        jnlp_embedded: 'PCEtLSAKLyoKICogQ29weXRSSDSDSD+-Dgkhmnwrr ...'
    } ;
    deployJava.runApplet(attributes, parameters, '1.6');
</script>
Note: Some encoding tools may wrap the encoded string into several 76-column lines. To use this multi-line attribute value in JavaScript code, specify the attribute value as a set of concatenated strings.

Friday, April 13, 2012

Java 7: Diamond Operator in Generics


Java 7 promotes the concept of convention over configuration. It simplifies the use of generics by using the diamond operator(<>) for constructor type inference. When using the diamond operator, the compiler infers the parameter types for the constructors which avoids explicit specification of the object. Before Java7:
List list = new ArrayList();
With Java 7, the diamond operator makes this shorter and simpler. It avoids unchecked warnings in a program and also reduces generic verbosity.
List list = new ArrayList<>();
In above case, compiler automatically infers 'String' for the constructor of ArrayList.