title: How to run Groovy Script in Java
body: <p>I have a Java code and I want to run a groovy script in Java the problem is that the method I created returns allways null</p>
  
  <p>This is my script inside an html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;script type="server/groovy"&gt;
          import com.daimler.Car
          def id = request.getParameter("id")
          car = Car.lookup(id)
      &lt;/script&gt;
      &lt;head&gt;
          &lt;title&gt;${car.brand}&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;h1 title="${car.brand}"&gt;${car.brand}&lt;/h1&gt;
          &lt;h2 data-if="car.ecoFriendly" title="${car.fuelType}"&gt;Fuel Type:
              ${car.fuelType}&lt;/h2&gt;
          &lt;div data-loop-model="car.models"&gt;Model: ${model}&lt;/div&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is my method to run the Script</p>
  
  <pre><code>   //the request parameter comes from the doGet method FYI
      public Object runScript(HttpServletRequest request) {
  
              GroovyShell groovyShell = new GroovyShell();
  
              try {
                  Script script = groovyShell.parse(new File("templates/index.tpl"));
                  Map bindings = script.getBinding().getVariables();
                  //setting atributes in script here
                  bindings.put("id",request);
  
                  //run the script
                  script.run();
  
                  //getting object values after the script
                  Object carObject = bindings.get("car");
                  return carObject;
  
              } catch (CompilationFailedException | IOException ex) {
                  java.util.logging.Logger.getLogger(CarServlet.class.getName()).log(Level.SEVERE, null, ex);
                  //improve 
                  return null;
              }
  </code></pre>