Groovy 1.7.0

## groovy.servlet
Class TemplateServlet

    java.lang.Object
      javax.servlet.GenericServlet
          javax.servlet.http.HttpServlet
              groovy.servlet.AbstractHttpServlet
                  groovy.servlet.TemplateServlet

    class TemplateServlet
    extends AbstractHttpServlet

A generic servlet for serving (mostly HTML) templates.

It delegates work to a `groovy.text.TemplateEngine` implementation processing HTTP requests.

#### Usage

`helloworld.html` is a headless HTML-like template

      <html>
        <body>
          <% 3.times { %>
            Hello World!
          <% } %>
          <br>
        </body>
      </html>

Minimal `web.xml` example serving HTML-like templates

     <web-app>
       <servlet>
         <servlet-name>template</servlet-name>
         <servlet-class>groovy.servlet.TemplateServlet</servlet-class>
       </servlet>
       <servlet-mapping>
         <servlet-name>template</servlet-name>
         <url-pattern>*.html</url-pattern>
       </servlet-mapping>
     </web-app>

#### Template engine configuration

By default, the TemplateServer uses the SimpleTemplateEngine which interprets JSP-like templates. The init parameter `template.engine` defines the fully qualified class name of the template to use:

       template.engine = [empty] - equals groovy.text.SimpleTemplateEngine
       template.engine = groovy.text.SimpleTemplateEngine
       template.engine = groovy.text.GStringTemplateEngine
       template.engine = groovy.text.XmlTemplateEngine

#### Logging and extra-output options

This implementation provides a verbosity flag switching log statements. The servlet init parameter name is:

       generated.by = true(default) | false

see:
  TemplateServlet#setVariables(ServletBinding)

author:
  Christian Stein

author:
  Guillaume Laforge

version:
  2.0

Nested Class Summary
`class` | `TemplateServlet.TemplateCacheEntry` Simple cache entry that validates against last modified and length attributes of the specified file.

Field Summary

Fields inherited from class AbstractHttpServlet
encoding, reflection, resourceNameMatcher, resourceNameReplaceAll, resourceNameReplacement, servletContext, verbose

Constructor Summary
`TemplateServlet()`

Method Summary
`def` | `TemplateServlet()` Create new TemplateSerlvet.
`protected Template` | `getTemplate(File file)` Gets the template created by the underlying engine parsing the request.
`void` | `init(ServletConfig config)` Initializes the servlet from hints the container passes.
`protected TemplateEngine` | `initTemplateEngine(ServletConfig config)` Creates the template engine.
`void` | `service(HttpServletRequest request, HttpServletResponse response)` Services the request with a response.

Methods inherited from class AbstractHttpServlet
getResourceConnection, getScriptUri, getScriptUriAsFile, init, setVariables

Methods inherited from class HttpServlet
service, log, log, init, init, destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll

Methods inherited from class GenericServlet
log, log, init, init, destroy, service, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll

Methods inherited from class Object
wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll

Constructor Detail

### TemplateServlet

    TemplateServlet()

Method Detail

### TemplateServlet

    public def TemplateServlet()

  Create new TemplateSerlvet.

### getTemplate

    protected Template getTemplate(File file)

  Gets the template created by the underlying engine parsing the request.

  This method looks up a simple (weak) hash map for an existing template object that matches the source file. If the source file didn't change in length and its last modified stamp hasn't changed compared to a precompiled template object, this template is used. Otherwise, there is no or an invalid template object cache entry, a new one is created by the underlying template engine. This new instance is put to the cache for consecutive calls.

  return:
    The template that will produce the response text.
  param:
    file The HttpServletRequest.
  throws:
    ServletException If the request specified an invalid template source file

### init

    public void init(ServletConfig config)

  Initializes the servlet from hints the container passes.

  Delegates to sub-init methods and parses the following parameters:

  - `"generatedBy"` : boolean, appends "Generated by ..." to the HTML response text generated by this servlet.
  param:
    config Passed by the servlet container.
  throws:
    ServletException if this method encountered difficulties
  see:
    TemplateServlet#initTemplateEngine(ServletConfig)

### initTemplateEngine

    protected TemplateEngine initTemplateEngine(ServletConfig config)

  Creates the template engine. Called by TemplateServlet#init(ServletConfig) and returns just `new groovy.text.SimpleTemplateEngine()` if the init parameter `template.engine` is not set by the container configuration.
  param:
    config Current serlvet configuration passed by the container.
  return:
    The underlying template engine or `null` on error.

### service

    public void service(HttpServletRequest request, HttpServletResponse response)

  Services the request with a response.

  First the request is parsed for the source file uri. If the specified file could not be found or can not be read an error message is sent as response.

  param:
    request The http request.
  param:
    response The http response.
  throws:
    IOException if an input or output error occurs while the servlet is handling the HTTP request
  throws:
    ServletException if the HTTP request cannot be handled

Copyright © 2003-2009 The Codehaus. All rights reserved.