title: Using Freemarker with Restlet 2.0 in a Java EE server
body: <p>I'm a bit confused with what is written in the documentation(s) for Freemarker and Restlet's freemarker extension.</p>
  
  <p>Here's the situation: The restlet engine serves an HTML representation of a resource (e.g. www.mysite.com/{user}/updates). The resource returned for this URI is an HTML page containing all the updates, that is created with a freemarker template. This application is hosted on a  Glassfish v3 server</p>
  
  <p>Question(s):</p>
  
  <ul>
  <li><p>The freemarker configuration should only be loaded once as per the freemarker documentation:</p>
  
  <pre><code>    /* You should do this ONLY ONCE in the whole application life-cycle:Create and adjust the configuration */
      Configuration cfg = new Configuration();
      cfg.setDirectoryForTemplateLoading(
              new File("/where/you/store/templates"));
      cfg.setObjectWrapper(new DefaultObjectWrapper());
  </code></pre>
  
  <p>What is the best place to do this in a Java EE app? I am thinking of having it as context-param in web.xml and using a ServletContextListener - but I'm not sure how to go about doing that.</p></li>
  <li><p>As per freemarker's documentation we could also add a freemarkerservlet and map <em>.ftl url-patterns to it. But this is already mapped by a Restlet servlet (i.e., the url-pattern of "/</em>"). So having another one for *.ftl doesn't make sense (or does it?)</p></li>
  </ul>
  
  <p>So the question is basically about how best to integrate with the 'configuration' of Freemarker so that it happens only once and what is the 'entry-point' for that piece of code (who calls it). Has anyone successfully used Freemarker + restlet in a Java EE environment? Any ideas?</p>
  
  <p>Thanks!</p>