Class TemplateServlet

java.lang.Object
jakarta.servlet.GenericServlet
jakarta.servlet.http.HttpServlet
groovy.servlet.AbstractHttpServlet
groovy.servlet.TemplateServlet
All Implemented Interfaces:
ResourceConnector, jakarta.servlet.Servlet, jakarta.servlet.ServletConfig, Serializable

public 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
 

Servlet Init Parameters

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
 

Groovy Source Encoding Parameter

The following servlet init parameter name can be used to specify the encoding TemplateServlet will use to read the template groovy source files:

   groovy.source.encoding
 
See Also:
  • Constructor Details

    • TemplateServlet

      public TemplateServlet()
      Create new TemplateServlet.
  • Method Details

    • getTemplate

      protected Template getTemplate(File file) throws jakarta.servlet.ServletException
      Gets the template created by the underlying engine parsing the request.

      A template already compiled for this file is reused while it remains current: the file's length and last modified stamp are compared with those it was compiled from, and it is compiled again if either has moved. A template may also be dropped and compiled again at any point, so do not rely on being given the same instance twice, and expect the one you are given to be serving other requests at the same time.

      Parameters:
      file - The file containing the template source.
      Returns:
      The template that will produce the response text.
      Throws:
      jakarta.servlet.ServletException - If the request specified an invalid template source file
    • getTemplate

      protected Template getTemplate(URL url) throws jakarta.servlet.ServletException
      Gets the template created by the underlying engine parsing the request.

      A template already compiled for this URL is reused. There is nothing to check it against, unlike the file form above, so a change behind the URL is not noticed. A template may be dropped and compiled again at any point, so do not rely on being given the same instance twice, and expect the one you are given to be serving other requests at the same time.

      Parameters:
      url - The URL containing the template source..
      Returns:
      The template that will produce the response text.
      Throws:
      jakarta.servlet.ServletException - If the request specified an invalid template source URL
    • init

      public void init(jakarta.servlet.ServletConfig config) throws jakarta.servlet.ServletException
      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.
      Specified by:
      init in interface jakarta.servlet.Servlet
      Overrides:
      init in class AbstractHttpServlet
      Parameters:
      config - Passed by the servlet container.
      Throws:
      jakarta.servlet.ServletException - if this method encountered difficulties
      See Also:
    • initTemplateEngine

      protected TemplateEngine initTemplateEngine(jakarta.servlet.ServletConfig config)
      Creates the template engine.

      Called by init(ServletConfig) and returns just new groovy.text.SimpleTemplateEngine() if the init parameter template.engine is not set by the container configuration.

      Parameters:
      config - Current servlet configuration passed by the container.
      Returns:
      The underlying template engine or null on error.
    • service

      public void service(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws jakarta.servlet.ServletException, IOException
      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.

      Overrides:
      service in class jakarta.servlet.http.HttpServlet
      Parameters:
      request - The http request.
      response - The http response.
      Throws:
      IOException - if an input or output error occurs while the servlet is handling the HTTP request
      jakarta.servlet.ServletException - if the HTTP request cannot be handled