Caucho maker of Resin Server | Application Server (Java EE Certified) and Web Server


Resin Documentation

home company docs 
app server 
 Resin Server | Application Server (Java EE Certified) and Web Server

a simple resource bean

Resources are beans configured in the resin.conf or resin-web.xml and stored in the WebBeans registry. The tutorial shows the configuration of a trivial bean as a resource and using it from a JSP file.



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

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.

resin-web.xml configuration

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="">

  <bean class="test.TestResource">
      <value>An Example Resource</value>

beandefines the singleton service
classthe class name of the resource bean
initAny bean-style configuration goes here
namethe optional WebBeans name of the resource
valueThe example bean's setValue parameter.

Using the resource from PHP

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


$resource = java_bean("testResource");

echo "PHP resource: " . $resource . "\n";


Using the resource from JSP

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


JSP resource: ${testResource}

Using the resource from a Servlet

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 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);
Resource: TestResource[An example resource]


Copyright © 1998-2015 Caucho Technology, Inc. All rights reserved. Resin ® is a registered trademark. Quercustm, and Hessiantm are trademarks of Caucho Technology.

Cloud-optimized Resin Server is a Java EE certified Java Application Server, and Web Server, and Distributed Cache Server (Memcached).
Leading companies worldwide with demand for reliability and high performance web applications including, CNET, DZone and many more are powered by Resin.

home company docs 
app server