首先创建Manager
Java代码:
- ScriptEngineManager sem = new ScriptEngineManager();
然后通过Manager根据ScriptEngine信息创建相应的ScriptEngine
有三种方式可以创建ScriptEngine
Java代码:
- getEngineByName(String shortName)
- getEngineByExtension(String extension)
- getEngineByMimeType(String mimeType)
shortName就是我们所说的javascript,JavaScript之类的
extension就是我们所说的js
mimeType就是我们所说的text/javascript
可以通过如下方式获取相应的shortName,extension和mimeType
Java代码:
- ScriptEngine engine = sem.getEngineByName("JavaScript");
- //get types
- List<String> types=engine.getFactory().getMimeTypes();
- for(String type:types){
- System.out.println(String.format("type:%s", type));
- }
- //get names
- List<String> names=engine.getFactory().getNames();
- for(String name:names){
- System.out.println(String.format("name:%s", name));
- }
- //get extensions
- List<String> extensions=engine.getFactory().getExtensions();
- for(String extension:extensions){
- System.out.println(String.format("extension:%s", extension));
- }
输出如下:
Java代码:
- type:application/javascript
- type:application/ecmascript
- type:text/javascript
- type:text/ecmascript
- name:js
- name:rhino
- name:JavaScript
- name:javascript
- name:ECMAScript
- name:ecmascript
- extension:js