Stateless Session Bean Example

In this part of Enterprise Session Beans, you will learn how to develop, deploy, and run a simple Java EE application named example using stateless session bean. The purpose of example is to performs the mathematical operations such as Addition, Subtraction, Multiplication, and Division.
The example application consists of an enterprise bean, which performs the calculations, and two types of clients: an application client and a web client.

There are following steps that you have to follow to develop a example JEE application
  1. Create the enterprise bean: CalculatorBean
  2. Create the web client: WebClient
  3. Deploy example onto the server.
  4. Using a browser, run the web client.
I. Creating the enterprise bean:
The enterprise bean in our example is a stateless session bean called CalculatorBean. The source code for CalculatorBean is in “net/roseindia/ejb3/stateless” directory.
Creating CalculatorBean requires these steps:
(i) Coding the bean’s Remote business interface and Enterprise bean class.
(ii) Compiling the source code with the Ant tool.
 (i) Coding the Business Interface
The business interface defines the business methods that a client can call remotely. The business methods are implemented in the enterprise bean class. The source code for the CalculatorRemote business interface is given below.
package net.roseindia.ejb3.stateless; import java.math.*;
import javax.ejb.Remote;
import java.lang.annotation.*;
@Remote
public interface CalculatorRemote {
 public float add(float x, float y);
 public float subtract(float x, float y);
 public float multiply(float x, float y);
 public float division(float x, float y);
}
Note that, the @Remote annotation decorating the interface definition. This lets the container know that CalculatorBean will be accessed by remote clients.
II. Coding the Enterprise Bean Class
The enterprise bean class for this example is called CalculatorBean. This class implements the four business methods (add, subtract, multiply, division) that are defined in the CalculatorRemote  business interface. The source code for the CalculatorBean class is given below.
package net.roseindia.ejb3.stateless;

import java.math.*;
import javax.ejb.Stateless;
import javax.ejb.Remote;  

@Stateless(name="CalculatorBean")
@Remote(CalculatorRemote.class)  
public class CalculatorBean implements CalculatorRemote{
  public float add(float x, float y){   
    return x + y;
   }

   public float subtract(float x, float y){
      return x - y;
   }
    public float multiply(float x, float y){
      return x * y;
   }
   public float division(float x, float y){
     return x / y;
   }
}
Note that, the @Stateless annotation decorating the enterprise bean class. This lets the container know that CalculatorBean is a stateless session bean.

Compiling and Packaging the example Example

Now you are ready to compile the remote business interface (CalculatorRemote.java), the enterprise bean class (CalculatorBean.java) and the application client (CalculatorClient.java), then package the compiled classes into an enterprise bean JAR.
II. Creating the calculator Web Client
The web client is contained in the JSP page "WebClient.jsp". A JSP page is a text-based document that contains JSP elements, which construct dynamic content, and static template data, expressed in any text-based format such as HTML, WML, and XML.
The source code for the “form.jsp” is given below.

The following statements given below in “WebClient.jsp” are used for locating the business interface, creating an enterprise bean instance, and invoking a business method.
InitialContext ic = new InitialContext();
CalculatorRemote calculator = (CalculatorRemote)ic.lookup("example/CalculatorBean/remote");

 

评论

此博客中的热门博文

Fader av pingyins

Nu förbjuder Kina handel med elfenben

Kineserna vill köpa Volvos kompetens