Haut: Background |
The contents of this document are a work in progress
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.
<web-app>
...
<listener>
<listener-class>org.codehaus.plexus.servlet.PlexusServletContextListener</listener-class>
</listener>
...
<web-app>
<servlet> <servlet-name>plexus</servlet-name> <servlet-class>org.codehaus.plexus.servlet.PlexusLoaderServlet</servlet-class> <load-on-startup>1</load-on-startup> </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 ); ... %>
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 );
}
}
If you need to access the instance of PlexusContainer object, it can be obtained by the following call:
PlexusContainer getPlexusContainer( ServletContext servletContext )
Maven dependency:
<dependency> <groupId>plexus</groupId> <artifactId>plexus-servlet</artifactId> <version>1.0-beta-2</version> </dependency>
Haut: Background |