O'Reilly logo

AspectJ Cookbook by Russ Miles

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

3.3. Deploying a Java Servlet That Uses AspectJ

Problem

You want to deploy a servlet that has been developed using AspectJ into Apache Tomcat.

Solution

Compile your Java Servlet from the command line using the ajc command or inside an Eclipse AspectJ project. Under the webapps directory inside Apache Tomcat, set up a new web application directory and WEB-INF subdirectory. Make the appropriate amendments to the server.xml file in the Tomcat configuration to enable your web application.

Copy the compiled Java Servlet .class files and corresponding aspect .class files into the webapps/%YOUR_APPLICATION_DIRECTORY%/WEB-INF/classes. Copy the aspectjrt.jar file into the webapps/%YOUR_APPLICATION_DIRECTORY%/WEB-INF/lib so the aspect-oriented features of your software can find the support components they need.

Amend your web application’s webapps/%YOUR_APPLICATION_DIRECTORY/WEB-INF/web.xml file to support access to the new Java Servlet. Finally, restart Tomcat to activate your web application.

Discussion

The following steps show how to create, compile, and deploy a simple Java Servlet that uses AspectJ:

  1. Create a Java Servlet and corresponding aspect similar to the ones shown in Examples Example 3-2 and Example 3-3.

    Example 3-2. A simple HelloWorld Java Servlet

    package com.oreilly.aspectjcookbook; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required