|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javax.servlet.GenericServlet | +--javax.servlet.http.HttpServlet | +--org.apache.velocity.tools.view.servlet.VelocityViewServlet
A servlet to process Velocity templates. This is comparable to the the JspServlet for JSP-based applications.
The servlet provides the following features:
VelocityViewServlet supports the following configuration parameters in web.xml:
There are methods you may wish to override to access, alter or control any part of the request processing chain. Please see the javadocs for more information on :
loadConfiguration(javax.servlet.ServletConfig)
: setContentType(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
: requestCleanup(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.velocity.context.Context)
: error(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Exception)
:
Field Summary | |
static java.lang.String |
CONTENT_TYPE
The HTTP content type context key. |
static java.lang.String |
DEFAULT_CONTENT_TYPE
The default content type for the response |
static java.lang.String |
DEFAULT_OUTPUT_ENCODING
Default encoding for the output stream |
private java.lang.String |
defaultContentType
The default content type. |
private java.lang.String |
encoding
The encoding to use when generating outputing. |
protected static java.lang.String |
INIT_PROPS_KEY
This is the string that is looked for when getInitParameter is called ("org.apache.velocity.properties"). |
static java.lang.String |
SERVLET_CONTEXT_KEY
Key used to access the ServletContext in the Velocity application attributes. |
protected static java.lang.String |
TOOLBOX_KEY
Key used to access the toolbox configuration file path from the Servlet or webapp init parameters ("org.apache.velocity.toolbox"). |
private ServletToolboxManager |
toolboxManager
A reference to the toolbox manager. |
private static org.apache.velocity.util.SimplePool |
writerPool
Cache of writers |
Fields inherited from class javax.servlet.http.HttpServlet |
|
Fields inherited from class javax.servlet.GenericServlet |
|
Constructor Summary | |
VelocityViewServlet()
|
Method Summary | |
protected org.apache.velocity.context.Context |
createContext(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Creates and returns an initialized Velocity context. |
void |
doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Handles GET - calls doRequest() |
void |
doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Handle a POST request - calls doRequest() |
protected void |
doRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Handles with both GET and POST requests |
protected void |
error(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
java.lang.Exception cause)
Invoked when there is an error thrown in any part of doRequest() processing. |
org.apache.velocity.Template |
getTemplate(java.lang.String name)
Retrieves the requested template. |
org.apache.velocity.Template |
getTemplate(java.lang.String name,
java.lang.String encoding)
Retrieves the requested template with the specified character encoding. |
protected org.apache.velocity.Template |
handleRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
org.apache.velocity.context.Context ctx)
Handle the template processing request. |
void |
init(javax.servlet.ServletConfig config)
Initializes servlet, toolbox and Velocity template engine. |
protected void |
initToolbox(javax.servlet.ServletConfig config)
Initializes the ServletToolboxManager for this servlet's toolbox (if any). |
protected void |
initVelocity(javax.servlet.ServletConfig config)
Initializes the Velocity runtime, first calling loadConfiguration(ServletConfig) to get a org.apache.commons.collections.ExtendedProperties of configuration information and then calling Velocity.init(). |
protected org.apache.commons.collections.ExtendedProperties |
loadConfiguration(javax.servlet.ServletConfig config)
Loads the configuration information and returns that information as an ExtendedProperties, which will be used to initialize the Velocity runtime. |
protected void |
mergeTemplate(org.apache.velocity.Template template,
org.apache.velocity.context.Context context,
javax.servlet.http.HttpServletResponse response)
merges the template with the context. |
protected void |
requestCleanup(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
org.apache.velocity.context.Context context)
cleanup routine called at the end of the request processing sequence allows a derived class to do resource cleanup or other end of process cycle tasks |
protected void |
setContentType(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Sets the content type of the response. |
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 |
public static final java.lang.String CONTENT_TYPE
public static final java.lang.String DEFAULT_CONTENT_TYPE
public static final java.lang.String DEFAULT_OUTPUT_ENCODING
public static final java.lang.String SERVLET_CONTEXT_KEY
protected static final java.lang.String TOOLBOX_KEY
protected static final java.lang.String INIT_PROPS_KEY
private static org.apache.velocity.util.SimplePool writerPool
private java.lang.String encoding
private java.lang.String defaultContentType
private ServletToolboxManager toolboxManager
Constructor Detail |
public VelocityViewServlet()
Method Detail |
public void init(javax.servlet.ServletConfig config) throws javax.servlet.ServletException
Initializes servlet, toolbox and Velocity template engine. Called by the servlet container on loading.
init
in interface javax.servlet.Servlet
init
in class javax.servlet.GenericServlet
config
- servlet configuation
javax.servlet.ServletException
protected void initToolbox(javax.servlet.ServletConfig config) throws javax.servlet.ServletException
config
- servlet configuation
javax.servlet.ServletException
protected void initVelocity(javax.servlet.ServletConfig config) throws javax.servlet.ServletException
config
- servlet configuration parameters
javax.servlet.ServletException
protected org.apache.commons.collections.ExtendedProperties loadConfiguration(javax.servlet.ServletConfig config) throws java.io.IOException
<servlet> <servlet-name> YourServlet </servlet-name> <servlet-class> your.package.YourServlet </servlet-class> <init-param> <param-name> org.apache.velocity.properties </param-name> <param-value> velocity.properties </param-value> </init-param> </servlet>Alternately, if you wish to configure an entire context in this fashion, you may use the following:
<context-param> <param-name> org.apache.velocity.properties </param-name> <param-value> velocity.properties </param-value> <description> Path to Velocity configuration </description> </context-param>Derived classes may do the same, or take advantage of this code to do the loading for them via :
ExtendedProperties p = super.loadConfiguration(config);and then add or modify the configuration values from the file.
config
- ServletConfig passed to the servlets init() function
Can be used to access the real path via ServletContext (hint)
java.io.IOException
- I/O problem accessing the specified file, if specified.public void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
doGet
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
java.io.IOException
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
doPost
in class javax.servlet.http.HttpServlet
javax.servlet.ServletException
java.io.IOException
protected void doRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
request
- HttpServletRequest object containing client requestresponse
- HttpServletResponse object for the response
javax.servlet.ServletException
java.io.IOException
protected void requestCleanup(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, org.apache.velocity.context.Context context)
request
- servlet request from clientresponse
- servlet reponsecontext
- context created by the createContext() methodprotected org.apache.velocity.Template handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, org.apache.velocity.context.Context ctx) throws java.lang.Exception
Handle the template processing request.
request
- client requestresponse
- client responsectx
- VelocityContext to fill
java.lang.Exception
protected void setContentType(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
The default implementation is :
response.setContentType(defaultContentType);where defaultContentType is set to the value of the default.contentType property, or "text/html" if that is not set.
request
- servlet request from clientresponse
- servlet reponse to clientprotected org.apache.velocity.context.Context createContext(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
Creates and returns an initialized Velocity context.
A new context of classChainedContext
is created and
initialized.
request
- servlet request from clientresponse
- servlet reponse to clientpublic org.apache.velocity.Template getTemplate(java.lang.String name) throws org.apache.velocity.exception.ResourceNotFoundException, org.apache.velocity.exception.ParseErrorException, java.lang.Exception
name
- The file name of the template to retrieve relative to the
template root.
org.apache.velocity.exception.ResourceNotFoundException
- if template not found
from any available source.
org.apache.velocity.exception.ParseErrorException
- if template cannot be parsed due
to syntax (or other) error.
java.lang.Exception
- if an error occurs in template initializationpublic org.apache.velocity.Template getTemplate(java.lang.String name, java.lang.String encoding) throws org.apache.velocity.exception.ResourceNotFoundException, org.apache.velocity.exception.ParseErrorException, java.lang.Exception
name
- The file name of the template to retrieve relative to the
template root.encoding
- the character encoding of the template
org.apache.velocity.exception.ResourceNotFoundException
- if template not found
from any available source.
org.apache.velocity.exception.ParseErrorException
- if template cannot be parsed due
to syntax (or other) error.
java.lang.Exception
- if an error occurs in template initializationprotected void mergeTemplate(org.apache.velocity.Template template, org.apache.velocity.context.Context context, javax.servlet.http.HttpServletResponse response) throws org.apache.velocity.exception.ResourceNotFoundException, org.apache.velocity.exception.ParseErrorException, org.apache.velocity.exception.MethodInvocationException, java.io.IOException, java.io.UnsupportedEncodingException, java.lang.Exception
template
- template object returned by the handleRequest() methodcontext
- context created by the createContext() methodresponse
- servlet reponse (use this to get the output stream or Writer
org.apache.velocity.exception.ResourceNotFoundException
org.apache.velocity.exception.ParseErrorException
org.apache.velocity.exception.MethodInvocationException
java.io.IOException
java.io.UnsupportedEncodingException
java.lang.Exception
protected void error(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.Exception cause) throws javax.servlet.ServletException, java.io.IOException
request
- original HttpServletRequest from servlet container.response
- HttpServletResponse object from servlet container.cause
- Exception that was thrown by some other part of process.
javax.servlet.ServletException
java.io.IOException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |