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.
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:
generated.by = true(default) | false
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
Fields inherited from class | Fields |
---|---|
class AbstractHttpServlet |
CONTENT_TYPE_TEXT_HTML, INC_PATH_INFO, INC_REQUEST_URI, INC_SERVLET_PATH, INIT_PARAM_RESOURCE_NAME_REGEX, INIT_PARAM_RESOURCE_NAME_REGEX_FLAGS |
Constructor and description |
---|
TemplateServlet() Create new TemplateServlet. |
Type Params | Return Type | Name and description |
---|---|---|
|
public void |
init(ServletConfig config) Initializes the servlet from hints the container passes. |
|
public void |
service(HttpServletRequest request, HttpServletResponse response) Services the request with a response. |
Methods inherited from class | Name |
---|---|
class AbstractHttpServlet |
getResourceConnection, init |
Initializes the servlet from hints the container passes.
Delegates to sub-init methods and parses the following parameters:
config
- Passed by the servlet container.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.
request
- The http request.response
- The http response.Copyright © 2003-2022 The Apache Software Foundation. All rights reserved.