Jsp中调用DLL(使用JNI)

jsp调用jni方法

注:本文介绍Jsp中调用Dll动态链接库文件的方法,摘编自CSDN 炼狱的专栏
我的环境:
--------
1. java编译器:eclipse 3.0.2
2. web服务器: tomcat 4.1.24
3. C编译器: VC6.0
4. 操作系统: win2k
5. java源文件目录: d:/study/src
6. java编译后的class文件目录: d:/study/bin
7. tomcat目录: d:/tomcat41


1.编写java文件:
//Util.java
package com.jni;

public class Util
{
    public native static int add( int x, int y );
    
    static
    {
        System.loadLibrary( "Util" );
    }
}

编译这个文件,生成Util.class文件.

2.生成对应的头文件:
打开一个dos窗口,进入存放class文件的目录:
d:/>cd d:/study/bin
d:/study/bin/>javah com.jni.Util

这样,会在d:/study/bin目录下生成com_jni_Util.h文件.

3. 编写c文件:
在VC6.0中:File->New->Projects->Win32 Dynamic-link Library,Project name 为Util, 创建一个empty DLL project.
把2所生成的com_jni_Util.h文件和C:/j2sdk1.4.2/include目录下的jni.h, C:/j2sdk1.4.2/include/win32目录下的jni_md.h复制到Util工程目录下.
(C:/j2sdk1.4.2为我的jsdk的安装路径).

在Util工程中创建一个c源文件,名为Util.c.
//Util.c
#include "com_jni_Util.h"

JNIEXPORT jint JNICALL Java_com_jni_Util_add
(JNIEnv *env, jclass jc, jint x, jint y)
{
    return x+y;
}

4. 生成Util.dll:
编译3中的Util.c文件后,会生成Util.dll文件.

5. 生成jar包:
把Util.class文件打成jar包,名为myutil.jar.

6. 编写jsp文件:
//test.jsp
<%@ page import="com.jni.Util" %>





5+7=<%=Util.add( 5, 7 )%>





7. 把myutil.jar包复制到d:/tomcat41/common/lib目录下;
   把Util.dll文件复制到d:/tomcat41/bin目录下(也可以是环境变量path的所设定的任何一个目录下);
   把test.jsp复制到d:/tomcat41/webapp/test目录下.

8. 启动tomcat.

9. 在ie中输入: http://localhost:8080/test/test.jsp.
   如果你能看到: 5+7=12 就成功了.
================================================
以下为网友回复:
#  javalover 发表于2006-12-01 17:57:00  IP: 222.66.100.*
这篇文章太好,我现在有了一个已经生成好的dll文件。在本地调用都没有问题,但是我想在jsp页面中来获得dll文件执行的结果。我现在应该怎么做哪?
我按照你讲的来做,把.class打包,放在tomacat的lib下,dll放在bin下。但是出现了一下的问题
2006-12-1 17:53:26 org.apache.jasper.compiler.Compiler generateClass
严重: Error compiling file: /C:/Tomcat 5.0/work/Catalina/localhost/test//org/apache/jsp/sysinfo_jsp.java [javac] Compiling 1 source file

C:/Tomcat 5.0/work/Catalina/localhost/test/org/apache/jsp/sysinfo_jsp.java:57: cannot resolve symbol
symbol : class PerfInfoReader
location: class org.apache.jsp.sysinfo_jsp
PerfInfoReader test = new PerfInfoReader();
^
C:/Tomcat 5.0/work/Catalina/localhost/test/org/apache/jsp/sysinfo_jsp.java:57: cannot resolve symbol
symbol : class PerfInfoReader
location: class org.apache.jsp.sysinfo_jsp
PerfInfoReader test = new PerfInfoReader();
^
C:/Tomcat 5.0/work/Catalina/localhost/test/org/apache/jsp/sysinfo_jsp.java:58: cannot resolve symbol
symbol : variable PerfInfoReader
location: class org.apache.jsp.sysinfo_jsp
double o = test.getPerfInfo(PerfInfoReader.CPUUSAGE);
^
3 errors
能告诉我如何做吗?我现在有了dll文件,也可以在本地来调用。如何在jsp中调用哪。
 
#   javalover 发表于2006-12-02 21:22:00  IP: 222.66.100.*
我测试了一下:
在tomacat下可以正常的调用JNI,在JBOSS下就不可以了,
有没有人知道在JBOSS下如何解决这个问题啊,
21:20:33,531 ERROR [Engine] StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.UnsatisfiedLinkError: getPerfInfo
at com.star.sam.sysmonitor.sysperfmonitor.PerfInfoReader.getPerfInfo(Native Method)
at com.star.sam.sysmonitor.sysperfmonitor.SysPerformaceSampling.getCPU(SysPerformaceSampling.java:22) at com.star.sam.system.SystemUtil.getCurrentCPU(SystemUtil.java:61)
at org.apache.jsp.sammas.sysinfo_jsp._jspService(sysinfo_jsp.java:234)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:66)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:162)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:534)

你可能感兴趣的:(Jsp中调用DLL(使用JNI))