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
public class HelloWorld {
public native void displayHelloWorld();//注意关键字native,这就说明这个方法是用本地方法实现的。
public native String getUserName();//从(本地文件)c 程序中返回一个值
static {//静态代码段里面导入了hello.dll。
System.loadLibrary("hello");//加载一个动态连接(dll)文件,在调用时它用最早的调用到内存中
//System.load("D:\\Tomcat 5.0\\webapps\\test\\jar\\hello");//能过指定的绝对路径来加载
}
/**//*
*构造方法
*/
public HelloWorld(){
}
public static void main(String[] args) {//调用本类的displayHelloWorld方法,(当然了方法实际上是用c语言实现的)
//new HelloWorld().displayHelloWorld();
String s = new HelloWorld().getUserName();
System.out.println("successful "+s);
//System.out.println(s.length());
}
}
2)用javac编译HelloWorld
3)用javah产生HelloWorld.h文件。它好比java中的一个接口。
/**//* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/**//* Header for class HelloWorld */
#ifndef _Included_HelloWorld
#define _Included_HelloWorld
#ifdef __cplusplus
extern "C" {
#endif
/**//*
* Class: HelloWorld
* Method: displayHelloWorld
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld
(JNIEnv *, jobject);
/**//*
* Class: HelloWorld
* Method: getUserName
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_HelloWorld_getUserName
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
4)编写一个*.c(HelloWorldImp.c)的文件。它好比一个接口的实现。
#include <jni.h>
#include "HelloWorld.h"
#include <stdio.h>
JNIEXPORT void JNICALL Java_HelloWorld_displayHelloWorld(JNIEnv *env, jobject obj)
{
printf("Hello world!\n");//这个例子中只输出一行Hello World!
// return;
}
JNIEXPORT jstring JNICALL Java_HelloWorld_getUserName (JNIEnv *env, jobject obj){
jstring jstr;
char str[]=" Rami very good! yes!\n";
jstr = (*env)->NewStringUTF(env,str);
//printf(str);
return jstr;
}
5) 编写一个applet程序来调用HelloWord。Applet1.java
import java.applet.Applet;
import java.awt.*;
public class Applet1 extends Applet {
/** *//**
* @param args the command line arguments
*/
public String UserName="applet call jni";
public void init(){
try{
System.out.println("test");
//new HelloWorld().displayHelloWorld();
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");
//new HelloWorld().displayHelloWorld();
UserName = new HelloWorld().getUserName();
System.out.println();
//new Hello().test();
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也有一个安全文件,也许是浏览调用的安全文件,就修改它。
/* AUTOMATICALLY GENERATED ON Thu Feb 24 09:23:05 CST 2005*/
/* DO NOT EDIT */
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所在的位置。
就这样应该大功告成了,
posted on 2005-09-21 15:13
Harryson 阅读(6191)
评论(13) 编辑 收藏 所属分类:
Jni--Communication