org.melati.test
Class WebmacroStandalone

Package class diagram package WebmacroStandalone
java.lang.Object
  extended by javax.servlet.GenericServlet
      extended by javax.servlet.http.HttpServlet
          extended by org.melati.test.WebmacroStandalone
All Implemented Interfaces:
Serializable, javax.servlet.Servlet, javax.servlet.ServletConfig

public class WebmacroStandalone
extends javax.servlet.http.HttpServlet

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.

See Also:
Serialized Form

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

WebmacroStandalone

public WebmacroStandalone()
Method Detail

init

public void init(javax.servlet.ServletConfig sc)
          throws javax.servlet.ServletException
The init() method will be called by your servlet runner whenever it wants to instantiate a new copy of your servlet. We initialize WebMacro here so as to avoid the expense of initializing it on every request.

Specified by:
init in interface javax.servlet.Servlet
Overrides:
init in class javax.servlet.GenericServlet
Throws:
javax.servlet.ServletException

destroy

public 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. You really ought to do this, since it might be a lot of resources.

Specified by:
destroy in interface javax.servlet.Servlet
Overrides:
destroy in class javax.servlet.GenericServlet

doGet

public void doGet(javax.servlet.http.HttpServletRequest req,
                  javax.servlet.http.HttpServletResponse resp)
           throws IOException
We only implement the GET method in this servlet.

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.

Overrides:
doGet in class javax.servlet.http.HttpServlet
Throws:
IOException


Copyright © 2000-2010 PanEris. All Rights Reserved.