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 :
| 限定符和类型 | 字段和说明 |
|---|---|
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
已过时。
Encoding for the output stream
|
protected static java.lang.String |
INIT_PROPS_KEY
已过时。
This is the string that is looked for when getInitParameter is
called (
bboss.org.apache.velocity.properties). |
static java.lang.String |
REQUEST
已过时。
The context key for the HTTP request object.
|
static java.lang.String |
RESPONSE
已过时。
The context key for the HTTP response object.
|
| 构造器和说明 |
|---|
VelocityServlet()
已过时。
|
| 限定符和类型 | 方法和说明 |
|---|---|
protected java.lang.String |
chooseCharacterEncoding(javax.servlet.http.HttpServletRequest request)
已过时。
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)
已过时。
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)
已过时。
Handles HTTP
GET requests by calling doRequest(HttpServletRequest, HttpServletResponse). |
void |
doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
已过时。
Handles HTTP
POST requests by calling doRequest(HttpServletRequest, HttpServletResponse). |
protected void |
doRequest(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
已过时。
Handles all requests (by default).
|
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.
|
Template |
getTemplate(java.lang.String name)
已过时。
Retrieves the requested template.
|
Template |
getTemplate(java.lang.String name,
java.lang.String encoding)
已过时。
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)
已过时。
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)
已过时。
Performs initialization of this servlet.
|
protected void |
initVelocity(javax.servlet.ServletConfig config)
已过时。
Initializes the Velocity runtime, first calling
loadConfiguration(ServletConvig) to get a
java.util.Properties of configuration information
and then calling Velocity.init().
|
protected java.util.Properties |
loadConfiguration(javax.servlet.ServletConfig config)
已过时。
Use VelocityViewServlet from the Velocity Tools
library instead.
|
protected void |
mergeTemplate(Template template,
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,
Context context)
已过时。
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)
已过时。
Sets the content type of the response, defaulting to
defaultContentType if not overriden. |
doDelete, doHead, doOptions, doPut, doTrace, getLastModified, service, servicepublic static final java.lang.String REQUEST
public static final java.lang.String RESPONSE
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
protected static final java.lang.String INIT_PROPS_KEY
bboss.org.apache.velocity.properties).public void init(javax.servlet.ServletConfig config)
throws javax.servlet.ServletException
init 在接口中 javax.servlet.Servletinit 在类中 javax.servlet.GenericServletconfig - The servlet configuration to apply.javax.servlet.ServletExceptionprotected void initVelocity(javax.servlet.ServletConfig config)
throws javax.servlet.ServletException
config - javax.servlet.ServletExceptionprotected java.util.Properties loadConfiguration(javax.servlet.ServletConfig config)
throws java.io.IOException,
java.io.FileNotFoundException
<servlet>
<servlet-name> YourServlet </servlet-name>
<servlet-class> your.package.YourServlet </servlet-class>
<init-param>
<param-name> bboss.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> bboss.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)java.io.FileNotFoundException - if a specified file is not found.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
GET requests by calling doRequest(HttpServletRequest, HttpServletResponse).doGet 在类中 javax.servlet.http.HttpServletrequest - response - javax.servlet.ServletExceptionjava.io.IOExceptionpublic void doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws javax.servlet.ServletException,
java.io.IOException
POST requests by calling doRequest(HttpServletRequest, HttpServletResponse).doPost 在类中 javax.servlet.http.HttpServletrequest - response - javax.servlet.ServletExceptionjava.io.IOExceptionprotected 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 responsejavax.servlet.ServletExceptionjava.io.IOExceptionprotected 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, 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 WriterResourceNotFoundExceptionParseErrorExceptionMethodInvocationExceptionjava.io.IOExceptionjava.io.UnsupportedEncodingExceptionjava.lang.Exceptionprotected 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 java.lang.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(java.lang.String name) throws ResourceNotFoundException, ParseErrorException, java.lang.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.java.lang.Exception - if an error occurs in template initializationpublic Template getTemplate(java.lang.String name, java.lang.String encoding) throws ResourceNotFoundException, ParseErrorException, java.lang.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.java.lang.Exception - if an error occurs in template initializationprotected Template handleRequest(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Context ctx) throws java.lang.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.java.lang.Exceptionprotected Template handleRequest(Context ctx) throws java.lang.Exception
getTemplate() method to produce your return
value.
null
or throw a more meaningful exception.ctx - The context to add your data to.java.lang.Exceptionprotected 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.ServletExceptionjava.io.IOException