|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object javax.servlet.GenericServlet javax.servlet.http.HttpServlet org.melati.test.WebmacroStandalone
public class WebmacroStandalone
This example demonstrates using WebMacro in "standalone" mode. Instead of subclassing from WMServlet you create and maintain your own WebMacro object and you are free to subclass from another servlet. Also, this technique can be used outside the servlet context.
The WebMacro master object is initialized when the servlet is initialized and destroyed when the servlet is destroyed. There is some overhead involved in creating the interface so you should prefer not to create one on every request, although it is not too expensive.
This servlet can be compiled and installed as an ordinary servlet. You need to ensure that your WebMacro.properties file is properly configured and available on your CLASSPATH. When setting up WebMacro.properties make sure that the TemplatePath is correctly set and that the template used by this servlet, "standalone.wm", is available on that path.
Constructor Summary | |
---|---|
WebmacroStandalone()
|
Method Summary | |
---|---|
void |
destroy()
It's not strictly necessary to destroy the WebMacro object when you're done with it--garbage collection will eventually get it--but it makes sure the resources get freed. |
void |
doGet(javax.servlet.http.HttpServletRequest req,
javax.servlet.http.HttpServletResponse resp)
We only implement the GET method in this servlet. |
void |
init(javax.servlet.ServletConfig sc)
The init() method will be called by your servlet runner whenever it wants to instantiate a new copy of your servlet. |
Methods inherited from class javax.servlet.http.HttpServlet |
---|
doDelete, doHead, doOptions, doPost, doPut, doTrace, getLastModified, service, service |
Methods inherited from class javax.servlet.GenericServlet |
---|
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public WebmacroStandalone()
Method Detail |
---|
public void init(javax.servlet.ServletConfig sc) throws javax.servlet.ServletException
init
in interface javax.servlet.Servlet
init
in class javax.servlet.GenericServlet
javax.servlet.ServletException
public void destroy()
destroy
in interface javax.servlet.Servlet
destroy
in class javax.servlet.GenericServlet
public void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws IOException
We create a WebContext object, populate it with some data, select a template, and write the output of that template to the output stream. Note that you need to pass the WebContext object to the template in order to write it, you also need an output stream, in this case the HttpServletResponse output stream is used.
If you were using WebMacro outside a servlet context you would not be able to construct a WebContext object, since you would not have the HttpServletRequest and HttpServletResponse objects which you need in order to do that. WebContext is a subclass of Context, so outside of a servlet just construct a TemplateContext object. You will obviously lose the ability to talk about servlet specific things in your templates (such as "Cookies") but otherwise it's the same.
There are a few exceptions you have to deal with. First, WebMacro may not be able to locate the template you've requested in which case it'll throw a NotFoundException. Second, the template will expect to find certain information in the TemplateContext, and if you fail to provide that information a ContextException will be thrown.
doGet
in class javax.servlet.http.HttpServlet
IOException
|
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |