public abstract class VelocityServlet
extends javax.servlet.http.HttpServlet
handleRequest()
method,
and add your data to the context. Then call
getTemplate("myTemplate.wm")
.
This class puts some things into the context object that you should
be aware of:
"req" - The HttpServletRequest object "res" - The HttpServletResponse objectThere are other methods you can override to access, alter or control any part of the request processing chain. Please see the javadocs for more information on :
Modifier and Type | Field and Description |
---|---|
static String |
CONTENT_TYPE
Deprecated.
The HTTP content type context key.
|
static String |
DEFAULT_CONTENT_TYPE
Deprecated.
The default content type for the response
|
static String |
DEFAULT_OUTPUT_ENCODING
Deprecated.
Encoding for the output stream
|
protected static String |
INIT_PROPS_KEY
Deprecated.
This is the string that is looked for when getInitParameter is
called (
org.apache.velocity.properties ). |
static String |
REQUEST
Deprecated.
The context key for the HTTP request object.
|
static String |
RESPONSE
Deprecated.
The context key for the HTTP response object.
|
Constructor and Description |
---|
VelocityServlet()
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
protected String |
chooseCharacterEncoding(javax.servlet.http.HttpServletRequest request)
Deprecated.
Chooses the output character encoding to be used as the value
for the "charset=" portion of the HTTP Content-Type header (and
thus returned by
response.getCharacterEncoding() ). |
protected Context |
createContext(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Deprecated.
Returns a context suitable to pass to the handleRequest() method
Default implementation will create a VelocityContext object, put the HttpServletRequest and HttpServletResponse into the context accessable via the keys VelocityServlet.REQUEST and VelocityServlet.RESPONSE, respectively. |
void |
doGet(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Deprecated.
Handles HTTP
GET requests by calling doRequest(HttpServletRequest, HttpServletResponse) . |
void |
doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Deprecated.
Handles HTTP
POST requests by calling doRequest(HttpServletRequest, HttpServletResponse) . |
protected void |
doRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Deprecated.
Handles all requests (by default).
|
protected void |
error(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
Exception cause)
Deprecated.
Invoked when there is an error thrown in any part of doRequest() processing.
|
Template |
getTemplate(String name)
Deprecated.
Retrieves the requested template.
|
Template |
getTemplate(String name,
String encoding)
Deprecated.
Retrieves the requested template with the specified
character encoding.
|
protected Template |
handleRequest(Context ctx)
|
protected Template |
handleRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
Context ctx)
Deprecated.
Implement this method to add your application data to the context,
calling the
getTemplate() method to produce your return
value. |
void |
init(javax.servlet.ServletConfig config)
Deprecated.
Performs initialization of this servlet.
|
protected void |
initVelocity(javax.servlet.ServletConfig config)
Deprecated.
Initializes the Velocity runtime, first calling
loadConfiguration(ServletConvig) to get a
java.util.Properties of configuration information
and then calling Velocity.init().
|
protected Properties |
loadConfiguration(javax.servlet.ServletConfig config)
Deprecated.
Use VelocityViewServlet from the Velocity Tools
library instead.
|
protected void |
mergeTemplate(Template template,
Context context,
javax.servlet.http.HttpServletResponse response)
Deprecated.
merges the template with the context.
|
protected void |
requestCleanup(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response,
Context context)
Deprecated.
A cleanup routine which is called at the end of the
doRequest(HttpServletRequest, HttpServletResponse)
processing sequence, allowing 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)
Deprecated.
Sets the content type of the response, defaulting to
defaultContentType if not overriden. |
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, service
public static final String REQUEST
public static final String RESPONSE
public static final String CONTENT_TYPE
public static final String DEFAULT_CONTENT_TYPE
public static final String DEFAULT_OUTPUT_ENCODING
protected static final String INIT_PROPS_KEY
org.apache.velocity.properties
).public void init(javax.servlet.ServletConfig config) throws javax.servlet.ServletException
init
in interface javax.servlet.Servlet
init
in class javax.servlet.GenericServlet
config
- The servlet configuration to apply.javax.servlet.ServletException
protected void initVelocity(javax.servlet.ServletConfig config) throws javax.servlet.ServletException
config
- javax.servlet.ServletException
protected Properties loadConfiguration(javax.servlet.ServletConfig config) throws IOException, FileNotFoundException
<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 :
Properties 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)FileNotFoundException
- if a specified file is not found.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, IOException
GET
requests by calling doRequest(HttpServletRequest, HttpServletResponse)
.doGet
in class javax.servlet.http.HttpServlet
request
- response
- javax.servlet.ServletException
IOException
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException
POST
requests by calling doRequest(HttpServletRequest, HttpServletResponse)
.doPost
in class javax.servlet.http.HttpServlet
request
- response
- javax.servlet.ServletException
IOException
protected void doRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException
request
- HttpServletRequest object containing client requestresponse
- HttpServletResponse object for the responsejavax.servlet.ServletException
IOException
protected void requestCleanup(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Context context)
doRequest(HttpServletRequest, HttpServletResponse)
processing sequence, allowing a derived class to do resource
cleanup or other end of process cycle tasks.request
- servlet request from clientresponse
- servlet reponsecontext
- context created by the createContext() methodprotected void mergeTemplate(Template template, Context context, javax.servlet.http.HttpServletResponse response) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException, UnsupportedEncodingException, 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 WriterResourceNotFoundException
ParseErrorException
MethodInvocationException
IOException
UnsupportedEncodingException
Exception
protected void setContentType(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
defaultContentType
if not overriden. Delegates to chooseCharacterEncoding(HttpServletRequest)
to select the
appropriate character encoding.request
- The servlet request from the client.response
- The servlet reponse to the client.protected String chooseCharacterEncoding(javax.servlet.http.HttpServletRequest request)
response.getCharacterEncoding()
).
Called by setContentType(HttpServletRequest,
HttpServletResponse)
if an encoding isn't already specified by
Content-Type. By default, chooses the value of
RuntimeSingleton's output.encoding
property.request
- The servlet request from the client.protected Context createContext(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
request
- servlet request from clientresponse
- servlet reponse to clientpublic Template getTemplate(String name) throws ResourceNotFoundException, ParseErrorException, Exception
name
- The file name of the template to retrieve relative to the
template root.ResourceNotFoundException
- if template not found
from any available source.ParseErrorException
- if template cannot be parsed due
to syntax (or other) error.Exception
- if an error occurs in template initializationpublic Template getTemplate(String name, String encoding) throws ResourceNotFoundException, ParseErrorException, Exception
name
- The file name of the template to retrieve relative to the
template root.encoding
- the character encoding of the templateResourceNotFoundException
- if template not found
from any available source.ParseErrorException
- if template cannot be parsed due
to syntax (or other) error.Exception
- if an error occurs in template initializationprotected Template handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Context ctx) throws Exception
getTemplate()
method to produce your return
value.
null
or throw a more meaningful exception
for the error handler to catch.request
- servlet request from clientresponse
- servlet reponsectx
- The context to add your data to.Exception
protected Template handleRequest(Context ctx) throws Exception
handleRequest( HttpServletRequest request,
HttpServletResponse response, Context ctx )
getTemplate()
method to produce your return
value.
null
or throw a more meaningful exception.ctx
- The context to add your data to.Exception
protected void error(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Exception cause) throws javax.servlet.ServletException, 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
IOException
Copyright © 2000–2016. All rights reserved.