The contents of this document are a work in progress

How to use Plexus in web applications

Before we demonstrate how we can set up a web application to start a Plexus Container and look up Plexus components, let us understand the alternatives that are available to embed Plexus container in web applications.


Plexus Servlet module exists for simplifying embedding and usage of Plexus in context of web applications which run inside the servlet container.

There are two ways of embedding Plexus.

  1. By setting up a PlexusServletContextListener
  2. By setting up a PlexusLoaderServlet.

    The first way is suported by servlet containers which are compliant with servlet specification 2.3 or higher. Second one should be used for compatibility with older versions of servlet containers

Setting up PlexusServletContextListener to embed Plexus


Setting up PlexusLoaderServlet to embed Plexus


By default, in both the above cases, the Plexus Embedder will search for the configuration files in the following locations:

  • /WEB-INF/plexus.xml (for main Plexus configuration file)
  • /WEB-INF/ (for properties file which is used for seeding Plexus context)

    These location can be overidden TODO: explain how

In both the above cases a new instance of Plexus Conatiner will be created and stored in "application" scope for the web application under the key: org.codehaus.plexus.PlexusConstants.PLEXUS_KEY

How to lookup and release components

In a Servlet

ServletContext context = getServletContext();
VelocityComponent velocityComponent = ( VelocityComponent ) PlexusServletUtils.lookup( context, VelocityComponent.ROLE );
PlexusServletUtils.release( context, velocityComponent );


VelocityComponent velocityComponent = ( VelocityComponent ) PlexusServletUtils.lookup( application, VelocityComponent.ROLE );
PlexusServletUtils.release( application, velocityComponent );

In Struts Action or in similar case

public abstract class BaseAction extends Action
    protected Object lookup( HttpServletRequest request, String role )
        throws ServletException
        HttpSession session = request.getSession();

        ServletContext application = session.getServletContext();

        Object retValue = PlexusServletUtils.lookup( application, role );

        return retValue;

    protected void release( HttpServletRequest request, Object component )
        throws ServletException
        HttpSession session = request.getSession();

        ServletContext application = session.getServletContext();

        PlexusServletUtils.release( application, component );

    void someAction( HttpServletRequest request )
        VelocityComponent velocityComponent= ( VelocityComponent ) lookup( request,  VelocityComponent.ROLE  );


        release( request, velocityComponent );

Obtaining the Plexus Container instance

If you need to access the instance of PlexusContainer object, it can be obtained by the following call:

PlexusContainer getPlexusContainer( ServletContext servletContext )

Required artifacts

Maven dependency:


Next: Setting up a Web Application Project