groovy.servlet
Class TemplateServlet

java.lang.Object
  extended byjavax.servlet.GenericServlet
      extended byjavax.servlet.http.HttpServlet
          extended bygroovy.servlet.TemplateServlet
All Implemented Interfaces:
java.io.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 , Guillaume Laforge
See Also:
Serialized Form

Field Summary
static java.lang.String DEFAULT_CONTENT_TYPE
           
private  javax.servlet.ServletContext servletContext
           
protected  TemplateEngine templateEngine
           
 
Fields inherited from class javax.servlet.http.HttpServlet
 
Fields inherited from class javax.servlet.GenericServlet
 
Constructor Summary
TemplateServlet()
           
 
Method Summary
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, java.lang.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(java.net.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, java.lang.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  java.net.URL resolveTemplateName(java.lang.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 java.lang.String DEFAULT_CONTENT_TYPE
See Also:
Constant Field Values

servletContext

private javax.servlet.ServletContext servletContext

templateEngine

protected TemplateEngine templateEngine
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,
                       java.lang.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(javax.servlet.ServletConfig)

doGet

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

Throws:
javax.servlet.ServletException
java.io.IOException

doPost

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

Throws:
javax.servlet.ServletException
java.io.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 - ...

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 java.lang.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:
java.lang.Exception

getTemplate

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

Parameters:
request - The HttpServletRequest.
Returns:
The template that will be merged.
Throws:
java.lang.Exception - Any exception.

resolveTemplateName

protected java.net.URL resolveTemplateName(java.lang.String templateName)
                                    throws java.lang.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:
java.lang.Exception - Any exception.

getTemplate

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

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

merge

protected void merge(Template template,
                     Binding binding,
                     javax.servlet.http.HttpServletResponse response)
              throws java.lang.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:
java.lang.Exception - Any exception.

error

protected void error(javax.servlet.http.HttpServletRequest request,
                     javax.servlet.http.HttpServletResponse response,
                     java.lang.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-2005 The Codehaus. All Rights Reserved.