The actual procedures used to develop the application are described below.
1) Creating the EJB Project
Select [File] > [New] > [Project] from the menu bar to display [New Project].
Select [EJB] > [EJB Project] from the [New Project] wizard, then click [Next].
Check and enter the following setup items:
Setup Items | Setup Content |
---|---|
Project name | EJBSample |
Target Runtime | Interstage Application Server V11.1 IJServer Cluster (Java EE) |
EJB Module version | 3.0 |
Configuration | Default Configuration for Interstage Application Server V11.1 IJServer Cluster (Java EE) |
Add project to an EAR | Do not select. |
Set the information, then click [Next].The EJB module page is displayed.
Check and enter the following setup items. After the following information is set, click [Finish].
Setup Items | Setup Content |
---|---|
Source Folder | ejbModule |
Generate deployment descriptor | Do not check |
Point
Under EJB 3.0, a deployment descriptor is not required. Enterprise beans can be created using only annotation.
2-1) Creating the Data Class
Create a data class that stores the country name and total population information, and gets information. To create the data class, select the created project, then right-click to display the context menu. Select [New] > [Class] from the context menu. The [New Java Class] wizard is displayed.
Check and enter the following setup items. After the following information is set, click [Finish].
Setup Items | Setup Content |
---|---|
Source folder | EJBSample/ejbModule |
Package | sample |
Name | CountryData |
The following source file is generated:
Source File | Description |
---|---|
CountryData.java | Data class |
Implement the processing for retaining the country names and total populations. Add the places shown in red below to the source.
Data Class Implementation (CountryData.java)
package sample; public class CountryData { private String countryName; private int totalPopulation; public CountryData(String name, int total) { setCountryName(name); setTotalPopulation(total); } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public int getTotalPopulation() { return totalPopulation; } public void setTotalPopulation(int totalPopulation) { this.totalPopulation = totalPopulation; } } |
Point
After the fields are added, and while the class is in the selected state, [Source] > [Generate Getters and Setters] can be selected from the menu to add getter/setter.
2-2) Creating the Session Bean Pattern
To create the session bean pattern, select the created project, then right-click to display the context menu. Select [New] > [Session Bean] from the context menu. The [Create EJB 3.0 Session Bean] wizard is displayed.
Check and enter the following setup items.
Setup Items | Setup Content |
---|---|
EJB project | EJBSample |
Source folder | /EJBSample/ejbModule |
Java package | sample |
Class name | PopulationRanking |
State type | Stateless |
Remote | Do not select. |
Local | Select sample.PopulationRankingLocal |
After the information is set, click [Next]. The information setup page of the session bean is displayed.
Check the following setup items. After the information is set and checked, click [Finish].
Setup Items | Setup Content |
---|---|
Bean name | PopulationRanking |
Transaction type | Container |
Interfaces | Sample.PopulationRankingLocal |
Inherited abstract methods | Select |
Constructors from superclass | Select |
When [Finish] is clicked, the following source files are generated.
Source Files | Description |
---|---|
PopulationRanking.java | Session bean class |
PopulationRankingLocal.java | Business interface |
2-3) Implementing the Session Bean
Implement as a session bean method the processing for entering the ranking and returning the country name and total population. Add the places in red below to the source.
Business Interface Implementation (PopulationRankingLocal.java)
package sample; import javax.ejb.Local; @Local public interface PopulationRankingLocal { public CountryData getCountryData(int rank); } |
Session Bean Class Implementation (PopulationRanking.java)
package sample; import javax.ejb.Stateless; /** * Session Bean implementation class PopulationRanking */ @Stateless public class PopulationRanking implements PopulationRankingLocal { /** * Default constructor. */ public PopulationRanking() { } private CountryData countries[] = new CountryData[] { new CountryData("China",1330000000), new CountryData("India",1140000000), new CountryData("America",300000000), new CountryData("Indonesia",230000000), new CountryData("Brazil",190000000), new CountryData("Pakistan",160000000), new CountryData("Bangladesh",150000000), new CountryData("Russia",140000000), new CountryData("Nigeria",140000000), new CountryData("Japan",130000000) }; public CountryData getCountryData(int rank) { --rank; if (rank < 0 || rank >= countries.length) { return null; } return countries[rank]; } } |
Select [File] > [New] > [Project] from the menu bar. The [New Project] is displayed.
Select [Java EE] > [Enterprise Application Project] from the [New Project] wizard, then click [Next].
Check and enter the following setup items:
Setup Items | Setup Content |
---|---|
Project name | EJBEARSample |
Target Runtime | Interstage Application Server V11.1 IJServer Cluster (Java EE) |
EAR version | 5.0 |
Configuration | Default Configuration for Interstage Application Server V11.1 IJServer Cluster (Java EE) |
Set the information, then click [Next].The module page to be added to EAR is displayed.
Check and enter the following setup items. After the following information is set, click [Finish].
Setup Items | Setup Content |
---|---|
Java EE Module Dependencies | Select EJBSample. |
Content Directory | EarContent |
Generate deployment descriptor | Do not select. |
If developing applications under normal circumstances, procedures to create a Web application as a client are needed, but in this introduction, a sample is imported and the important points as an EJB client are checked. If you would like to learn the detailed procedure for creating a Web application, refer to "2.2 Introduction" in the Web application chapter.
Use the following procedure to import a sample.
Select [File] > [Import] from the menu. Select [General] > [Existing Projects into Workspace] from the displayed Import wizard. Select [Select archive file], then click the [Browse] button and select the following files:
<Workbench installation folder>\sample\EJBSample.zip
Select [EJBClientSample] from [Projects], then click [Finish].
After the EJBClientSample project has been imported, select the EJBEARSample project. Right-click to display the context menu and select [Properties]. Select [Java EE Module Dependencies] from the Properties dialog box. Select EJBClientSample and add the EJB client to the EAR.
4-1) Creating the Client Project
Create a dynamic Web project as an EJB client. Since this can be completed by importing a sample, details are omitted. Refer to the following table for the settings.
Setup Items | Setup Content |
---|---|
Project name | EJBClientSample |
Target Runtime | Interstage Application Server V11.1 IJServer Cluster (Java EE) |
Configuration | Default Configuration for Interstage Application Server V11.1 IJServer Cluster (Java EE) |
Dynamic Web Module version | 2.5 |
Add project to an EAR | Select |
EAR Project Name | EJBEARSample |
4-2) Setting the Build Path
Set the build path (classpath) used to reference the business interface (PopulationRanking). This is already set in the imported sample project.
To check the settings, select the project (EJBClientSample), and right-click. Select [Properties] from the context menu. Select [Java EE Module Dependencies] from the Properties dialog box to display the [Java EE Modules] tab.
Check that the following setting is selected.
Setup Items | Setup Content |
---|---|
JAR/Module | EJBSample.jar |
4-3) Creating the Servlet Class
Create the servlet class. The servlet class is already created in the imported sample project with the following settings.
Setup Items | Setup Content |
---|---|
Java package | sample |
Class name | ServletController |
Superclass | javax.servlet.http.HttpServlet |
Which method stubs would you like to create? | Constructors from the superclass |
4-4) Creating the I/O Page
Create the I/O page JSP. A file like the following is already created in the imported sample project.
Input Page (default.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>World ranking for total population</title> </head> <body> <h1>Input page</h1> <p> Enter a ranking from 1 to 10. </p> <p></p> <form action="ServletController" method="post"> Rank:<br> <input name="rank" type="text" size="10">Rank<br> <p></p> <input type="submit" value="OK"> <input type="reset" value="Clear"> </form> </body> </html> |
Output Page (result.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>World ranking for total population</title> </head> <body> <h1>Output page</h1> Total population ranking ${param.rank} rank is "${applicationScope.ranking.countryName}"<br> Total population is ${applicationScope.ranking.totalPopulation} people. <br> <hr> <form action="ServletController" method="post"> <input type="submit" value=" Back to input page"> <input type="hidden" name="mode" value="top"> </form> </body> </html> |
Error Page (error.jsp)
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>World ranking for total population</title> </head> <body> <h1>Error page</h1> The entered ranking is not in the specified range, or a server error occurred.<br> <br> <hr> <form action="ServletController" method="post"> <input type="submit" value=" Back to input page "> <input type="hidden" name="mode" value="top"> </form> </body> </html> |
4-5) Setting the Dependency Injection
The implementation for invoking the session bean is performed in the servlet class. This is already implemented in the servlet source of the imported sample project.
Create a business interface field, set the following EJB annotation, and specify the Dependency Injection.
EJB Annotation Definition
@EJB private PopulationRankingLocal business; |
Point
Instead of the lookup method used up to J2EE1.4, Dependency Injection is used in Java EE and can be used to fetch and use an object. (During execution, the object is automatically set in a field by means of a Java EE container.)
4-6) Implementing the Session Bean Invocation Processing
To invoke the session bean method, simply use the business interface type field that defined the EJB annotation to invoke the method without making any changes.
This is already implemented in the servlet source of the imported sample project.
Definition for the Session Bean Invocation Method
CountryData country = business.getCountryData(rank); |
The servlet class in which session bean invocation is implemented is as shown below. (The imported project sample source is in this format).
The parts in red are the added and changed places.
Servlet Class (ServletController.java)
package sample; import java.io.IOException; import javax.ejb.EJB; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class ServletController */ public class ServletController extends HttpServlet { private static final long serialVersionUID = 1L; @EJB private PopulationRankingLocal business; /** * @see HttpServlet#HttpServlet() */ public ServletController() { super(); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("ISO-8859-1"); ServletContext sc = getServletContext(); // Get the type of file to be invoked. String mode = request.getParameter("mode"); if (mode != null && mode.equals("top")) { // Invoke the Input page HTML file. RequestDispatcher inRd = getServletContext().getRequestDispatcher("/default.jsp"); inRd.forward(request, response); return; } int rank; // Input check try { rank = Integer.parseInt(request.getParameter("rank")); } catch (NumberFormatException e) { RequestDispatcher errRd = sc.getRequestDispatcher("/error.jsp"); errRd.forward(request, response); return; } CountryData country = business.getCountryData(rank); |
Point
After the required implementation is coded, right-click on the Java editor, then select [Source] > [Organize Imports] from the context menu. The import statement that is appropriate for the classpath that was set in the project can then be inserted.
5) Checking the Application Operation
5-1) Associating the Project with a Server
Select a server in the Servers view, then select [Add and Remove Projects] from the context menu.
Select the created EAR project from the available projects, then click [Add]. Check that the EAR project has been added to the configured projects.
Check the following setup items, then click [Finish].
Setup Items | Setup Content |
---|---|
Configured projects | EJBEARSample |
Point
If the deployment destination server in the Servers view is not registered, then it must be added. For details on how to do that, refer to "6.2.6 Checking Application Behavior".
5-2) Setting Breakpoints
Set a breakpoint at the first line of the getCountryData() method of the session bean class. To set or delete a breakpoint, double-click the ruler part on the left edge of the editor.
5-3) Launching the Server
Select a server in the Servers view, then select [Debug] or [Restart in Debug] or [Connect(Debug Launch)/Login] from the context menu.
Point
If you want to simply launch the application without using the debugger, select [Start] from the context menu.
The EAR file is deployed automatically before the server starts.
Check the following information in the Servers view:
Item to Check | Contents to Check |
---|---|
Server state | Started |
Server status | Synchronized |
EAR file (EJBEARSample) state | Synchronized |
State of the EJB-JAR (EJBSample) file under the EAR file | Synchronized |
State of the WAR file (EJBClientSample) under the EAR file | Synchronized |
5-4) Executing the Application
In the Servers view, select the deploying Web project (EJBClientSample), then from the context menu, select [Web browser]. The Web browser is started, and the input window is opened.
Setup Items | Setup Content |
---|---|
Input page URL | http://localhost/EJBClientSample/ |
Enter a population ranking in the [Rank:] input field, then click [OK].
Point
The user can select the project, then select [Run As] > [Run on Server] or [Debug As] > [Debug on Server] from the context menu to add the server, add the project, launch the server, and launch the client all together.
In addition, the Web browser used as the client can be changed by selecting [Window] > [Web Browser] from the menu.
5-5) Debugging the Application
The application runs and is interrupted at the breakpoints. Check the Variables view display. Select [Run] > [Step Over] from the menu and check the program state in the Variables view. For debug details, refer to "6.2.6.1 Debugging".
Point
In addition to checking values, values can be changed in the Variables view.
Processing that has been interrupted by the debugger can be restarted by selecting [Run] > [Resume] from the menu. This allows the processing on the server side to complete so that the output page is displayed on the Web browser. Check that the country name and total population for the entered ranking is displayed.
6) Deploying the Application to the Operating Environment
An EAR file must be created in order to deploy the application to the operating environment. The created EAR file uses the server deployment function to deploy the application to the server.
6-1) Exporting the Application
The EAR file is created from the Export wizard. To launch the Export wizard, select [File] > [Export]. From [Export] wizard, select [Java EE] > [EAR file].
The EAR Export wizard is displayed.
Check and enter the following setup items. After the following information is set, click [Finish].
Setup Items | Setup Content |
---|---|
EAR project | Specify EJBEARSample. |
Destination | Specify the creation destination for the EAR file. |
6-2) Deploying to the Operating Environment
The application can be deployed from a remote environment to the operating environment by logging in to the Interstage Java EE Admin Console at the operating environment and by specifying a local EAR file.