SeamFramework.orgCommunity Documentation
Sometimes developers need to access web application resources from application code. Typically the
ServletContext
is used to load resources by calling getResource()
.
Unfortunately the ServletContext
cannot be accessed in all situations.
Especially CDI extensions can be problematic in this regard as they are executed during a stage in the
application startup in which the ServletContext
may not have been created yet.
Solder offers some help in this situation. The class WebResourceLocator
provides a simple
way to obtain resources from the web application. Under the covers this class uses the
WebResourceLocationProvider
SPI to retrieve the location of the resources.
The following example shows how to use the class:
WebResourceLocator locator = new WebResourceLocator();
InputStream stream = locator.getWebResource("/WEB-INF/web.xml");
if (stream != null) {
// parse the input stream
}
As you can see using the WebResourceLocator
is very easy. Just create an instance of the
class and then use getWebResource()
to retrieve an InputStream.
Please note that you should always prefer to use the standard Servlet API to load resources from the web
application if possible. This Solder API is only intended to be used if it is not possible to use the
ServletContext
(like for example in CDI extensions).