Scopes
Scopes
 

Init-based application scope

Introduction

When you need the "application" scoped beans, you need to access the ServletContext. But to access ServletContext through a HttpServletRequest, you need to pass through the HttpSession object! You maybe asked: why do I have to pass through a session to get the ServletContext, while the ServletContext exists from the start of the web application? And what do I do if I don't have any session? Do I have to create a new one?

With Scopes now you can use an "init-based" application scope, where the ServletContext is configured at start-up of the web application and without passing through a session object.

To use window scope see Using Scopes with "window" scope

What to put in "init-based" application scope

You can put everything you need to put in "normal" application scope, because it is the same object (ServletContext) that it is used!

Using Scopes with "init-based application" scope

To use the init-based "application" scope support in your web application you have to:

  • extract the binary release;
  • copy the "scopes.jar" file in the directory [webapp_root]/WEB-INF/lib;
  • copy "lib/commons-io.jar" in [webapp_root]/WEB-INF/lib, or download the latest version from Jakarta Commons I/O site.
  • configure the CompleteHttpScopesFilter in front of your servlet, by modifying your "web.xml" file:
    <filter>
      <filter-name>scopesFilter</filter-name>
      <filter-class>net.sourceforge.scopes.filters.CompleteScopesHttpFilter</filter-class>
      <init-param>
      <param-name>manager-scope-names</param-name>
      <param-value>request,session,application</param-value>
      </init-param>
      <init-param>
      <param-name>manager-class-names</param-name>
      <param-value>
        net.sourceforge.scopes.managers.impl.RequestScopeManager,
        net.sourceforge.scopes.managers.impl.SessionScopeManager,
        net.sourceforge.scopes.managers.impl.InitBasedApplicationScopeManager
      </param-value>
      </init-param>
    </filter>
    <filter-mapping>
      <filter-name>scopesFilter</filter-name>
      <servlet-name>myServletName</servlet-name>
    </filter-mapping>
  • Installation finished! For the use see the Quick start

How "init-based" application scope works

When the Scopes filter is initialized, the "InitBasedApplicationScopeManager" takes the ServletContext from the FilterConfig object, using it for the rest of the execution.