2012-01-26 1 views

답변

0

아직 자바 스크립트 인터프리터 기능이없는 자바로 작성된 웹 브라우저를 작성하거나 수정하는 것처럼 들립니다. 그렇다면 Rhino를 사용하여 추가하는 것이 가장 쉽습니다.

Mozilla Rhino examples page에서 사용하는 방법에 대한 예제가 많이 있습니다. this example을 보여줍니다으로 Rhino에서, 호출 꽤 쉽게 : 그래서

/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 
* 
* ***** BEGIN LICENSE BLOCK ***** 
* Version: MPL 1.1/GPL 2.0 
* 
* The contents of this file are subject to the Mozilla Public License Version 
* 1.1 (the "License"); you may not use this file except in compliance with 
* the License. You may obtain a copy of the License at 
* http://www.mozilla.org/MPL/ 
* 
* Software distributed under the License is distributed on an "AS IS" basis, 
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
* for the specific language governing rights and limitations under the 
* License. 
* 
* The Original Code is Rhino code, released 
* May 6, 1998. 
* 
* The Initial Developer of the Original Code is 
* Netscape Communications Corporation. 
* Portions created by the Initial Developer are Copyright (C) 1999 
* the Initial Developer. All Rights Reserved. 
* 
* Contributor(s): 
* 
* Alternatively, the contents of this file may be used under the terms of 
* the GNU General Public License Version 2 or later (the "GPL"), in which 
* case the provisions of the GPL are applicable instead of those above. If 
* you wish to allow use of your version of this file only under the terms of 
* the GPL and not to allow others to use your version of this file under the 
* MPL, indicate your decision by deleting the provisions above and replacing 
* them with the notice and other provisions required by the GPL. If you do 
* not delete the provisions above, a recipient may use your version of this 
* file under either the MPL or the GPL. 
* 
* ***** END LICENSE BLOCK ***** */ 

import org.mozilla.javascript.*; 

/** 
* RunScript: simplest example of controlling execution of Rhino. 
* 
* Collects its arguments from the command line, executes the 
* script, and prints the result. 
* 
* @author Norris Boyd 
*/ 
public class RunScript { 
    public static void main(String args[]) 
    { 
     // Creates and enters a Context. The Context stores information 
     // about the execution environment of a script. 
     Context cx = Context.enter(); 
     try { 
      // Initialize the standard objects (Object, Function, etc.) 
      // This must be done before scripts can be executed. Returns 
      // a scope object that we use in later calls. 
      Scriptable scope = cx.initStandardObjects(); 

      // Collect the arguments into a single string. 
      String s = ""; 
      for (int i=0; i < args.length; i++) { 
       s += args[i]; 
      } 

      // Now evaluate the string we've colected. 
      Object result = cx.evaluateString(scope, s, "<cmd>", 1, null); 

      // Convert the result to a string and print it. 
      System.err.println(Context.toString(result)); 

     } finally { 
      // Exit from the context. 
      Context.exit(); 
     } 
    } 
} 

웹 페이지에 포함 된 자바 스크립트를 실행하도록 설정할 때, 당신은 scope 객체에 다양한 환경 객체 (document 등)를 추가 한 것 그런 다음 evaluateString을 사용하여 평가하십시오.

이 작업은 매우 중요하지 않은 작업입니다.이 작업은 이 될 것입니다. 자바를 처음 접한다면 조금 더 작은 것으로 시작하는 것이 좋습니다.

+0

thanQ에

public static void main(String[] args) { ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine engine = mgr.getEngineByName("JavaScript"); try { engine.eval("print('Hello, world!')"); } catch (ScriptException ex) { ex.printStackTrace(); } } 

최저 문서는 매우 간단하다? –

+0

@HemenAshodia : 저는 JWebPane을 사용한 적이 없습니다. 빠른 검색을 통해 많은 페이지를 볼 수 있지만, [this one] (http://blogs.oracle.com/thejavatutorials/entry/html_component)은 나에게 자세히 설명하는 것처럼 보입니다. –

+0

귀하의 응답에 대해 ThanQ –

1

사용하면 jwebpane입니다 중량 설명해주십시오 수 모질라의 웹 사이트 답 n에 대한 http://www.mozilla.org/rhino/doc.html

+0

좀 더 자세하게 설명해 주시겠습니까 –

+0

Rhino를 서버 측의 JavaScript Evaluation 용으로 만 사용했습니다. 위의 예제와 매우 유사한 파일 시스템에서 스크립트를로드하고 eval을 호출합니다. 불행히도 나는 결코 자신의 브라우저를 구현하지 못했습니다. –