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

local stateless session hello

Stateless sessions make database queries and updates robust by setting transaction boundaries at each business method. This stateless session bean example annotates a single business method with a SUPPORTS transaction attribute, marking the method as a read-only transaction boundary.

See also:


A Hello, World example for EJB 3.0 is much simpler than for earlier versions of EJB. To implement the EJB you need to implement:

  • A local interface
  • The bean implementation

To configure Resin to be a server for the EJB you need to:

  • Configure the ejb-stateless-bean
  • Inject the bean into the application servlet

In this tutorial, a simple "Hello" EJB is created and deployed within Resin.

Files in this tutorial

WEB-INF/web.xmlweb.xml configuration
WEB-INF/classes/example/Hello.javaThe local interface for the stateless session bean
WEB-INF/classes/example/HelloBean.javaThe implementation for the stateless session bean
WEB-INF/classes/example/HelloServlet.javaThe client for the stateless session bean

Local Interface

The remote interface defines the client view of the bean. It declares all the business methods. Our only business method is the hello method.
package example;

public interface Hello {
  public String hello();

Bean Implementation

The second class for EJBs is the bean implementation class. It implements the functionality provided by the remote interface.
package example;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import static javax.ejb.TransactionAttributeType.SUPPORTS;

import javax.webbeans.Named;

public class HelloBean implements Hello {
  private String _greeting;

  public String hello()
    return _greeting;


The @Stateless annotation marks the bean as a stateless session bean. Resin will create a stub implementing Hello and store it in the WebBeans directory with type Hello and name @Named("HelloBean").

The @Stateless annotation can have an optional name value which overrides the default name of "HelloBean".


The @javax.webbeans.Named annotation tells Resin to lookup the greeting String in the WebBeans directory with the name binding "greeting" when the session bean is created.

In this example, the greeting is configured with an <env-entry> in the web.xml.

Alternate Dependency Injection

In some cases, it may be clearer to configure the session bean directly, rather than using WebBeans injection. Instead of creating a separate <env-entry>, you can configure the greeting value using XML straight from the resin-web.xml file.

<web-app xmlns="">

  <ejb-stateless-bean class="qa.TestBean">
      <greeting>Hello, World from web.xml</greeting>



Managing transactions is the primary purpose of stateless session beans. Transactions are a more powerful version of a synchronized lock used to protect database integrity. @TransactionAttribute marks the transaction boundary for each business method.

public String hello()

The hello() business method uses SUPPORTS because it's a read-only method. It doesn't need to start a new transaction on its own, but will participate in any transaction that already exists.

The REQUIRED transaction value starts up a new transaction if none already exists. It's used when updating database values.

REQUIREDStart a new transaction if necessary
SUPPORTSDon't start a new transaction, but use one if it exists

Configuring the EJB stateless bean

<ejb-stateless-bean> configure the session bean from the resin-web.xml. The <ejb-stateless-bean> entry will look at the bean's annotations to enhance the class.

ejb-stateless-bean in web.xml
<web-app xmlns="">

  <env-entry env-entry-name="greeting"
             env-entry-value="Hello, World."/>

  <ejb-stateless-bean class="qa.TestBean"/>


The <ejb-stateless-bean> can optionally configure the bean directly with an <init> tag as described in the alternate dependency injection section.

import javax.webbeans.In;

public class HelloServlet extends GenericServlet {
  @In private Hello _hello;

  public void service(HttpServletRequest req, HttpServletResponse res)
    throws IOException, ServletException
    PrintWriter out = res.getWriter();


The @In annotation tells Resin to look for a Hello component in the WebBeans directory.

The servlet could also lookup the Hello bean with JNDI in the init() method or use an <init> configuration in the web.xml:

alternative configuration
<servlet servlet-name="hello" servlet-class="example.HelloServlet">
  <init hello="${HelloBean}"/>


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