title: Reuse Nashorn ScriptEngine in Servlet
body: <p>I want to execute a JavaScript within a servlet. Is it possible to reuse the same Scripting Engine across all servlet invocations? Servlet instances are shared by multiple threads. Does this require to create a new Scripting Engine per request? That would be a unacceptable performance penalty. As an example, is the following code save?</p>
  
  <pre><code>public class MyServlet extends HttpServlet {
  
  private ScriptEngineManager factory;
  private ScriptEngine engine;
  
  @Override
  public void init() throws ServletException {
      factory = new ScriptEngineManager();
      engine = factory.getEngineByName("nashorn");
  }
  
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
      try (PrintWriter writer = res.getWriter()) {
          ScriptContext newContext = new SimpleScriptContext();
          newContext.setBindings(engine.createBindings(), ScriptContext.ENGINE_SCOPE);
          Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
          engineScope.put("writer", writer);
          Object value = engine.eval("writer.print('Hello, World!');", engineScope);
          writer.close();
      } catch (IOException | ScriptException ex) {
          Logger.getLogger(AsyncServlet.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>If this is not safe, what would be the best way to avoid creating an engine per request? Using a pool of engines? </p>
  
  <p><strong>Edit:</strong>
  Is it possible to reuse one and the same engine and one and the same JavaScriptObject, which results as the evaluation of a JS-function, for all servlet requests, if the function does not change any shared object but uses only the arguments given with the call? Look at the following adaptaion of the above example:</p>
  
  <pre><code>public class MyServlet extends HttpServlet {
  
  private ScriptEngineManager factory;
  private ScriptEngine engine;
  private ScriptObjectMirror script;
  
  @Override
  public void init() throws ServletException {
      try {
          factory = new ScriptEngineManager();
          engine = factory.getEngineByName("nashorn");
          script = (ScriptObjectMirror)engine.eval("function(writer) {writer.print('Hello, World!');}");
      } catch (ScriptException ex) {
          Logger.getLogger(MyServlet.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
      try (PrintWriter writer = res.getWriter()) {
          script.call(null, writer);
          writer.close();
      } catch (IOException ex) {
          Logger.getLogger(MyServlet.class.getName()).log(Level.SEVERE, null, ex);
      }
  }
  </code></pre>
  
  <p>Is this safe?</p>