Class TemplateServlet
- java.lang.Object
- 
- javax.servlet.GenericServlet
- 
- javax.servlet.http.HttpServlet
- 
- groovy.servlet.AbstractHttpServlet
- 
- groovy.servlet.TemplateServlet
 
 
 
 
- 
- All Implemented Interfaces:
- ResourceConnector,- java.io.Serializable,- Servlet,- ServletConfig
 
 public class TemplateServlet extends AbstractHttpServlet A generic servlet for serving (mostly HTML) templates.It delegates work to a groovy.text.TemplateEngineimplementation processing HTTP requests.Usagehelloworld.htmlis a headless HTML-like template<html> <body> <% 3.times { %> Hello World! <% } %> <br> </body> </html>Minimal web.xmlexample 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 configurationBy default, the TemplateServer uses the SimpleTemplateEnginewhich interprets JSP-like templates. The init parametertemplate.enginedefines 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 ParametersLogging and extra-output optionsThis implementation provides a verbosity flag switching log statements. The servlet init parameter name is: generated.by = true(default) | false Groovy Source Encoding ParameterThe 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 
- 
- 
Field Summary- 
Fields inherited from class groovy.servlet.AbstractHttpServletCONTENT_TYPE_TEXT_HTML, encoding, INC_PATH_INFO, INC_REQUEST_URI, INC_SERVLET_PATH, INIT_PARAM_RESOURCE_NAME_REGEX, INIT_PARAM_RESOURCE_NAME_REGEX_FLAGS, namePrefix, reflection, resourceNamePattern, resourceNameReplaceAll, resourceNameReplacement, servletContext, verbose
 
- 
 - 
Constructor SummaryConstructors Constructor Description TemplateServlet()Create new TemplateServlet.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Modifier and Type Method Description protected TemplategetTemplate(java.io.File file)Gets the template created by the underlying engine parsing the request.protected TemplategetTemplate(java.net.URL url)Gets the template created by the underlying engine parsing the request.voidinit(ServletConfig config)Initializes the servlet from hints the container passes.protected TemplateEngineinitTemplateEngine(ServletConfig config)Creates the template engine.voidservice(HttpServletRequest request, HttpServletResponse response)Services the request with a response.- 
Methods inherited from class groovy.servlet.AbstractHttpServletapplyResourceNameMatcher, generateNamePrefixOnce, getResourceConnection, getScriptUri, getScriptUriAsFile, removeNamePrefix, setVariables
 - 
Methods inherited from class javax.servlet.http.HttpServletdoDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
 - 
Methods inherited from class javax.servlet.GenericServletdestroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
 
- 
 
- 
- 
- 
Method Detail- 
getTemplateprotected Template getTemplate(java.io.File file) throws 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 file containing the template source.
- Returns:
- The template that will produce the response text.
- Throws:
- ServletException- If the request specified an invalid template source file
 
 - 
getTemplateprotected Template getTemplate(java.net.URL url) throws 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 URL. If there is no cache entry, a new one is created by the underlying template engine. This new instance is put to the cache for consecutive calls. - Parameters:
- url- The URL containing the template source..
- Returns:
- The template that will produce the response text.
- Throws:
- ServletException- If the request specified an invalid template source URL
 
 - 
initpublic void init(ServletConfig config) throws 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:
- initin interface- Servlet
- Overrides:
- initin class- AbstractHttpServlet
- Parameters:
- config- Passed by the servlet container.
- Throws:
- ServletException- if this method encountered difficulties
- See Also:
- initTemplateEngine(ServletConfig)
 
 - 
initTemplateEngineprotected TemplateEngine initTemplateEngine(ServletConfig config) Creates the template engine.Called by init(ServletConfig)and returns justnew groovy.text.SimpleTemplateEngine()if the init parametertemplate.engineis not set by the container configuration.- Parameters:
- config- Current servlet configuration passed by the container.
- Returns:
- The underlying template engine or nullon error.
 
 - 
servicepublic void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.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:
- servicein class- HttpServlet
- Parameters:
- request- The http request.
- response- The http response.
- Throws:
- java.io.IOException- if an input or output error occurs while the servlet is handling the HTTP request
- ServletException- if the HTTP request cannot be handled
 
 
- 
 
-