facebook

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 Test1WebTest1EJB, Test1Client, and Test1Connector respectively. An EAR Project  must have at least one web project or one EJB project associated with it.

  1. Select File>New>Project, select Enterprise Application Project, and click Next.
  2. Type Test1 into the Project name field.
  3. Accept the default Java EE version and target runtime, and click Next.

    jeenewentapp1
    Creating an Enterprise Application project
  4. Click New Module, and click Finish to create all applicable modules. The modules are included and selected in the dependencies list.

    jeenewentappconfigure
    Selected dependencies

  5. Click Finish to create the EAR project.
Completing the wizard creates and configures five MyEclipse projects, Test1, Test1Client, Test1Connector, Test1EJB, and Test1Web.

jeeexplorer
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

  1. 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. .
  2. Select the Test1Web module project, click Remove, and click OK.

    jeeremovemodule
    Removing the Test1Web module from Test1
The removal performs the following actions:
  • 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

  1. 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. .
  2. Click Add.
  3. Select Project, and click Next.
  4. 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.

    jeeaddprojecttodeploy
    Selecting the module to add to the project
  5. Click OK to add the module to the project.

The completion process performs the following actions:

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.

  1. Download the following project, traderXEAR(JEE5).zip.
  2. Select File>Import, expand General, and choose Existing Project into Workspace. Click Next.
  3. Click the Select archive file option, browse for the sample zip file, and click Finish.

    jeeimportproject
    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.

    jeeexplorertrader
    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.
  1. Right-click the server in the Servers view, and select Add/ Remove Deployments.
  2. 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.

    deploying_project
    Adding a deployment
  3. 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

  1. Click .
  2. Type the URL http://localhost:8080/TraderWeb/html/welcome.html in the address field, and press Enter.

    jeebrowserwelcome
    TraderX Welcome page
  3. Click Enter on the Welcome screen.
  4. In the Stock Trading web form, enter values in the Stock Symbol and Quantity fields, and click Submit.

    jeebrowser
    TraderX stock trading form
When the stock trading form is submitted, the transaction.jsp interacts with the servlet that initiates the transaction with a stateless session EJB named Trader. The Trader EJB is only for demonstration purposes and does not interact with real trading services.

jeeconsoleresults
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

  1. Right-click Test1 in the Explorer, and select Properties.
  2. Select Deployment>Deployment Assembly This menu option was updated in MyEclipse 2017. For prior versions, click here. , and then click Add.

    jeeadddependency
    Adding a dependency to Test1
  3. Select Project, and click Next

    jeeaddoptionalpkg
    Selecting the project directive
  4. Select the dependent project (we are using Logging for this example), and click Finish. Click OK to close the Properties window.
  5. 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.
  6. Select Deployment>Deployment Assembly This menu option was updated in MyEclipse 2017. For prior versions, click here. , and then click the Manifest Entries tab.
  7. Click Add, select Logging.jar, and click Finish. Click OK to close the Properties window.