问题
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 :
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.Create a
jarfile containing your applet's class files and resources.cd buildjar cvf DynamicTreeDemo.jar componentsSign your
jarfile 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 johndoewhere keystore is setup and located at "myKeyStore" alias is "johndoe" keystore password and alias password are "abc123"
Create a
JNLPfile 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>
- 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>
For this example, place
DynamicTreeDemo.jar,dynamictree-applet.jnlp, andAppletPage.htmlin the same directory on the local machine or a web server. A web server is not required for testing this applet.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