Init-based application scope


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:
  • 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.