...
javascript 与applet的通讯 和java与c(dll文件)通讯,最终在b/s架构下实现与硬件(读卡器)通讯
在b/s架构下怎样实现客户端的硬件(读卡器)设备通讯,得到硬件(读卡器)的数据。在此与硬件设备通讯的C或其它的通讯设备语言是由
商提供的。我只负责实现javascript与applet通讯和jni与c通讯.在此我们要解决的问题是:
1)javascript和applet怎样通讯?
2)java怎样调用c来实现和硬件通讯?
3)由于applet的安全级别非常的高,那么怎样实现安全认证或数字签名?
4)由于在B/S架构下程序一般放在服务器端,那么怎样来实现对客户端的硬件(读卡器)操作呢?
用一个实现实例来说明上述问题:
1)写一个java程序利用jni来调用用c产生的dll(动态连接库)如下HelloWorld.java
java 代码
-
public class HelloWorld {
-
public native void displayHelloWorld();
-
public native String getUserName();
-
static {
-
System.loadLibrary("hello");
-
-
}
-
-
-
-
public HelloWorld(){
-
}
-
public static void main(String[] args) {
-
-
String s = new HelloWorld().getUserName();
-
System.out.println("successful "+s);
-
-
}
-
}
2)用javac编译HelloWorld
3)用javah产生HelloWorld.h文件。它好比java中的一个接口。
c 代码
-
-
#include <jni.h></jni.h>
-
-
- #ifndef _Included_HelloWorld
- #define _Included_HelloWorld
-
#ifdef __cplusplus
-
extern "C" {
-
#endif
-
-
-
-
-
-
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
-
(JNIEnv *, jobject);
-
-
-
-
-
-
-
JNIEXPORT jstring JNICALL Java_HelloWorld_getUserName
-
(JNIEnv *, jobject);
-
-
#ifdef __cplusplus
-
}
- #endif
-
#endif
4)编写一个*.c(HelloWorldImp.c)的文件。它好比一个接口的实现。
c代码
- #include <jni.h></jni.h>
- #include "HelloWorld.h"
-
#include <stdio.h></stdio.h>
-
-
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
-
{
-
printf("Hello world!\n");
-
-
}
-
-
JNIEXPORT jstring JNICALL Java_HelloWorld_getUserName (JNIEnv *env, jobject obj){
-
jstring jstr;
-
char str[]=" Rami very good! yes!\n";
-
jstr = (*env)->NewStringUTF(env,str);
-
-
return jstr;
-
-
}
5) 编写一个applet程序来调用HelloWord。Applet1.java
java 代码
-
import java.applet.Applet;
-
import java.awt.*;
-
-
-
-
public class Applet1 extends Applet {
-
-
-
-
public String UserName="applet call jni";
-
-
public void init(){
-
try{
-
System.out.println("test");
-
-
System.out.println(new HelloWorld().getUserName());
-
System.out.println("test end");
-
}catch(Exception e){
-
e.printStackTrace();
-
System.out.println(e);
-
}
-
}
-
-
public void paint(Graphics g) {
-
g.drawString("Hello:"+UserName,20,20);
-
}
-
-
-
-
public void setUserName(String UserName){
-
this.UserName = UserName;
-
}
-
public String getUserName(){
-
return UserName;
-
}
-
-
public void test(){
-
try{
-
System.out.println("test");
-
-
UserName = new HelloWorld().getUserName();
-
System.out.println();
-
-
System.out.println("test end******");
-
}catch(Exception e){
-
e.printStackTrace();
-
System.out.println(e);
-
}
-
}
-
}
6)编写一个jsp文件调用applet。jniApplet.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
<script language="JavaScript">
function changeUserName(){
aaa = new String(document.form1.UserName.value);
document.myApplet.setUserName(aaa);
var s = document.myApplet.getUserName();
document.myApplet.test();
var ss = document.myApplet.getUserName();
alert(ss);
document.form1.UserName.value = ss;
document.myApplet.repaint();
return false;
}
</script>
</head>
<body>
<table>
<tr>
<td align="center" >
<applet ID='myApplet' Name='myApplet' code='Applet1.class' width='120' height='30' codebase=".\jar" archive="monitor.jar">
</applet>
</td>
<td align="center" valign="bottom" nowrap>
<form name=form1 >
Your Name:<input name=UserName size=10>
<input type=Button name=Btn value="Change Name" language="JavaScript" onclick="return changeUserName();">
</form>
</td>
</tr>
</table>
</body>
</html>
7)对刚才编译出来的文件进行打包,这样有利于applet的下载速度。
jar cvf monitor *.*
8)部署web程序
9)对客户端修改安全权限文件。此文件在jdk的安装目录下。但测试发现在C:\Program Files\Java\j2re1.4.2_06\lib\security也有一个安全文件,也许是浏览调用的安全文件,就修改它。
java 代码
-
-
-
-
keystore "file:/C:/j2sdk1.4.2_06/jre/lib/security/cacerts", "JKS";
-
-
grant codeBase "file:${java.home}/lib/ext/*" {
-
permission java.security.AllPermission;
-
};
-
grant signedBy "monitor", codeBase "http://192.168.1.35:8080/" {
-
permission java.security.AllPermission, signedBy "monitor";
-
permission java.lang.RuntimePermission "usePolicy";
-
permission java.lang.RuntimePermission "createClassLoader", signedBy "monitor";
-
permission java.lang.RuntimePermission "getClassLoader";
-
permission java.lang.RuntimePermission "setContextClassLoader";
-
};
-
-
grant {
-
permission java.lang.RuntimePermission "stopThread";
-
permission java.net.SocketPermission "localhost:1024-", "listen";
-
permission java.util.PropertyPermission "java.version", "read";
-
permission java.util.PropertyPermission "java.vendor", "read";
-
permission java.util.PropertyPermission "java.vendor.url", "read";
-
permission java.util.PropertyPermission "java.class.version", "read";
-
permission java.util.PropertyPermission "os.name", "read";
-
permission java.util.PropertyPermission "os.version", "read";
-
permission java.util.PropertyPermission "os.arch", "read";
-
permission java.util.PropertyPermission "file.separator", "read";
-
permission java.util.PropertyPermission "path.separator", "read";
-
permission java.util.PropertyPermission "line.separator", "read";
-
permission java.util.PropertyPermission "java.specification.version", "read";
-
permission java.util.PropertyPermission "java.specification.vendor", "read";
-
permission java.util.PropertyPermission "java.specification.name", "read";
-
permission java.util.PropertyPermission "java.vm.specification.version", "read";
-
permission java.util.PropertyPermission "java.vm.specification.vendor", "read";
-
permission java.util.PropertyPermission "java.vm.specification.name", "read";
-
permission java.util.PropertyPermission "java.vm.version", "read";
-
permission java.util.PropertyPermission "java.vm.vendor", "read";
-
permission java.util.PropertyPermission "java.vm.name", "read";
-
permission java.lang.RuntimePermission "createClassLoader";
-
permission java.lang.RuntimePermission "getClassLoader";
-
permission java.lang.RuntimePermission "setContextClassLoader";
-
-
permission java.security.AllPermission;
-
permission java.lang.RuntimePermission "usePolicy";
-
permission java.lang.RuntimePermission "createClassLoader";
-
permission java.lang.RuntimePermission "getClassLoader";
-
permission java.lang.RuntimePermission "setContextClassLoader";
-
};
-
-
grant {
-
permission java.lang.RuntimePermission "loadLibrary.hello";
-
};
10)在客户端配置dll。在环境变量path中设置dll所在的位置。
就这样应该大功告成了,
不知道各位有没有这样的需求,有没有更好的方法呢? 能给出意见和建议吗?谢谢