Sun Java System Application Server |
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.
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.
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"; }
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.
Follow these instructions to build, deploy, and run this sample application.
app_dir
is the sample application base
directory: samples_install_dir/javaee5/enterprise/servlet-stateless-ear
Change directory to app_dir.
app_dir>
ant
all
app_dir> ant
undeploy clean
Follow these instructions to build, deploy, and run this sample application using NetBeans IDE.
samples_install_dir/javaee5/enterprise/servlet-stateless-ear
as the project.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. 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
If you have problems when running the application, refer to troubleshooting document.
Copyright © 2006 Sun Microsystems, Inc. All rights reserved.