admin 管理员组

文章数量: 1086019


2024年2月29日发(作者:oracle数据库触发器的作用)

Java调用js方法

java调用js方法分三种:(从jdk1.6开始java支持了调用脚本)

一.调用java代码内部编写的js方法

简单示例:

import ble;

import Engine;

import EngineManager;

public class js {

public static void main(String[] args) throws Exception {

// 获得一个JavaScript脚本引擎

ScriptEngineManager mgr = new ScriptEngineManager();

ScriptEngine engine = ineByName("JavaScript");

//执行脚本

("function test(p){return 'this is test js in

java,'+p}");

//转化成父类(Invocable),因为ScriptEngine中没有调用js的方法

参数");

}

二.调用外部js文件中的方法

又细分为:调用本地js文件

外网js文件(也就是网页中嵌入的js脚本文件)

简单示例:

1.调用本地js文件

实现原理:通过File文件得到js的文件流,然后放入()方法中,其他的都

和第一种原理一致;

import ;

import putStream;

import treamReader;

import ;

import ble;

import Engine;

import EngineManager;

public class js2 {

Invocable inv = (Invocable) engine;

//调用脚本得到返回值

String value = (String)Function("test","我是一个 n(value);

}

public static void main(String[] args) throws Exception {

}

}

ScriptEngineManager mgr = new ScriptEngineManager();

ScriptEngine engine = ineByName("JavaScript");

//加载一个文件

File f=new File("./WebRoot/js/");

Reader r=new InputStreamReader(new FileInputStream(f));

//把Reader放入eval中(读者可以去API看一下,重载了很多的eval()方法)

(r);

Invocable inv = (Invocable) engine;

String value = (String)Function("test","我是一个参数n(value);

");

注:外部的js(./WebRoot/js/)文件内容为:

function test(p){

}

简单示例:

2.调用外网js文件

实现原理:通过解析HttpUrlConnection类得到JSP页面内容,然后通过正则表达式匹配js

脚本路径,然后通过HttpUrlConnection访问js文件,得到Reader,其他原理同上

import tream;

import treamReader;

import ;

import LConnection;

import ;

import r;

import n;

import ble;

import Engine;

import EngineManager;

public class js3 {

public static void main(String[] args) throws Exception {

//构造一个脚本器

return '调用了外部js方法 '+p;

ScriptEngineManager mgr = new ScriptEngineManager();

ScriptEngine engine = ineByName("JavaScript");

String url="localhost/Test";

String html=l(url);

//使用正则匹配js文件路径

String js=getTextZZ(html,

"[s]*");

js=url+tZZ(js, "Q/E[.&S]+");

//得到流

Reader r=getReader(js, "utf-8");

//把Reader放入eval中(读者可以去API看一下,重载了很多的eval()方法)

(r);

Invocable inv = (Invocable) engine;

String value = (String)Function("test","我是一个参数n(value);

");

}

//得到Reader(使用HttpURLConnection得到外部js文件的Reader流)

public static Reader getReader(String url,String code) {

URL URL=null;

HttpURLConnection conn=null;

InputStream in=null;

Reader br=null;

try {

URL=new URL(url);

conn=(HttpURLConnection)nnection();

//设置get请求

uestMethod("GET");

//得到流

in=utStream();

br=new InputStreamReader(in,code);

}catch(Exception ex){

tackTrace();

}

return br;

}

//根据内容得到匹配

public static String getTextZZ(String Text,String zz){

Pattern p = e(zz);

Matcher m = r(Text);

String group="";

while(()){

group=();

}

return group;

}

//得到HTML

public static String getHtml(String url) {

URL URL=null;

}

}

HttpURLConnection conn=null;

InputStream in=null;

BufferedReader br=null;

String html=null;

try {

URL=new URL(url);

conn=(HttpURLConnection)nnection();

uestMethod("GET");

String code=getCode(tentType());

in=utStream();

br=new BufferedReader(new InputStreamReader(in,code));

//得到HTML文档

String data="";

while((data=ne())!=null){

html+=data+"n";

}

}catch(Exception ex){

tackTrace();

}finally{

try{

();

();

nect();

}catch(Exception ex){

tackTrace();

}

}

return html;

注:外部的js(./WebRoot/js/)文件内容为:

function test(p){

return '调用了外部js方法 '+p;

}

注:我们所需的JSP文件(里面嵌入了js脚本)

<%@ page language="java" import=".*" pageEncoding="UTF-8"%>

JS测试

三.调用jsp页面中的js方法

<%@ page language="java" import=".*" pageEncoding="UTF-8"%>

JS测试

注:我的思路:通过正则匹配出JSP页面中的js脚本,然后将匹配的内容传到

()方法中,就ok了。这种方法比较笨,可能还有更好的实

现效果,有想到更好的方法的读者可以分享一下。

(第三种方式的实现代码就不写了,只要会了第二种,第三种就轻而易举了)

有做这方面的可以加我QQ:275070023(请注java调用js方法)共同进步嘛


本文标签: 文件 调用 方法 脚本 得到