groovy.servlet
Class TemplateServlet

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

public class TemplateServlet
extends javax.servlet.http.HttpServlet

A generic servlet for 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.

Example HelloWorld.template:


 
  <html>
  <body>
  <% 3.times { %>
  Hello World!
 
<% } %> </body> </html>


Note:
Automatic binding of context variables and request (form) parameters is disabled by default. You can enable it by setting the servlet config init parameters to true.

 bindDefaultVariables = init("bindDefaultVariables", false);
 bindRequestParameters = init("bindRequestParameters", false);
 

Version:
1.3
Author:
Christian Stein
See Also:
Serialized Form

Field Summary
protected  boolean bindDefaultVariables
           
protected  boolean bindRequestParameters
           
static String DEFAULT_CONTENT_TYPE
           
protected  TemplateEngine templateEngine
           
 
Constructor Summary
TemplateServlet()
           
 
Method Summary
protected  Binding createBinding(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Creates the application context.
protected  TemplateEngine createTemplateEngine(javax.servlet.ServletConfig config)
          Creates the template engine.
 void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Delegates to doRequest(HttpServletRequest, HttpServletResponse).
 void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Delegates to doRequest(HttpServletRequest, HttpServletResponse).
protected  void doRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Processes all requests by dispatching to helper methods.
protected  void error(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Exception exception)
          Simply sends an internal server error page (code 500).
protected  Template getTemplate(javax.servlet.http.HttpServletRequest request)
          Gets the template by its name.
protected  Template getTemplate(URL templateURL)
          Gets the template by its url.
protected  Template handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Binding binding)
          Default request handling.
 void init(javax.servlet.ServletConfig config)
          Initializes the servlet.
protected  boolean init(javax.servlet.ServletConfig config, String param, boolean value)
          Convient evaluation of boolean configuration parameters.
protected  void merge(Template template, Binding binding, javax.servlet.http.HttpServletResponse response)
          Merges the template and writes response.
protected  void requestDone(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Binding binding)
          Called one request is processed.
protected  URL resolveTemplateName(String templateName)
          Locate template and convert its location to an URL.
protected  void setContentType(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Sets DEFAULT_CONTENT_TYPE.
 
Methods inherited from class javax.servlet.http.HttpServlet
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, 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
 

Field Detail

DEFAULT_CONTENT_TYPE

public static final String DEFAULT_CONTENT_TYPE
See Also:
Constant Field Values

templateEngine

protected TemplateEngine templateEngine

bindDefaultVariables

protected boolean bindDefaultVariables

bindRequestParameters

protected boolean bindRequestParameters
Constructor Detail

TemplateServlet

public TemplateServlet()
Method Detail

init

public void init(javax.servlet.ServletConfig config)
Initializes the servlet.

Parameters:
config - Passed by the servlet container.

init

protected boolean init(javax.servlet.ServletConfig config,
                       String param,
                       boolean value)
Convient evaluation of boolean configuration parameters.

Parameters:
config - Servlet configuration passed by the servlet container.
param - Name of the paramter to look up.
value - Default value if parameter name is not set.
Returns:
true or false.

createTemplateEngine

protected TemplateEngine createTemplateEngine(javax.servlet.ServletConfig config)
Creates the template engine. Called by init(ServletConfig) and returns just SimpleTemplateEngine() if the init parameter templateEngine is not set.

Parameters:
config - This serlvet configuration passed by the container.
Returns:
The underlying template engine.
See Also:
#createTemplateEngine()

doGet

public void doGet(javax.servlet.http.HttpServletRequest request,
                  javax.servlet.http.HttpServletResponse response)
           throws javax.servlet.ServletException,
                  IOException
Delegates to doRequest(HttpServletRequest, HttpServletResponse).

Throws:
javax.servlet.ServletException
IOException

doPost

public void doPost(javax.servlet.http.HttpServletRequest request,
                   javax.servlet.http.HttpServletResponse response)
            throws javax.servlet.ServletException,
                   IOException
Delegates to doRequest(HttpServletRequest, HttpServletResponse).

Throws:
javax.servlet.ServletException
IOException

doRequest

protected void doRequest(javax.servlet.http.HttpServletRequest request,
                         javax.servlet.http.HttpServletResponse response)
                  throws javax.servlet.ServletException
Processes all requests by dispatching to helper methods. TODO Outline the algorithm. Although the method names are well-chosen. :)

Parameters:
request - The http request.
response - The http response.
Throws:
javax.servlet.ServletException - ...
IOException - ...

createBinding

protected Binding createBinding(javax.servlet.http.HttpServletRequest request,
                                javax.servlet.http.HttpServletResponse response)
                         throws Exception
Creates the application context. Sets 5 variables if and only if bindDefaultParameters is true:

 binding.setVariable("request", request);
 binding.setVariable("response", response);
 binding.setVariable("context", servletContext);
 binding.setVariable("session", request.getSession(true));
 binding.setVariable("out", response.getWriter());
 
Binds all form parameters, too. This is, where we leave the clean MVC pattern and Velocity behind. (...) Nobody told you to quit Velocity anyway. :)

Parameters:
request - The HTTP request.
response - The HTTP response.
Returns:
Groovy Binding also known as application context.
Throws:
Exception - Any exception.

setContentType

protected void setContentType(javax.servlet.http.HttpServletRequest request,
                              javax.servlet.http.HttpServletResponse response)
Sets DEFAULT_CONTENT_TYPE.

Parameters:
request - The HTTP request.
response - The HTTP response.

handleRequest

protected Template handleRequest(javax.servlet.http.HttpServletRequest request,
                                 javax.servlet.http.HttpServletResponse response,
                                 Binding binding)
                          throws Exception
Default request handling.
Leaving Velocity behind again. The template, actually the Groovy code in it, could handle/process the entire request. Good or not? This depends on you! :)
Anyway, here no exception is thrown -- but it's strongly recommended to override this method in derived class and do the real processing against the model inside it. The template should be used, like Velocity templates, to produce the view, the html page. Again, it's up to you!

Parameters:
request - The HTTP request.
response - The HTTP response.
binding - The application context.
Returns:
The template that will be merged.
Throws:
Exception

getTemplate

protected Template getTemplate(javax.servlet.http.HttpServletRequest request)
                        throws Exception
Gets the template by its name.

Returns:
The template that will be merged.
Throws:
Exception - Any exception.

resolveTemplateName

protected URL resolveTemplateName(String templateName)
                           throws Exception
Locate template and convert its location to an URL.

Parameters:
templateName - The name of the template.
Returns:
The URL pointing to the resource... the template.
Throws:
Exception - Any exception.

getTemplate

protected Template getTemplate(URL templateURL)
                        throws Exception
Gets the template by its url.

Parameters:
templateURL - The url of the template.
Returns:
The template that will be merged.
Throws:
Exception - Any exception.

merge

protected void merge(Template template,
                     Binding binding,
                     javax.servlet.http.HttpServletResponse response)
              throws Exception
Merges the template and writes response.

Parameters:
template - The template that will be merged... now!
binding - The application context.
response - The HTTP response.
Throws:
Exception - Any exception.

error

protected void error(javax.servlet.http.HttpServletRequest request,
                     javax.servlet.http.HttpServletResponse response,
                     Exception exception)
Simply sends an internal server error page (code 500).

Parameters:
request - The HTTP request.
response - The HTTP response.
exception - The cause.

requestDone

protected void requestDone(javax.servlet.http.HttpServletRequest request,
                           javax.servlet.http.HttpServletResponse response,
                           Binding binding)
Called one request is processed. This clean-up hook is always called, even if there was an exception flying around and the error method was executed.

Parameters:
request - The HTTP request.
response - The HTTP response.
binding - The application context.


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