Creating a servlet with ServletPathServlet and ServletPathDispatcher in ATG 9

Imagine that you want to create a servlet in ATG 9 that processes a POST request from an external website. Normal ATG Form Handlers are not of any use here because they use dynamic url parameters to be invoked. What you need is a plain old Java servlet. You can create one in ATG using two methods:

Option 1: What you would do in any other Application server without ATG

  • Create a class that extends javax.servlet.http.HttpServlet
  • Implement the doPost() and doGet() methods.
  • Add servlet mapping to web.xml

The disadvantage of the above method is that if you need access to the DynamoHttpRequest, DynamoHttpResponse objects, you need to extract them from the request using atg.servlet.ServletUtil.

public class ExampleServlet extends HttpServlet
{

    public doGet(HttpServletRequest request, HttpServletResponse response) {
       DynamoHttpServletRequest dRequest = ServletUtil.getDynamoRequest(request);
       DynamoHttpServletResponse dResponse= dRequest.getResponse();
      ...
     }
   public doPost(HttpServletRequest request, HttpServletResponse response) {
      ...
    }
}
web.xml
 <servlet>
     <servlet-name>exampleServlert</servlet-name>
     <servlet-class>com.spltech.co.uk.ExampleServlet</servlet-class>
 </servlet>

<servlet-mapping>

 <servlet-name>exampleServlet</servlet-name>

 <url-pattern>/exampleServlet</url-pattern>

 </servlet-mapping>

Option 2: The ATG Way – Using ServletPathServlet and ServletPathDispatcher

If you are developing in ATG, chances are is that you want to do things the ATG way. Here is how you do it:

  • Create a property file for /atg/dynamo/servlet/pipeline/ServletPathServlet. Override the servletPaths property and add the path that you want the servlet to be called for (e.g. /complete-payment).
  • Create a property file for /atg/dynamo/servlet/pipeline/ServletPathDispatcher. Override the dispatcherServiceMap property and add the mapping between the path that you added in the previous step and the ATG component that you want to be called.
  • Create a class that extends DynamoServlet. Override the service(DynamoHttpServletRequest req, DynamoHttpServletResponse resp) method and place here the code that does the business logic for your servlet.
  • Restart ATG and try to access your servlet at /dyn/complete-payment. In this case dyn is the default prefix added to all servlet paths called by the ServletPathDispatcher.

ServletPathServlet.properties:

##############################
#
# This splits the pathInfo of an incoming request into
# a servletPath/pathInfo combination, if the pathInfo
# starts with one of the servletPaths.  You can later
# dispatch off one of these paths using the
# ServletPathDispatcher component
#

servletPaths+=
			/example-servlet

ServletPathDispatcher.properties:

#
# This dispatcher will send any requests with a
# servletPath of "/exittracking" to the ExitTracking
# component.  If you add more servletPaths here, you
# should also remember to add those paths to the
# ServletPathServlet
#
dispatcherServiceMap+=
					/example-servlet=/uk/co/spltech/servlet/ExampleServlet

ExampleServlet.java:

/**
 * Example Servlet  
 * @author spltech
 *
 */
public class ExampleServlet extends DynamoServlet {
    public void service(DynamoHttpServletRequest req, DynamoHttpServletResponse res)
        throws ServletException, IOException
    { 
       ....
    }
}

ExampleServlet.properties:

$class=spltech.commerce.order.purchase.PaymentResponseServlet
$scope=global
...