|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--javax.servlet.GenericServlet | +--javax.servlet.http.HttpServlet | +--org.apache.velocity.servlet.VelocityServlet
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 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 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 |
private static java.lang.String |
defaultContentType
The default content type, itself defaulting to DEFAULT_CONTENT_TYPE if not configured. |
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 java.lang.String |
OLD_INIT_PROPS_KEY
Use of this properties key has been deprecated, and will be removed in Velocity version 1.5. |
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. |
private static SimplePool |
writerPool
Cache of writers |
Fields inherited from class javax.servlet.http.HttpServlet |
HEADER_IFMODSINCE, HEADER_LASTMOD, LSTRING_FILE, lStrings, METHOD_DELETE, METHOD_GET, METHOD_HEAD, METHOD_OPTIONS, METHOD_POST, METHOD_PUT, METHOD_TRACE |
Fields inherited from class javax.servlet.GenericServlet |
config |
Constructor Summary | |
VelocityServlet()
|
Method Summary | |
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() . |
void |
doPost(javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Handles HTTP POST requests by calling #doRequest() . |
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)
Deprecated. Use handleRequest( HttpServletRequest request,
HttpServletResponse response, 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)
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,
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. |
Methods inherited from class javax.servlet.http.HttpServlet |
|
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, registerNatives, toString, wait, wait, wait |
Field Detail |
public 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
private static java.lang.String defaultContentType
DEFAULT_CONTENT_TYPE
if not configured.protected static final java.lang.String INIT_PROPS_KEY
org.apache.velocity.properties
).private static final java.lang.String OLD_INIT_PROPS_KEY
private static SimplePool writerPool
Constructor Detail |
public VelocityServlet()
Method Detail |
public void init(javax.servlet.ServletConfig config) throws javax.servlet.ServletException
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
protected 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> 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)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()
.doGet
in class javax.servlet.http.HttpServlet
public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
POST
requests by calling #doRequest()
.doPost
in class javax.servlet.http.HttpServlet
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 responseprotected 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 Writerprotected 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.protected Template handleRequest(Context ctx) throws java.lang.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.protected 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.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |