|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavax.servlet.GenericServlet
javax.servlet.http.HttpServlet
org.apache.velocity.tools.view.servlet.VelocityViewServlet
public class 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)
: fillContext(org.apache.velocity.context.Context, javax.servlet.http.HttpServletRequest)
: 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 |
protected static java.lang.String |
DEFAULT_PROPERTIES_PATH
Default velocity properties file path. |
protected static java.lang.String |
DEFAULT_TOOLBOX_PATH
Default toolbox configuration file path. |
static java.lang.String |
DEFAULT_TOOLS_PROPERTIES
Default Runtime properties. |
private java.lang.String |
defaultContentType
The default content type. |
protected static java.lang.String |
INIT_PROPS_KEY
This is the string that is looked for when getInitParameter is called ("org.apache.velocity.properties"). |
private static long |
serialVersionUID
serial version id |
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"). |
protected ToolboxManager |
toolboxManager
A reference to the toolbox manager. |
private org.apache.velocity.app.VelocityEngine |
velocity
|
private boolean |
warnOfOutputStreamDeprecation
Whether we've logged a deprecation warning for ServletResponse's getOutputStream() . |
private static org.apache.velocity.util.SimplePool |
writerPool
Cache of writers |
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 e)
Invoked when there is an error thrown in any part of doRequest() processing. |
protected void |
fillContext(org.apache.velocity.context.Context context,
javax.servlet.http.HttpServletRequest request)
This is an extension hook for users who subclass this servlet to make their own modifications to the Context . |
protected java.lang.String |
findInitParameter(javax.servlet.ServletConfig config,
java.lang.String key)
Looks up an init parameter with the specified key in either the ServletConfig or, failing that, in the ServletContext. |
protected java.io.Writer |
getResponseWriter(javax.servlet.http.HttpServletResponse response)
Procure a Writer with correct encoding which can be used even if HttpServletResponse's getOutputStream() method
has already been called. |
protected org.apache.velocity.Template |
getTemplate(javax.servlet.http.HttpServletRequest request)
Gets the requested template. |
protected org.apache.velocity.Template |
getTemplate(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Gets the requested template. |
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.app.VelocityEngine |
getVelocityEngine()
Returns the underlying VelocityEngine being used. |
protected java.lang.String |
getVelocityProperty(java.lang.String key,
java.lang.String alternate)
Simplifies process of getting a property from VelocityEngine, because the VelocityEngine interface sucks compared to the singleton's. |
protected org.apache.velocity.Template |
handleRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
org.apache.velocity.context.Context ctx)
Deprecated. This will be removed in VelocityTools 2.0. |
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 velocityEngine.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. |
private org.apache.commons.collections.ExtendedProperties |
loadDefaultProperties()
|
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 |
performMerge(org.apache.velocity.Template template,
org.apache.velocity.context.Context context,
java.io.Writer writer)
This is here so developers may override it and gain access to the Writer which the template will be merged into. |
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. |
protected void |
setVelocityEngine(org.apache.velocity.app.VelocityEngine ve)
Sets the underlying VelocityEngine |
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 |
---|
private static final long serialVersionUID
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
public static final java.lang.String DEFAULT_TOOLS_PROPERTIES
protected static final java.lang.String TOOLBOX_KEY
protected static final java.lang.String INIT_PROPS_KEY
protected static final java.lang.String DEFAULT_TOOLBOX_PATH
protected static final java.lang.String DEFAULT_PROPERTIES_PATH
protected ToolboxManager toolboxManager
private static org.apache.velocity.util.SimplePool writerPool
private org.apache.velocity.app.VelocityEngine velocity
private java.lang.String defaultContentType
private boolean warnOfOutputStreamDeprecation
getOutputStream()
.
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.
NOTE: If no charset is specified in the default.contentType property (in your velocity.properties) and you have specified an output.encoding property, then that will be used as the charset for the default content-type of pages served by this servlet.
init
in interface javax.servlet.Servlet
init
in class javax.servlet.GenericServlet
config
- servlet configuation
javax.servlet.ServletException
protected java.lang.String findInitParameter(javax.servlet.ServletConfig config, java.lang.String key)
protected java.lang.String getVelocityProperty(java.lang.String key, java.lang.String alternate)
initVelocity(ServletConfig)
has already been called.
protected org.apache.velocity.app.VelocityEngine getVelocityEngine()
protected void setVelocityEngine(org.apache.velocity.app.VelocityEngine ve)
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
private org.apache.commons.collections.ExtendedProperties loadDefaultProperties()
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 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
This was a common extension point, but has been deprecated.
It has no single replacement. Instead, you should override
fillContext(org.apache.velocity.context.Context, javax.servlet.http.HttpServletRequest)
to add custom things to the Context
or override a getTemplate(javax.servlet.http.HttpServletRequest)
method to change how
Template
s are retrieved
request
- client requestresponse
- client responsectx
- VelocityContext to fill
java.lang.Exception
protected 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 clientprotected void fillContext(org.apache.velocity.context.Context context, javax.servlet.http.HttpServletRequest request)
Context
. It is a partial
replacement of the deprecated handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.velocity.context.Context)
method. This
implementation does nothing.
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.Template getTemplate(javax.servlet.http.HttpServletRequest request) throws org.apache.velocity.exception.ResourceNotFoundException, org.apache.velocity.exception.ParseErrorException, java.lang.Exception
Gets the requested template.
request
- client request
org.apache.velocity.exception.ResourceNotFoundException
org.apache.velocity.exception.ParseErrorException
java.lang.Exception
protected org.apache.velocity.Template getTemplate(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws org.apache.velocity.exception.ResourceNotFoundException, org.apache.velocity.exception.ParseErrorException, java.lang.Exception
Gets the requested template.
request
- client requestresponse
- client response (whose character encoding we'll use)
org.apache.velocity.exception.ResourceNotFoundException
org.apache.velocity.exception.ParseErrorException
java.lang.Exception
public 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(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
response
- servlet reponse (used to get a 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 performMerge(org.apache.velocity.Template template, org.apache.velocity.context.Context context, java.io.Writer writer) throws org.apache.velocity.exception.ResourceNotFoundException, org.apache.velocity.exception.ParseErrorException, org.apache.velocity.exception.MethodInvocationException, java.lang.Exception
template
- template object returned by the handleRequest() methodcontext
- Context created by the createContext(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
writer
- a VelocityWriter that the template is merged into
org.apache.velocity.exception.ResourceNotFoundException
org.apache.velocity.exception.ParseErrorException
org.apache.velocity.exception.MethodInvocationException
java.lang.Exception
protected void error(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, java.lang.Exception e) throws javax.servlet.ServletException
request
- original HttpServletRequest from servlet container.response
- HttpServletResponse object from servlet container.e
- Exception that was thrown by some other part of process.
javax.servlet.ServletException
protected java.io.Writer getResponseWriter(javax.servlet.http.HttpServletResponse response) throws java.io.UnsupportedEncodingException, java.io.IOException
Procure a Writer with correct encoding which can be used
even if HttpServletResponse's getOutputStream()
method
has already been called.
This is a transitional method which will be removed in a future version of Velocity. It is not recommended that you override this method.
response
- The response.
Writer
, possibly created using the
getOutputStream()
.
java.io.UnsupportedEncodingException
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(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |