Developing Java EE Enterprise Applications
This tutorial presents the basic features, concepts, and techniques for getting started in the development of Java EE enterprise applications. You will learn how to:
- Create an enterprise application (EAR) project
- Add and remove modules
- Package, deploy, and test an EAR project
This feature is available in MyEclipse.
1. Creating an Enterprise Application Project
In this example, you will create a Java EE enterprise application (EAR) project named Test1 that is composed of a web project, EJB project, client project, and connector project. The dependent projects are named Test1Web, Test1EJB, Test1Client, and Test1Connector respectively. An EAR Project must have at least one web project or one EJB project associated with it.
- Select File>New>Project, select Enterprise Application Project, and click Next.
- Type Test1 into the Project name field.
- Accept the default Java EE version and target runtime, and click Next.
Creating an Enterprise Application project - Click New Module, and click Finish to create all applicable modules. The modules are included and selected in the dependencies list.
Selected dependencies - Click Finish to create the EAR project.
Test1 EAR and dependent projects
2. Adding and Removing Modules
MyEclipse module projects can be added to or removed from an EAR project at any time as long as the EAR Project maintains at least one web or EJB module project association. Adding and removing modules is done using the project’s Deployment Descriptor property.
- Note: The process of adding or removing a module project changes the physical shape of the deployed form of an EAR project. If your EAR project is deployed, undeploy it before adding or removing a module project. Use on the toolbar or right-click the project, and select Java EE Tools>Add and Remove Project Deployments This menu option was updated in MyEclipse 2017. For prior versions, click here. .
2.1 Removing a Module Project
- Right-click the Test1 project In the Explorer, and select Java EE Tools>Add and Remove Modules This menu option was updated in MyEclipse 2017. For prior versions, click here. .
- Select the Test1Web module project, click Remove, and click OK.
Removing the Test1Web module from Test1
- The Test1Web module association with the Test1 project is removed.
- Test1Web project dependencies on the Test1EJB module project is removed.
Note: The code base of Test1Web project is not altered in any other way.
2.2 Adding a Module Project
- Right-click the Test1 project In the Explorer, and select Java EE Tools>Add and Remove Modules This menu option was updated in MyEclipse 2017. For prior versions, click here. .
- Click Add.
- Select Project, and click Next.
- Select the Test1Web project and click Finish. If the list is empty, then there are no existing projects that can be associated with this EAR project.
Selecting the module to add to the project - Click OK to add the module to the project.
The completion process performs the following actions:
- A project dependency from Test1 to Test1Web is created (see MyEclipse Enterprise Application Projects for more info).
- Java project dependencies from Test1Web to Test1EJB are created.
At this point you can safely deploy or redeploy your EAR project.
3. Installing an Example Java EE Enterprise Application
When creating an EAR project, a majority of the effort is concentrated toward developing the application logic of its web and EJB module projects.
The following example EAR project is a simple stock trading simulation consisting of a Web project, an EJB project, and an Enterprise Project. The web module consists of JSPs through which the user enters buying and selling details for a stock symbol. When the order is executed, the current JSP posts the buy/sell order to a servlet that interacts with a stateless session EJB to complete the transaction.
- Download the following project, traderXEAR(JEE5).zip.
- Select File>Import, expand General, and choose Existing Project into Workspace. Click Next.
- Click the Select archive file option, browse for the sample zip file, and click Finish.
Importing sample projects from archive file
Note: If the Migration wizard appears, click Next and then Finish to migrate the project to your version of MyEclipse.
Imported TraderX projects
4. Deploying an EAR Project
For project deployment, you must use a MyEclipse-supported application server that supports EARs. This tutorial uses TomEE.
Note: You can have greater control over deployed files using MyEclipse’s Deployment Assembly.- Right-click the server in the Servers view, and select Add/ Remove Deployments.
- Select TraderJEE5 in the Available column, and click Add. You can select either packaged or exploded deployment by using the drop-down beside the module in the Configured column, but exploded is recommended for the most efficient development process. Click Finish.
Adding a deployment - Right-click the server, and select Start if the server is not running. As the server is launched, messages are streamed to the Console view. Watch for a message stating that TraderJEE5 was successfully deployed.
5. Testing an EAR Project
- Click .
- Type the URL http://localhost:8080/TraderWeb/html/welcome.html in the address field, and press Enter.
TraderX Welcome page - Click Enter on the Welcome screen.
- In the Stock Trading web form, enter values in the Stock Symbol and Quantity fields, and click Submit.
TraderX stock trading form
TraderX trade confirmation
6. Advanced: Optional Package Support Using Dependent Java Projects
An optional package is one or more stand-alone JAR files that a Java EE module requires for its operation. In many ways, the term “optional package” is a misnomer as a dependent Java EE module does not function correctly without the presence of such Jars. A more appropriate name for such Jars is “required packages”. The Java EE specification allows optional package Jar archives to be included within an EAR archive and referenced through a `Class-Path` entry in the MANIFEST.MF file of any module that requires the services provided by the Jars.
Optional package deployment is more commonly needed for EJB modules that use other Jar packaged services. Unlike the WAR archive that can accommodate packaged libraries, an EJB Jar has no such provision. For example, if an EJB module uses Log4J for its logging service, then the Log4J Jar libraries must be included in the EAR and referenced in the EJB module’s manifest file.
To configure a Java project to serve as an optional package, you must make it a dependent of an enterprise project. In the following example scenario, we use a Test1EJB project that requires logging services that are defined by a Logging Java project. Because of the limited Java EE deployment form for EJB projects, use an enterprise project that includes the Test1EJB project as a module and the Logging Java project as a dependency.
6.1 Configuring Dependent Java Projects To Serve As Optional Packages
- Right-click Test1 in the Explorer, and select Properties.
- Select Deployment>Deployment Assembly
This menu option was updated in MyEclipse 2017. For prior versions, click here.
, and then click Add.
Adding a dependency to Test1 - Select Project, and click Next
Selecting the project directive - Select the dependent project (we are using Logging for this example), and click Finish. Click OK to close the Properties window.
- To include the dependent logging.jar module to the Class-Path segment of the Test1EJB project’s MANIFEST.MF, right-click Test1EJB, and select Properties.
- Select Deployment>Deployment Assembly This menu option was updated in MyEclipse 2017. For prior versions, click here. , and then click the Manifest Entries tab.
- Click Add, select Logging.jar, and click Finish. Click OK to close the Properties window.