When you do a portal project, you need to log in to a C/s business system from the portal single point, so that you need to invoke the client's local SSO from the browser to enter the C/S business system. Think about it. There are two solutions, one is to write a DLL or OCX control to invoke SSO, and the second is to invoke SSO through the applet.
Finally, we chose the way of the applet, in the process of writing, found that the details of a lot of things, now I enumerate it out, I hope you less detours.
First, take a look at a simple applet.
Import java.applet.*;
Import java.awt.*;
public class Helloapplet extends Applet {
public void Paint (Graphics g) {
G.drawrect (0, 0, 499, 149);
g.DrawString ("Printing ...", 5, 70);
}
}
The following is an HTML page:
<HTML>
<HEAD>
<title>a Simple program</title>
</HEAD>
<BODY>
<CENTER>
<applet code= "Helloapplet.class" width= "height=" >
</APPLET>
</CENTER>
</BODY>
</HTML>
The premise of the operation is to copy the compiled Helloapplet.class and HTML in the same directory. This is a very simple example, but such simple examples are of little use in practical applications.
Second, next to learn the use of the applet's label.
Learn the properties of the applet label first:
Width:applet width on an HTML page
Height:applet the height on the HTML page
Name:applet a name on an HTML page that is used for multiple applets on an HTML page of a zone name.
ID: Specify an id attribute for the applet so that it can be treated as a simple object, using JavaScript to invoke its object's methods.
Archive: The URL of this applet when the applet is stored in a JAVA file or ZIP file.
Code: The URL that executes the class of the applet.
Codebase: Indicates the base URL of the applet, if the code attribute is relative.
Param: Passing parameters.
Of these properties, the code and codebase properties may be the most prone to errors. For example, you might see:
If my web project has a index.jsp page in the root directory, the page needs to invoke an applet, and the Helloapplet.java is under the COM package. The right thing to do is to first copy the Helloapplet.class to the same directory in index.jsp, and then
<applet code= "com. Helloapplet.class "id=" Helloapplet "codebase=". />
Attention:
1, the code must write the package name, codebase= "." The representation starts at the current directory of the index.jsp.
2, be sure to put the helloapplet.class copy out of the JSP page directory, if placed under web-info/classes/com/will be wrong, because the Web-inf folder security is relatively high, the client can not access.
Three, we look at a complex example, this example in the front page needs to invoke the applet is not a class, so you need to call a jar package, and this applet needs to invoke the client local application.
(1) Applet class:
Package com;
Import java.applet.*;
Import java.awt.*;
Import java.io.IOException;
public class Helloapplet extends Applet {
public void Paint (Graphics g) {
G.drawrect (0, 0, 499, 149);
g.DrawString ("Hello World", 5, 70);
}
/**
* Call the local calculator through the applet
*/
public void Openapp () {
String url = "C://windows//system32//calc.exe";
System.out.println ("url=" + URL);
try {
Runtime.getruntime (). exec (URL);
catch (IOException e) {
E.printstacktrace ();
}
}
}
(2) JSP page
<%@ page language= "java" import= "java.util.*" pageencoding= "GBK"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>demo</title>
<body>
<applet code= "com. Helloapplet.class "id=" Helloapplet "codebase=". ARCHIVE = "Helloapplet.jar" width= "height=" >
</APPLET>
<p><a href= "#" onclick= "Openapp ()" > Calculator </a></p>
<script type= "Text/javascript" >
function Openapp ()
{
var helloapplet = document.getElementById ("Helloapplet");
Helloapplet.openapp ();
}
</script>
</body>
(3) The Helloapplet into a jar package, because in the actual application of a class can solve the problem is very little. CMD switch to the web-info/classes directory, packaged by the following command:
JAR-CVF Helloapplet.jar *.*
(4) To enable the applet to access the local application, you need to sign it.
Keytool-genkey-keystore Hello.store-alias Hello
This command is used to generate a keystore, which should produce a hello.store file after execution, and you can modify the name. Also in the execution of the command when there are prompts you to enter the secret key library password, here you must remember, or later to use when you can not enter. Enter the password, it will prompt you to enter the user name, the company name and other information, these are not required to enter, you can directly jump back, and finally asked you whether the correct input, you can enter Y or N to answer the correct or wrong.
Keytool-export-keystore Hello.store-alias Hello-file Hello.cert
This command is used to generate a certificate to use when the signature is executed, and a Pepper.cert file is generated when the command is finished.
Jarsigner-keystore Hello.store Helloapple
This command uses the certificate generated above to sign our jar file. Finally, the jar package is copied to the location directory of the JSP file.
(5) because the applet is running as a sandbox mechanism, security settings need to be made when invoking the local program. In the Jre/lib/security/java.policy file where you run the applet, add permission java.security.AllPermission at the end of the note: If the client has multiple JRE installed, You need to find out which JRE is actually running the applet.
(6) At the time of running, the first time you will be prompted to trust the applet, click to make quasi trust, and then run, the next time will not be prompted.
Note: In the browser debugging applet, you can open the Java console, located in the IE Tool menu of the Java console.