8.9. Creating a Web Interface

WAF uses the Servlet API for handling web requests. Any standard servlet, adapted to use the WAF servlet facilities (com.arsdigita.web.BaseApplicationServlet and related classes), can be used to serve your content. For our example application, we will use a basic Servlet to create our UI . For information on how to create a UI for your application using Bebop, refer to Chapter 12 Presentation (Bebop) Tutorial.

package com.example.binder;

import com.arsdigita.web.Application;
import com.arsdigita.web.BaseApplicationServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 * A servlet to serve pages of the binder application.
 *
 * @see com.example.binder.Binder
 * @author Justin Ross
 */
public final class BinderServlet extends BaseApplicationServlet {
    private static final Logger s_log = Logger.getLogger(BinderServlet.class);

    public void doService(final HttpServletRequest sreq,
                          final HttpServletResponse sresp,
                          final Application app)
            throws ServletException, IOException {
        sresp.getWriter().write("YO");
    }
}

Example 8-9. binder/src/com/example/binder/BinderServlet.java

An Application is associated with a path into the servlet container. The WAF dispatcher will look up your application and use its getContextPath and getServletPath methods to dispatch to your UI code. Our webapp will be mounted as binder in the servlet container, so we specify Binder's context path to be /binder.

    public final String getContextPath() {
        return "/binder";
    }

Example 8-10. Setting the application's dispatch destination

When the dispatcher encounters a request for an instance of the binder application, it will look up the appropriate Binder object and use its getContextPath and getServletPath methods to dispatch to the appropriate servlet code, in this case BinderServlet.

By specifying the servlet path this way, we index into the servlet container's native method of dispatch, by pattern. Your servlet container uses a webapp-standard file web.xml to declare pattern-to-servlet mappings.

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<web-app>
  <servlet>
    <servlet-name>binder</servlet-name>
    <servlet-class>com.example.binder.BinderServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>binder</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

Example 8-11. binder/web/WEB-INF/web.xml