public class TemplateServlet extends AbstractHttpServlet
It delegates work to a groovy.text.TemplateEngine
implementation
processing HTTP requests.
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>
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
This implementation provides a verbosity flag switching log statements. The servlet init parameter name is:
generate.by = true(default) | false
setVariables(ServletBinding)
,
Serialized FormCONTENT_TYPE_TEXT_HTML, encoding, INC_PATH_INFO, INC_REQUEST_URI, INC_SERVLET_PATH, reflection, resourceNameMatcher, resourceNameReplaceAll, resourceNameReplacement, servletContext, verbose
Constructor and Description |
---|
TemplateServlet()
Create new TemplateSerlvet.
|
Modifier and Type | Method and Description |
---|---|
protected Template |
getTemplate(java.io.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.
|
getResourceConnection, getScriptUri, getScriptUriAsFile
doDelete, doGet, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service
protected Template getTemplate(java.io.File file) throws javax.servlet.ServletException
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.
file
- The HttpServletRequest.javax.servlet.ServletException
- If the request specified an invalid template source filepublic void init(javax.servlet.ServletConfig config) throws javax.servlet.ServletException
Delegates to sub-init methods and parses the following parameters:
init
in interface javax.servlet.Servlet
init
in class AbstractHttpServlet
config
- Passed by the servlet container.javax.servlet.ServletException
- if this method encountered difficultiesinitTemplateEngine(ServletConfig)
protected TemplateEngine initTemplateEngine(javax.servlet.ServletConfig config)
init(ServletConfig)
and returns just
new groovy.text.SimpleTemplateEngine()
if the init parameter
template.engine
is not set by the container configuration.config
- Current serlvet configuration passed by the container.null
on error.public void service(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
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.
service
in class javax.servlet.http.HttpServlet
request
- The http request.response
- The http response.java.io.IOException
- if an input or output error occurs while the servlet is
handling the HTTP requestjavax.servlet.ServletException
- if the HTTP request cannot be handledprotected void setVariables(ServletBinding 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 implicite hard-coded keywords:
Example binding all servlet context variables:
class Mytlet extends TemplateServlet {
protected void setVariables(ServletBinding binding) {
// Bind a simple variable
binding.setVariable("answer", new Long(42));
// Bind all servlet context attributes...
ServletContext context = (ServletContext) binding.getVariable("context");
Enumeration enumeration = context.getAttributeNames();
while (enumeration.hasMoreElements()) {
String name = (String) enumeration.nextElement();
binding.setVariable(name, context.getAttribute(name));
}
}
}
binding
- to be modified