Deploying a .jar into a html page

爷,独闯天下 提交于 2020-01-25 04:31:38

问题


So I have the following code in Java which deploys an applet into a html page.

Due to security problems I'm trying to make it a .jar and then sign that .jar to see first how that will work.

Unfortunately I'm not really sure what I should change in order to run it as a .jar, given that the main class in the .jar is the same:

p.println(document+"<applet name=\"myApp\" codebase="+codebase+" code="+code+ " width='+(scnWid-30)+' height='+(scnHei-45)+'>');");
        p.println(document+"<param name=user value=\""+user+"\">');");
        p.println(document+"<param name=sessionid value=\""+sessionid+"\">');");

Where codebase and code are taken here :

public void init(ServletConfig config) throws ServletException {
            super.init(config);
            ServletContext context = getServletContext();
            applet_code_base = context.getInitParameter("applet_code_base");
            applet_code = context.getInitParameter("applet_code");

          }

回答1:


The Oracle website at this location says:

To start any applet from an HTML file for running inside a browser, you use the applet tag. For more information, see the Java Applets lesson. If the applet is bundled as a JAR file, the only thing you need to do differently is to use the archive parameter to specify the relative path to the JAR file.

It claims that

<applet code=TicTacToe.class 
        archive="TicTacToe.jar"
        width="120" height="120">
</applet>

is the appropriate html code to display an applet stored as TicTacToe.jar in the same directory as the html file.




回答2:


Reference Deploying An Applet In Under 10 Minutes :

  1. Compile / build your applet's Java code and make sure all class files and resources such as images etc. are in a separate directory, example build/components.

  2. Create a jar file containing your applet's class files and resources.

    cd build

    jar cvf DynamicTreeDemo.jar components

  3. Sign your jar file if the applet needs special security permissions, for example, to be launched in a modern JRE with default settings. By default, unsigned code will be blocked.

    jarsigner -keystore myKeyStore -storepass abc123 -keypass abc123 DynamicTreeDemo.jar johndoe

    where keystore is setup and located at "myKeyStore" alias is "johndoe" keystore password and alias password are "abc123"

  4. Create a JNLP file that describes how your applet should be launched.

dynamictree-applet.jnlp

<?xml version="1.0" encoding="UTF-8"?>
<jnlp spec="1.0+" codebase="" href="">
    <information>
        <title>Dynamic Tree Demo</title>
        <vendor>Dynamic Team</vendor>
    </information>
    <resources>
        <!-- Application Resources -->
        <j2se version="1.6+"
              href="http://java.sun.com/products/autodl/j2se"
              max-heap-size="128m" />

        <jar href="DynamicTreeDemo.jar" main="true" />

    </resources>
    <applet-desc 
         name="Dynamic Tree Demo Applet"
         main-class="components.DynamicTreeApplet"
         width="300"
         height="300">
     </applet-desc>
</jnlp>     
  1. Create the HTML page that will display the applet. Invoke the runApplet function from the Deployment Toolkit to deploy the applet.

AppletPage.html

<body>
    ....
    <script src="http://java.com/js/deployJava.js"></script>

    <script> 
        var attributes = { code:'components.DynamicTreeApplet',  width:300, height:300} ; 
        var parameters = {jnlp_href: 'dynamictree-applet.jnlp'} ; 
        deployJava.runApplet(attributes, parameters, '1.6'); 
    </script>
    ....
</body>                 
  1. For this example, place DynamicTreeDemo.jar, dynamictree-applet.jnlp, and AppletPage.html in the same directory on the local machine or a web server. A web server is not required for testing this applet.

  2. View `AppletPage.html in a web browser. The Dynamic Tree Demo Applet will be displayed. View Java Console Log for error and debug messages.

For more information see Deployment Toolkit 101 - Java Tutorials Blog



来源:https://stackoverflow.com/questions/27373757/deploying-a-jar-into-a-html-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!