groovy.servlet
Class TemplateServlet

java.lang.Object
  extended byjavax.servlet.GenericServlet
      extended byjavax.servlet.http.HttpServlet
          extended bygroovy.servlet.AbstractHttpServlet
              extended bygroovy.servlet.TemplateServlet
All Implemented Interfaces:
ResourceConnector, Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class TemplateServlet
extends AbstractHttpServlet

A generic servlet for serving (mostly HTML) templates. It wraps a groovy.text.TemplateEngine to process HTTP requests. By default, it uses the groovy.text.SimpleTemplateEngine which interprets JSP-like (or Canvas-like) templates. The init parameter templateEngine defines the fully qualified class name of the template to use.

Headless helloworld.html example


  <html>
    <body>
      <% 3.times { %>
        Hello World!
      <% } %>
      <br>
      session.id = ${session.id}
    </body>
  </html> 
 

Version:
2.0
Author:
Christian Stein, Guillaume Laforge
See Also:
setVariables(ServletBinding), Serialized Form

Field Summary
 
Fields inherited from class groovy.servlet.AbstractHttpServlet
CONTENT_TYPE_TEXT_HTML, INC_PATH_INFO, INC_SERVLET_PATH, servletContext
 
Constructor Summary
TemplateServlet()
          Create new TemplateSerlvet.
 
Method Summary
protected  Template getTemplate(File file)
          Gets the template created by the underlying engine parsing the request.
 void init(javax.servlet.ServletConfig config)
          Initializes the servlet from hints the container passes.
protected  TemplateEngine initTemplateEngine(javax.servlet.ServletConfig config)
          Creates the template engine.
 void service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Services the request with a response.
protected  void setVariables(ServletBinding binding)
          Override this method to set your variables to the Groovy binding.
 
Methods inherited from class groovy.servlet.AbstractHttpServlet
getResourceConnection, getScriptUri, getScriptUriAsFile
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
 
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TemplateServlet

public TemplateServlet()
Create new TemplateSerlvet.

Method Detail

getTemplate

protected Template getTemplate(File file)
                        throws javax.servlet.ServletException
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.

Parameters:
file - The HttpServletRequest.
Returns:
The template that will produce the response text.
Throws:
IOException - If the request specified an invalid template source file
javax.servlet.ServletException

init

public void init(javax.servlet.ServletConfig config)
          throws javax.servlet.ServletException
Initializes the servlet from hints the container passes.

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

Specified by:
init in interface javax.servlet.Servlet
Overrides:
init in class AbstractHttpServlet
Parameters:
config - Passed by the servlet container.
Throws:
javax.servlet.ServletException - if this method encountered difficulties
See Also:
initTemplateEngine(ServletConfig)

initTemplateEngine

protected TemplateEngine initTemplateEngine(javax.servlet.ServletConfig config)
Creates the template engine. Called by init(ServletConfig) and returns just new groovy.text.SimpleTemplateEngine() if the init parameter templateEngine is not set by the container configuration.

Parameters:
config - Current serlvet configuration passed by the container.
Returns:
The underlying template engine or null on error.
See Also:
initTemplateEngine(javax.servlet.ServletConfig)

service

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

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
javax.servlet.ServletException - if the HTTP request cannot be handled

setVariables

protected void setVariables(ServletBinding binding)
Override this method to set your variables to the Groovy binding.

All variables bound the binding are passed to the template source text, e.g. the HTML file, when the template is merged.

The binding provided by TemplateServlet does already include some default variables. As of this writing, they are (copied from ServletBinding):

And via explicit hard-coded keywords:

Example binding all servlet context variables:


 class Mytlet extends TemplateServlet {
 
   private ServletContext context;
   
   public void init(ServletConfig config) {
     this.context = config.getServletContext();
   }
 
   protected void setVariables(ServletBinding binding) {
     Enumeration enumeration = context.getAttributeNames();
     while (enumeration.hasMoreElements()) {
       String name = (String) enumeration.nextElement();
       binding.setVariable(name, context.getAttribute(name));
     }
   }
 
 }
 

Parameters:
binding - to get modified
See Also:
TemplateServlet


Copyright © 2003-2005 The Codehaus. All Rights Reserved.