Why use ActionServlet?
1. Traditional methods
When you create a web-aware application, you always deal with two things: the code (application logic)
and the look&feel (HTML pages). Traditional approaches use two basic methods:
- Code that generates the HTML output - servlets (or CGI scripts etc.), for example:
public class SimpleServlet extends HttpServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML><BODY>");
out.println("<H1>This is output from servlet.</H1>");
out.println("</BODY></HTML>");
out.close();
}
}
- Code that is embedded in HTML - JSP (or ASP, PHP etc.), for example:
<HTML>
<jsp:useBean id="cart" scope="session" class="ShoppingCart"/>
<jsp:setProperty name="cart" property="*" />
<% cart.processRequest(request); %>
<br> You have the following items in your cart:
<OL>
<%
String[] items = cart.getItems();
for (int i=0; i<items.length; i++) {
%>
<li> <%= items[i] %>
<%
}
%>
</OL>
</HTML>
Both these approaches mix application logic with look&feel, which should be created as quite separate tasks!
Not to mention traditioal PHP-like approach, where there are often hardwired three different things: 1) SQL database commands,
2) HTML and 3) PHP code. And it is similar with JSP + taglibs, which use: 1) Java snippets, 2) HTML and 3) custom tags.
2. Model-View-Controller (MVC)
Using a template engine like WebMacro or
Velocity is undoubtely an advance of
the programming model - you can separate Model (data representation), View
(HTML template) and Controller (manages the application flow).
In MVC model, there is almost no code in the HTML templates - just the simple WebMacro/Velocity script, which
can be easily understood by the web designer, and there is no HTML embedded in the program (servlet) code.
- Servlet code using WebMacro:
public class GuestBook extends WMServlet {
private Vector book = new Vector();
public Template handle(WebContext context) throws HandlerException {
if ("input".equals(context.getForm("action"))) {
// get the form variables
String name = (String) context.getForm("name"); // (1)
String email = (String) context.getForm("email");
// ...
context.put("registry", book); // (2)
return getTemplate("output.wm"); // (3)
}
}
}
- WebMacro HTML template:
<html>
#set $Response.ContentType = "text/html"
<TABLE>
#foreach $guest in $registry {
<TR>
<TD>$guest.Name</TD>
<TD>$guest.Email</TD>
</TR>
}
</TABLE>
</html>
The only "ugly" parts (in bold) are the necessities that retrieve parameters values from HTTP request (1),
set output $variables (2) and select templates to be displayed (3). "Ugly" means that the code above depends
on the template and vice versa. Simply, you cannot change parameters or template names without recompiling and
you cannot create reusable code easily.
3. One more level of abstraction
ActionServlet removes the dependencies mentioned above. You can map HTTP requests to methods and pass HTTP
parameters values to them, map return values to template names and bind $variables (not in the picture)
independently - by means of an XML configuration file (called ActionConfig):

4. Independence of components
Although there are other frameworks that help you to create web applications, many of them force you to implement
their proprietary interfaces or subclass and use their classes to make your application do anything.
Usually, you have to create wrapper classes if you want to interface your current logic.
In contrast, ActionServlet tries to minimalize the dependence on the framework. ActionServlet
components can be classes of any type (JavaBeans, EJBs, Corba objects etc.) and can be used directly (with no
wrappers) - their methods can be invoked by ActionServlet handled actions. Custom instantiation and initialization
of components is possible by Instantiators.
Independence of components is important, because they can be reused in any application without
modifications. Data between components can be sent easily by linking components with
<invoke> elements - ActionServlet
performs automatic data type conversion by means of type handlers:

5. Conclusion
With ActionServlet, you can directly use any components - they do not depend on the framework (nor templating engine).
You can deal easily with session data, because components can be scoped to session (or request, application, static)
- like with JSPs.
For more benefits see Specification: Development using ActionServlet
|