<document>
<localtoc/>

A resource in Resin is any bean configured in the resin.conf or resin-web.xml. Resources are stored in the WebBeans registry. Because resources can be any Java class conforming to the bean configuration patterns, resources provide a flexible configuration.

There is also more documentation for Resin's IoC/Dependency Injection.

Some typical resources include:

- Databases
- JMS connections
- EJB stateful and stateless session beans
- JCA resources

Because resources are configured in the resin.conf/resin-web.xml and are created before any servlets start, they are very convenient for globally available beans, even allowing for more complex configuration beans extending the idea of <env-entry> (which stored simple Strings or Integers in JNDI.)

The `TestResource` bean is almost as simple as possible. It has a single String configuration parameter and does nothing but print its value using `toString()`.

package test; public class TestResource { private String _value = "default"; public void setValue(String value) { _value = value; } public String toString() { return "TestResource[" + _value + "]"; } }

In other words, you have lots of flexibility of things to configure as resources.

The resin-web.xml (or resin.conf) configures the resource with the <bean> tag. The resource is created and stored in the environment where it is defined. So a resource configured in the <host> will be available for all web-apps in that host and a resource configured in a <web-app> will only be available for that <web-app>.

<web-app xmlns="http://caucho.com/ns/resin"> <bean class="test.TestResource"> <init> <value>An Example Resource</value> </init> </bean> </web-app> <deftable>
tag description
bean defines the singleton service
class the class name of the resource bean
init Any bean-style configuration goes here
name the optional WebBeans name of the resource
value The example bean's `setValue` parameter.
</deftable>

Your PHP scripts can use any WebBeans resource by using the `java_bean()` method. The method will return the named resource.

<php? $resource = java_bean("testResource"); echo "PHP resource: " . $resource . "\n"; ?>

JSP pages can also use WebBeans resource in the JSP expression language.

JSP resource: ${testResource}

The example uses a servlet to demonstrate the resource, but any injectable class could use `@In` to look up and use the resource. Because resources are stored globally in WebBeans, they can avoid the complexity of passing objects around or storing in the servlet context.

If you save the resource, it's important that the saved field is reloaded when the web-app restarts. The resource has the same lifetime as its environment: web-app, host or cluster. When that environment closes, the resource is no longer valid and must be discarded. In particular, it is important to store any resource in an object's field, not in a static field or static hashtable.

package test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.webbeans.In; public class TestServlet extends HttpServlet { @In private TestResource _resource; public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { PrintWriter out = res.getWriter(); out.println("Resource: " + _resource); } } <results> Resource: TestResource[An example resource] </results>
</document>