Overview

This introduction to web applications follows on from the PicoContainer scopes page

There are only about a million Java web-frameworks for Java. There more recent and sophisticated ones try to do Dependency Injection for 'actions' or 'controllers'.

The most logical and commonly adopted paradigm when using Dependency Injection in a web framework is to define three container, one for each webapp scope:

  • application-scoped container: the root container created when the context is initialized
  • session-scoped container: the session container, created when the session is initialized, and having the application container as its parent
  • request-scoped container: the request container, created upon each request, and having the session one as its parent.

The WebappComposer interface allows the user to define which components are registered at each scope. These can either be defined programmatically, ie in Java, or read from scripts using the ScriptedWebappComposer.

The org.picocontainer.web.PicoServletContainerListener is the servlet listener that manages te instantiation, assembly and disposal of the the appropriate PicoContainers when applications/sessions start/stop. It actually creates a single instance of the scoped containers as the web app loads. For the application one, caching is set. For the session and request level ones, we store instantiated components for those scopes in ThreadLocal. For the session container, the same components are stored in the HttpSession as an attribute.

Here is an example of a web.xml:

<web-app>
  <context-param>
    <param-name>webapp-composer-class
    <param-value>org.picocontainer.web.sample.ExampleWebappComposer
  </context-param>

  <filter>
    <filter-name>picoFilter
    <filter-class>org.picocontainer.web.PicoServletContainerFilter
  </filter>

  <filter-mapping>
    <filter-name>picoFilter
    <url-pattern>/*
  </filter-mapping>

  <listener>
    <listener-class>org.picocontainer.web.PicoServletContainerListener
  </listener>

</web-app>

The above web.xml may not be sufficient for all web frameworks. Refer to the left menu for details of each supported web framework.