| javax.servlet.http.HttpServlet org.apache.velocity.servlet.VelocityServlet
VelocityServlet | abstract public class VelocityServlet extends HttpServlet (Code) | | Base class which simplifies the use of Velocity with Servlets.
Extend this class, implement the 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 object
There 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 :
- loadConfiguration() : for setting up the Velocity runtime
- createContext() : for creating and loading the Context
- setContentType() : for changing the content type on a request
by request basis
- handleRequest() : you must implement this
- mergeTemplate() : the template rendering process
- requestCleanup() : post rendering resource or other cleanup
- error() : error handling
If you put a String with key "contentType" object into the context within either your
servlet or within your template, then that will be used to override
the default content type specified in the properties file.
author: Dave Bryson author: Jon S. Stevens author: Geir Magnusson Jr. author: Kent Johnson author: Daniel Rall author: $Id: VelocityServlet.java 463298 2006-10-12 16:10:32Z henning $ |
Method Summary | |
protected String | chooseCharacterEncoding(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() ).
Called by
VelocityServlet.setContentType(HttpServletRequest,HttpServletResponse) if an encoding isn't already specified by
Content-Type. | protected Context | createContext(HttpServletRequest request, 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. | public void | doGet(HttpServletRequest request, HttpServletResponse response) Handles HTTP GET requests by calling
VelocityServlet.doRequest(HttpServletRequest,HttpServletResponse) . | public void | doPost(HttpServletRequest request, HttpServletResponse response) Handles HTTP POST requests by calling
VelocityServlet.doRequest(HttpServletRequest,HttpServletResponse) . | protected void | doRequest(HttpServletRequest request, HttpServletResponse response) Handles all requests (by default). | protected void | error(HttpServletRequest request, HttpServletResponse response, Exception cause) Invoked when there is an error thrown in any part of doRequest() processing. | public Template | getTemplate(String name) Retrieves the requested template.
Parameters: name - The file name of the template to retrieve relative to thetemplate root. | public Template | getTemplate(String name, String encoding) Retrieves the requested template with the specified
character encoding. | protected Template | handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) Implement this method to add your application data to the context,
calling the getTemplate() method to produce your return
value.
In the event of a problem, you may handle the request directly
and return null or throw a more meaningful exception
for the error handler to catch.
Parameters: request - servlet request from client Parameters: response - servlet reponse Parameters: ctx - The context to add your data to. | protected Template | handleRequest(Context ctx) Implement this method to add your application data to the context,
calling the getTemplate() method to produce your return
value.
In the event of a problem, you may simple return null
or throw a more meaningful exception.
VelocityServlet.handleRequest(HttpServletRequest request,HttpServletResponse response,Context ctx) Parameters: ctx - The context to add your data to. | public void | init(ServletConfig config) Performs initialization of this servlet. | protected void | initVelocity(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 Properties | loadConfiguration(ServletConfig config) Loads the configuration information and returns that
information as a Properties, which will be used to
initialize the Velocity runtime. | protected void | mergeTemplate(Template template, Context context, HttpServletResponse response) merges the template with the context. | protected void | requestCleanup(HttpServletRequest request, HttpServletResponse response, Context context) A cleanup routine which is called at the end of the
VelocityServlet.doRequest(HttpServletRequest,HttpServletResponse) processing sequence, allowing a derived class to do resource
cleanup or other end of process cycle tasks. | protected void | setContentType(HttpServletRequest request, HttpServletResponse response) Sets the content type of the response, defaulting to
VelocityServlet.defaultContentType if not overriden. |
CONTENT_TYPE | final public static String CONTENT_TYPE(Code) | | The HTTP content type context key.
|
DEFAULT_CONTENT_TYPE | final public static String DEFAULT_CONTENT_TYPE(Code) | | The default content type for the response
|
DEFAULT_OUTPUT_ENCODING | final public static String DEFAULT_OUTPUT_ENCODING(Code) | | Encoding for the output stream
|
INIT_PROPS_KEY | final protected static String INIT_PROPS_KEY(Code) | | This is the string that is looked for when getInitParameter is
called (org.apache.velocity.properties ).
|
REQUEST | final public static String REQUEST(Code) | | The context key for the HTTP request object.
|
RESPONSE | final public static String RESPONSE(Code) | | The context key for the HTTP response object.
|
chooseCharacterEncoding | protected String chooseCharacterEncoding(HttpServletRequest request)(Code) | | 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() ).
Called by
VelocityServlet.setContentType(HttpServletRequest,HttpServletResponse) if an encoding isn't already specified by
Content-Type. By default, chooses the value of
RuntimeSingleton's output.encoding property.
Parameters: request - The servlet request from the client. The chosen character encoding. |
createContext | protected Context createContext(HttpServletRequest request, HttpServletResponse response)(Code) | | 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.
Parameters: request - servlet request from client Parameters: response - servlet reponse to client context |
error | protected void error(HttpServletRequest request, HttpServletResponse response, Exception cause) throws ServletException, IOException(Code) | | Invoked when there is an error thrown in any part of doRequest() processing.
Default will send a simple HTML response indicating there was a problem.
Parameters: request - original HttpServletRequest from servlet container. Parameters: response - HttpServletResponse object from servlet container. Parameters: cause - Exception that was thrown by some other part of process. throws: ServletException - throws: IOException - |
handleRequest | protected Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) throws Exception(Code) | | Implement this method to add your application data to the context,
calling the getTemplate() method to produce your return
value.
In the event of a problem, you may handle the request directly
and return null or throw a more meaningful exception
for the error handler to catch.
Parameters: request - servlet request from client Parameters: response - servlet reponse Parameters: ctx - The context to add your data to. The template to merge with your context or null, indicatingthat you handled the processing. throws: Exception - since: Velocity v1.1 |
initVelocity | protected void initVelocity(ServletConfig config) throws ServletException(Code) | | Initializes the Velocity runtime, first calling
loadConfiguration(ServletConvig) to get a
java.util.Properties of configuration information
and then calling Velocity.init(). Override this
to do anything to the environment before the
initialization of the singelton takes place, or to
initialize the singleton in other ways.
Parameters: config - throws: ServletException - |
loadConfiguration | protected Properties loadConfiguration(ServletConfig config) throws IOException, FileNotFoundException(Code) | | Loads the configuration information and returns that
information as a Properties, which will be used to
initialize the Velocity runtime.
Currently, this method gets the initialization parameter
VelocityServlet.INIT_PROPS_KEY, which should be a file containing
the configuration information.
To configure your Servlet Spec 2.2 compliant servlet runner to pass
this to you, put the following in your WEB-INF/web.xml file
<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.
Parameters: config - ServletConfig passed to the servlets init() functionCan be used to access the real path via ServletContext (hint) java.util.Properties loaded with configuration values to be usedto initialize the Velocity runtime. throws: FileNotFoundException - if a specified file is not found. throws: IOException - I/O problem accessing the specified file, if specified. |
Methods inherited from javax.servlet.http.HttpServlet | protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected void doHead(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected void doTrace(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) protected long getLastModified(HttpServletRequest req)(Code)(Java Doc) protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException(Code)(Java Doc) public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException(Code)(Java Doc)
|
|
|