Scripting Scripting within Java

황제낙엽 2012.01.03 14:38 조회 수 : 69

sitelink1 https://wikis.oracle.com/display/code/Scripting+within+Java 
sitelink2  
sitelink3 http://1 
sitelink4 http://ko 
sitelink5  
sitelink6 http://sitelink1 

The following code sample is from How Java Plays With Scripting Languages

 

The Java Scripting Framework provides a set of scripting APIs, which allow Java Scripting Engines to be used in Java applications. The API is intended for use by application programmers who wish to execute programs written in scripting languages, in their Java applications. Let's go through a code sample to see how this can be achieved.

 

The simplest way to use the scripting API is as follows:

 

1. Create a ScriptEngineManager object.
2. Get a ScriptEngine object from the manager.
3. Evaluate the script using the ScriptEngine's eval methods.

import javax.script.*;
public class EvalScript {
    public static void main(String[] args) throws Exception {
        // create a script engine manager
        ScriptEngineManager factory = new ScriptEngineManager();
        // create a JavaScript engine
        ScriptEngine engine = factory.getEngineByName("JavaScript");

        // create a Java object
        String name = "Tom";

        // create the binding
        engine.put("greetingname", name);

        // evaluate JavaScript code from String
        engine.eval("println('Hello, ' + greetingname)");
        engine.eval("println('The name length is ' +  greetingname.length)");
    }
}

 

The output is:

Hello, Tom
The name length is 3

 

Let's go through the code sample:

 

1. The Script Engine Discovery Mechanism identifies the Script Engine by the script name "JavaScript".
2. Create the bindings with key/value pairs, the key is "greetingname" JavaScript variable name, the value is the Java object proxy, which wraps the Java String "name".
3. The "eval()" method of Script Engine parses the JavaScript code and executes it by internally using a front-end compiler and back-end executor.
4. During the execution, the method and attribute of the Java object can be accessed from the script.

번호 제목 글쓴이 날짜 조회 수
231 싱글톤 방식의 테스트용 Temporary Data Access Object 황제낙엽 2017.01.12 1603
230 SimpleDateFormat Symbol file 황제낙엽 2016.12.20 74
229 JSON-lib Java Library file 황제낙엽 2013.04.09 91
228 JSP 파일에서 getOutputStream() has already been called for this response 에러 황제낙엽 2013.04.24 11479
227 servlet 에서의 json 한글처리 황제낙엽 2013.04.23 1519
226 -file.encoding의 역할 (다국어, 한국어) 황제낙엽 2013.04.10 235
225 [The type HttpUtils is deprecated] javax.servlet.http.HttpUtils 황제낙엽 2013.03.20 276
224 com.oreilly.servlet.multipart 를 이용한 파일 업로드 file 황제낙엽 2013.03.19 104
223 String to InputSource 황제낙엽 2012.12.03 77
222 Class.getResource() vs. ClassLoader.getResource()/getResources() 황제낙엽 2012.06.24 57
221 Jar파일에 포함된 리소스 접근하는 방법(How to read a resource from a JAR file ) file 황제낙엽 2012.06.24 164
220 Java에서 URL 다루기 file 황제낙엽 2012.06.24 88
219 HttpServletResponse.setContentType(java.lang.String type) 과 MIME 타입 황제낙엽 2012.04.20 172
218 code, codebase 속성과 applet object 동적 생성 file 황제낙엽 2012.04.17 85
217 한글 파일명 깨짐으로 살펴본 다국어 처리 문제 (UTF-8) 황제낙엽 2012.03.22 10121
216 BufferedReader.readLine() hangs 황제낙엽 2012.02.23 520
215 OS 쉘 명령어(shell script) 실행하기 [ProcessBuilder, Runtime.getRuntime().exec()] 황제낙엽 2012.02.22 664
214 PreProcess 실행 (전처리기 만들기) file 황제낙엽 2012.01.04 169
213 javax.script와 타입변환 황제낙엽 2012.01.03 62
» Scripting within Java 황제낙엽 2012.01.03 69