Sun Java System Application Server 

Samples Index

The Servlet-Stateless Session Bean Sample Application

This is a simple EJB 3.0 stateless session bean with a local business interface that is invoked from a servlet. This sample demonstrates the concept of dependency injection - one of the new features of Java EE 5.0.

Business Interface

The stateless session bean has a local business interface with a single business method.

package enterprise.servlet_stateless_ejb;

public interface StatelessSession {
    public String sayHello(String name);
}

The lack of any annotation here implies that the StatelessSession is a local interface. To be more explicit, the javax.ejb.Local annotation can be used.

Stateless Session Bean Class

The bean implementation is:

package enterprise.servlet_stateless_ejb;

import javax.ejb.Stateless;

@Stateless
public class StatelessSessionBean
    implements StatelessSession {

    public String sayHello(String name) {
        return "Hello, " + name + "!\n";
    }

Servlet class

The servlet presents a text box to the user where the user can type a name. When the user clicks the submit button, the servlet greets the user whose name was typed in the text box. Internally, the servlet invokes the sayHello method of the stateless session bean passing the user name as the argument.

The servlet declares an instance variable whose type is same as the business interface of the session bean. Traditionally, the servlet would have looked up the bean's home interface from the component's environment and then would have created an instance using the create method of the home interface. However, with Java EE 5.0, the servlet can just declare its dependency on the bean by just annotating the instance variable with the @EJB annotation. At runtime, when the servlet instance is created, the container injects (or provides) an instance of an object that implements the business interface.

Building, Deploying, and Running the Application

Follow these instructions to build, deploy, and run this sample application.

  1. Setup your build environment and Configure the application server with which the build system has to work by following the common build instructions.
  2. app_dir is the sample application base directory: samples_install_dir/javaee5/enterprise/servlet-stateless-ear
  3. Change directory to app_dir.
  4. Build, Deploy and run the sample application using the target all
  5. app_dir>ant all

  6. The sample can be run by pointing your browser to the following URL: http://localhost:8080/servlet-stateless-war/servlet Or by calling ant run target
  7. Use the targets "undeploy"  and "clean"to undeploy the sample application and to remove the temporary directories like build and dist.

    app_dir> ant undeploy clean

Building, Deploying, and Running the Application in NetBeans IDE

Follow these instructions to build, deploy, and run this sample application using NetBeans IDE.

  1. Refer to common build instructions. for setting up NetBeans IDE and the application server with which the IDE will use.
  2. In NetBeans IDE, select File->OpenProject and select samples_install_dir/javaee5/enterprise/servlet-stateless-ear as the project.
  3. Right click on servlet-stateless-ear and select Run Project which will build, deploy and run the project. If configured properly, the IDE will open the browser with appropriate url. Sample output is given below.
  4. NetBeans IDE output
      All operations completed successfully
      post-run-deploy:
      run-deploy:
      Browsing: http://localhost:8080/servlet-stateless-war/
      run-display-browser:
      run-ac:
      run:
      BUILD SUCCESSFUL (total time: 6 seconds)
      
    Browser output
      http://localhost:8080/servlet-stateless-war/
    
      Servlet2Stateless:: Please enter your name
    
      Name:
      SubmitQuery
      

Troubleshooting

If you have problems when running the application, refer to troubleshooting document.

 


Copyright © 2006 Sun Microsystems, Inc. All rights reserved.