|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.servlet.http.HttpServlet groovy.servlet.AbstractHttpServlet groovy.servlet.TemplateServlet
public class TemplateServlet extends AbstractHttpServlet
A generic servlet for serving (mostly HTML) templates.
It delegates work to agroovy.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>
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
generated.by = true(default) | false
groovy.source.encoding
Field Summary |
---|
Fields inherited from class AbstractHttpServlet | |
---|---|
CONTENT_TYPE_TEXT_HTML, INC_PATH_INFO, INC_REQUEST_URI, INC_SERVLET_PATH, encoding, reflection, resourceNameMatcher, resourceNameReplaceAll, resourceNameReplacement, servletContext, verbose |
Method Summary | |
---|---|
java.lang.Object
|
TemplateServlet()
Create new TemplateServlet. |
protected Template
|
getTemplate(java.io.File file)
Gets the template created by the underlying engine parsing the request. |
protected Template
|
getTemplate(java.net.URL url)
Gets the template created by the underlying engine parsing the request. |
void
|
init(ServletConfig config)
Initializes the servlet from hints the container passes. |
protected TemplateEngine
|
initTemplateEngine(ServletConfig config)
Creates the template engine. |
void
|
service(HttpServletRequest request, HttpServletResponse response)
Services the request with a response. |
Methods inherited from class AbstractHttpServlet | |
---|---|
getResourceConnection, getScriptUri, getScriptUriAsFile, init, setVariables |
Method Detail |
---|
public java.lang.Object TemplateServlet()
protected Template getTemplate(java.io.File file)
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 file containing the template source.
protected Template getTemplate(java.net.URL url)
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.
url
- The URL containing the template source..
public void init(ServletConfig config)
config
- Passed by the servlet container.
protected TemplateEngine initTemplateEngine(ServletConfig config)
new groovy.text.SimpleTemplateEngine()
if the init parameter
template.engine
is not set by the container configuration.
config
- Current servlet configuration passed by the container.null
on error.
public void service(HttpServletRequest request, HttpServletResponse 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.
Groovy Documentation