Prev Class | Next Class | Frames | No Frames |
Summary: Nested | Field | Method | Constr | Detail: Nested | Field | Method | Constr |
HttpServlet
org.apache.velocity.servlet.VelocityServlet
public abstract class VelocityServlet
extends 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 :
Field Summary | |
static String |
|
static String |
|
static String |
|
protected static String |
|
private static String |
|
static String |
|
static String |
|
private static String |
|
private static SimplePool |
|
Method Summary | |
protected String |
|
protected Context |
|
void |
|
void |
|
protected void |
|
protected void |
|
Template |
|
Template |
|
protected Template |
|
protected Template |
|
void |
|
protected void |
|
protected Properties |
|
protected void |
|
protected void |
|
protected void |
|
public static final String CONTENT_TYPE
The HTTP content type context key.
public static final String DEFAULT_CONTENT_TYPE
The default content type for the response
public static final String DEFAULT_OUTPUT_ENCODING
Encoding for the output stream
protected static final String INIT_PROPS_KEY
This is the string that is looked for when getInitParameter is called (org.apache.velocity.properties
).
private static final String OLD_INIT_PROPS_KEY
Use of this properties key has been deprecated, and will be removed in Velocity version 1.5.
public static final String REQUEST
The context key for the HTTP request object.
public static final String RESPONSE
The context key for the HTTP response object.
private static String defaultContentType
The default content type, itself defaulting toDEFAULT_CONTENT_TYPE
if not configured.
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 byresponse.getCharacterEncoding()
). Called bysetContentType(HttpServletRequest,HttpServletResponse)
if an encoding isn't already specified by Content-Type. By default, chooses the value of RuntimeSingleton'soutput.encoding
property.
- Parameters:
request
- The servlet request from the client.
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.
- Parameters:
request
- servlet request from clientresponse
- servlet reponse to client
- Returns:
- context
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
Handles HTTPGET
requests by callingdoRequest()
.
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
Handles HTTPPOST
requests by callingdoRequest()
.
protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
Handles all requests (by default).
- Parameters:
request
- HttpServletRequest object containing client requestresponse
- HttpServletResponse object for the response
protected void error(HttpServletRequest request, HttpServletResponse response, Exception cause) throws ServletException, IOException
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.response
- HttpServletResponse object from servlet container.cause
- Exception that was thrown by some other part of process.
public Template getTemplate(String name) throws ResourceNotFoundException, ParseErrorException, Exception
Retrieves the requested template.
- Parameters:
name
- The file name of the template to retrieve relative to the template root.
- Returns:
- The requested template.
- Throws:
ResourceNotFoundException
- if template not found from any available source.ParseErrorException
- if template cannot be parsed due to syntax (or other) error.
public Template getTemplate(String name, String encoding) throws ResourceNotFoundException, ParseErrorException, Exception
Retrieves the requested template with the specified character encoding.
- Parameters:
name
- The file name of the template to retrieve relative to the template root.encoding
- the character encoding of the template
- Returns:
- The requested template.
- Throws:
ResourceNotFoundException
- if template not found from any available source.ParseErrorException
- if template cannot be parsed due to syntax (or other) error.
- Since:
- Velocity v1.1
protected Template handleRequest(HttpServletRequest request, HttpServletResponse response, Context ctx) throws Exception
Implement this method to add your application data to the context, calling thegetTemplate()
method to produce your return value.
In the event of a problem, you may handle the request directly and returnnull
or throw a more meaningful exception for the error handler to catch.
- Parameters:
request
- servlet request from clientresponse
- servlet reponsectx
- The context to add your data to.
- Returns:
- The template to merge with your context or null, indicating that you handled the processing.
- Since:
- Velocity v1.1
protected Template handleRequest(Context ctx) throws Exception
Deprecated. Use
handleRequest( HttpServletRequest request, HttpServletResponse response, Context ctx )
Implement this method to add your application data to the context, calling thegetTemplate()
method to produce your return value.
In the event of a problem, you may simple returnnull
or throw a more meaningful exception.
- Parameters:
ctx
- The context to add your data to.
- Returns:
- The template to merge with your context.
public void init(ServletConfig config) throws ServletException
Performs initialization of this servlet. Called by the servlet container on loading.
- Parameters:
config
- The servlet configuration to apply.
protected void initVelocity(ServletConfig config) throws ServletException
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.
protected Properties loadConfiguration(ServletConfig config) throws IOException, FileNotFoundException
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() function Can be used to access the real path via ServletContext (hint)
- Returns:
- java.util.Properties loaded with configuration values to be used to initialize the Velocity runtime.
protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException, UnsupportedEncodingException, Exception
merges the template with the context. Only override this if you really, really really need to. (And don't call us with questions if it breaks :)
- Parameters:
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
protected void requestCleanup(HttpServletRequest request, HttpServletResponse response, Context context)
A cleanup routine which is called at the end of thedoRequest(HttpServletRequest,HttpServletResponse)
processing sequence, allowing a derived class to do resource cleanup or other end of process cycle tasks.
- Parameters:
request
- servlet request from clientresponse
- servlet reponsecontext
- context created by the createContext() method
protected void setContentType(HttpServletRequest request, HttpServletResponse response)
Sets the content type of the response, defaulting todefaultContentType
if not overriden. Delegates tochooseCharacterEncoding(HttpServletRequest)
to select the appropriate character encoding.
- Parameters:
request
- The servlet request from the client.response
- The servlet reponse to the client.