Scripting Scripting within Java

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

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.

번호 제목 글쓴이 날짜 조회 수
243 [HttpURLConnection] POST로 파라미터 넘기기 황제낙엽 2017.08.01 917
242 HttpURLConnection POST 방식 사용하기 황제낙엽 2017.08.01 867
241 Runtime 클래스를 이용한 윈도우 프로그램 실행 예제 황제낙엽 2017.08.01 451
240 JSON Util (JSON 을 다루기 위해 직접 작성한 유틸 클래스) file 황제낙엽 2017.07.10 834
239 자바 리플렉션(Java Reflection) 간단한 설명 및 사용방법 정리 file 황제낙엽 2017.07.10 521
238 Generate random numbers (Random.java) 황제낙엽 2017.07.02 910
237 쓰레드(Thread)를 중간에 종료시키는 방법 황제낙엽 2017.03.15 5540
236 JSON 라이브러리(API) 종류 황제낙엽 2017.01.18 730
235 [JSON기초04] 자바 JSON 데이터에서 KEY 값 알아오기 (TIP) 황제낙엽 2017.01.18 7292
234 [JSON기초03] 자바 JSON Google Simple JSON을 이용한 간단한 JSON DATA 파싱 황제낙엽 2017.01.18 929
233 [JSON기초02] 자바 JSON Google Simple JSON을 이용한 간단한 JSON DATA 생성 황제낙엽 2017.01.18 488
232 [JSON기초01] JSON이란? XML이란? JSON 개념, XML 개념 설명 황제낙엽 2017.01.18 706
231 싱글톤 방식의 테스트용 Temporary Data Access Object 황제낙엽 2017.01.12 1958
230 SimpleDateFormat Symbol file 황제낙엽 2016.12.20 507
229 JSON-lib Java Library file 황제낙엽 2013.04.09 473
228 JSP 파일에서 getOutputStream() has already been called for this response 에러 황제낙엽 2013.04.24 11941
227 servlet 에서의 json 한글처리 황제낙엽 2013.04.23 1882
226 -file.encoding의 역할 (다국어, 한국어) 황제낙엽 2013.04.10 566
225 [The type HttpUtils is deprecated] javax.servlet.http.HttpUtils 황제낙엽 2013.03.20 607
224 com.oreilly.servlet.multipart 를 이용한 파일 업로드 file 황제낙엽 2013.03.19 485