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
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:
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.
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.
Note that, the @Stateless annotation decorating the enterprise bean class. This lets the container know that CalculatorBean is a stateless session bean.
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");
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
- Create the enterprise bean: CalculatorBean
- Create the web client: WebClient
- Deploy example onto the server.
- Using a browser, run the web client.
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 InterfaceThe 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.*; |
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; |
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.
InitialContext ic = new InitialContext();
CalculatorRemote calculator = (CalculatorRemote)ic.lookup("example/CalculatorBean/remote");
评论
发表评论