Dynamic generation of jnlp using jsp (Java Web Star)

If you are reading this article, I am assuming that you have some knowledge about JNLP (Java Web Start) and want to make the jnlp dynamic.

Typical JNLP file looks like :

<jnlp spec="1.0+" codebase="http://localhost:8080/" href="Test.jnlp">
    <information>
        <title>Jnlp Testing</title>
        <vendor>Test Vendor</vendor>
        <homepage href="http://localhost:8080/" />
        <description>Testing Testing</description>
    </information>
    <security>
        <all-permissions/>
    </security>
    <resources>
        <j2se version="1.6+" />
        <jar href="TestJnlp.jar" />
    </resources>
    <application-desc main-class="com.test.TestJnlp">
                <argument>some argument</argument>
        </application-desc>
</jnlp>

To make your jnlp dynamic, copy the content of your jnlp into a jsp file and add few lines to make it dynamic.

  • Add this at the top of the jsp
<%
    response.setHeader("Pragma", "no-cache");
    response.setHeader("Expires", "0");
    response.setContentType("application/x-java-jnlp-file");
%>

The above scriplet will remove the page caching and set the page ContentType to jnlp since this is actually a jsp file but it will be rendered as jnlp file.

  • change href and main argument to get data from request attribute.
<jnlp codebase='<%=request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ request.getContextPath() + "/yourpath" %>'>

......................

<application-desc main-class="com.test.TestJnlp">
       <argument><%=request.getParameter("test_param")%></argument>
</application-desc>
</jnlp>

Hence using above code when you try to render it,you will get a dynamic jnlp in form of jsp which can be executed as jnlp.Complete example is given below.

<jnlp spec="1.0+" codebase='<%=request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ request.getContextPath() + "/yourpath" %>'>
    <information>
        <title>Jnlp Testing</title>
        <vendor>Test Vendor</vendor>
        <homepage href="http://localhost:8080/" />
        <description>Testing Testing</description>
    </information>
    <security>
        <all-permissions/>
    </security>
    <resources>
        <j2se version="1.6+" />
        <jar href="TestJnlp.jar" />
    </resources>
    <application-desc main-class="com.test.TestJnlp">
                <argument><%=request.getParameter("test_param")%></argument>
        </application-desc>
</jnlp>

Few point to remember:
1. We should not use codebase attribute because it is used to verify the server copy of jnlp and in our case it is a dynamic one, so we should not use it.
2. In order to get request parameter while jnlp generation we have to provide those parameters to the jnlp using url param.
Something like:

http://localhost:8080/mycontext/mydynamicjnlp.jsp?test_param=somevalue

Which will provide the necessary value for substitution and let you download final jnlp.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s