简体   繁体   中英

Javascript call java applet signed

My function applet

public String sign(String data) {
    String rs = "";
    if (data != null && !data.isEmpty()) {
        loadKeyStore();
        if (lsCertificateId.size() > 0) {
            selectCertId();
        //doSign
            if (password != null && !password.isEmpty()
            && selectedId != null && !selectedId.isEmpty()) {
                byte[] signedData = doSign(data.getBytes());
                rs = Base64.encode(signedData);
            }
        }
    }
    return rs;
}

I call function applet from javascript, I had allowed Java (TM) run on browser

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        <script type="text/javascript" src="jquery-2.1.1.min.js" ></script>
        <form>
            <textarea id="txtData" ></textarea>
            <br/>
            <input type="button" id="btnSign" value="Sign"  />
            <br/>
            <div id="lblSignedData"></div>
            <!--            <object id="appSign" width="300" height="100" type="application/x-java-applet" >
                            <param name="scriptable" value="true" />
                            <param name="mayscript" value="true" />
                            <param name="archive" value="applet/DsApplet.jar" />
                            <param name="code" value="dsapplet.DsApplet" />                
                        </object>-->
            <!--                        <object type="application/x-java-applet" height="300" width="550">
                                        <param name="code" value="Sample" />
                                        <param name="archive" value="Sample.jar" />
                                        Applet failed to run.  No Java plug-in was found.
                                    </object>-->
            <applet width="1" height="1" id="appSign" 
                    archive="applet/DsApplet.jar"
                    code="com.alupvn.digitalbill.applet.SignerApplet">
                <param name="signedAlgorithm" value="SHA1withRSA" />
                <param name="dlls" value=" vdctdcsp11.dll,vnpt-ca_csp11.dll,BkavCA.dll,vnpt-ca_v34.dll,viettel-ca.dll,ShuttleCsp11_3003.dll,ngp11v211.dll,st3csp11.dll,gclib.dll,fpt-ca.dll,CA2_v34.dll,CA2_csp11.dll,psapkcs.dll,ostc1_csp11.dll,fpt-ca-stx.dll,viettel-ca_v1.dll,viettel-ca_v2.dll,viettel-ca_v3.dll,etpkcs11.dll,U1000AUTO.dll,safe-ca.dll,eToken.dll,Vina-CA.dll,Vina-CA_s.dll,vnpt-ca_cl_v1.dll,ostt1_csp11.dll,ostt2_csp11.dll,ostt3_csp11.dll,ostc2_csp11.dll,viettel-ca_v4.dll,viettel-ca_v5.dll,viettel-ca_v6.dll,Vina-CAv3.dll,Vina-CAv4.dll,Vina-CAv5.dll,nca_eps2k2a.dll,nca_eps2k3a.dll" />
            </applet>


            <script type="text/javascript">
                $("#btnSign").click(function() {
                    var dataSigned = document.appSign.sign($("#txtData").val());
                    $("#lblSignedData").html(dataSigned);
                    //                alert("adsf");
                });
            </script>
            <!--<script src="http://www.java.com/js/deployJava.js"></script>-->
            <!--            <script type="text/javascript">
                            var attributes = {id: 'appSign', code: 'applet.DsApplet',
                                archive: 'DsApplet.jar',
                                width: 300, height: 100};
                            var parameters = {jnlp_href: 'DsApplet.jnlp'};
                            deployJava.runApplet(attributes, parameters, '1.6');
                            alert(document.appSign.getArch()) ;
                        </script>-->
        </form>
    </body>
</html>

When I open the HTML file I get error:

var dataSigned = document.appSign.sign($("#txtData").val()); is not a function

Does anyone know how to fix it?

  1. Sign your applet and all the .jar dependencies with a certificate.
  2. Populate your manifest with all the tags mentioned below (it's in xml because I use maven, you can write in the way you prefer)
<codebase>http://location.of.your.jar/</codebase>
<permissions>all-permissions</permissions>
<Application-Library-Allowable-Codebase>http://location.of.your.jar/</Application-Library-Allowable-Codebase>
<Manifest-Version>1.0</Manifest-Version>
<Implementation-Title>App Name</Implementation-Title>
<Implementation-Version>0.1.0</Implementation-Version>
<Application-Name></Application-Name>
<Created-By>1.8.0_45</Created-By>
<Main-Class>package.YourClass</Main-Class>
<mode>development (or production)</mode>
<url>url of the application</url>
  1. Surround your java method with the doPrivileged
  2. Be sure that your browser has the java plugin enabled
  3. Put your http path of your web app in the java exception list
  4. If your url has _ (underscore/underline) probably it won't be recognized.
  5. Try to move your .jar to the same folder of your html, not using the /applet folder.
  6. Take a look on this post , I was having a similar issue.

Remember, this error saying that 'is not a function' is because your .jar is not loading - or you made something wrong with the js syntax, what I don't think so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM