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 using MyEclipse. You will learn how to:

  • Create an enterprise application (EAR) project
  • Add and remove modules
  • Package, deploy, and test an EAR project

Duration Time: 30 Minutes

Start Tutorial Now

Don't have MyEclipse? Download Now

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.

    Note: This tutorial uses the default JavaEE 6 version; however, JavaEE 7 is an available option when creating new projects.
    Java EE enterprise application project
    Creating an Enterprise Application project

    Project Name - Must be a valid Eclipse project name.

    Project Location - Enable checkbox to use the default file-system location for the new project and its resources. The default location is in the workspace directory for which MyEclipse was started.

    Location Directory - If the Use Default Location checkbox is deselected, you can choose an alternative file location outside of the workspace directory. 

    Note: You cannot choose an alternate directory within the workspace directory, as this is not allowed by Eclipse.

    Java EE Version - Specifies the Java EE specification compliance level. Please ensure that you select the proper specification level for the Java EE features your application requires and that your target deployment application server(s) support this specification level. Check with your application server vendor for information regarding Java EE specification level compliance.

    Add Maven Support - Enabling this option includes Maven project management support to your project. See Using Maven in MyEclipse for more information.

    Target Runtime - A runtime is selected by default based on your chosen Java EE version. A runtime must be selected for various containers (Spring, JPA, Struts, etc.) to appear on your project’s build path. You can change the runtime on the Runtime tab of the Project Properties>Project Facets page.

  4. Click New Module, and click Finish to create all applicable modules. The modules are included and selected in the dependencies list.
    Java EE enterprise application project modules
    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.

    Java EE enterprise application structure
    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. Therefore, if your EAR project is deployed, you should always undeploy it before adding or removing a module project. Use  on the toolbar or right-click the project, and select MyEclipse>Add and Remove Project Deployments.


    2.1 Removing a Module Project

    1. Right-click the Test1 project In the Explorer, and select MyEclipse>Add and Remove Modules.
    2. Select the Test1Web module project, click Remove, and click OK.
      Java EE enterprise application modules
      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 MyEclipse>Add and Remove Modules.
    2. Click Add.
      Java EE enterprise application modules
      Adding a module project to Test1
    3. Select Project, and click Next.
    4. Select the Test1Web project. If the list is empty, then there are no existing projects that can be associated with this EAR project.
    5. Click Finish, and click OK on the Module Projects window.
      Java EE enterprise application assembly
      Selecting the module to add 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. See the related topics section for related information.

    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.
      Java EE enterprise application project import
      Importing sample projects from archive fileJava EE enterprise application structure
      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 JBoss. 

    Note: You can have greater control over deployed files using MyEclipse's Deployment Assembly.

    1. Right-click the JBoss 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.
      Java EE enterprise application deployment
      Adding a deployment
    3. Right-click the server, and select Start if the server is not running. As JBoss is launched, messages are streamed to the Console view. Watch for a message stating that TraderJEE5 was successfully deployed. 
      Java EE enterprise application deployment
      JBoss output messages during startup

    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.
      Java EE enterprise application
      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.
      Java EE enterprise application testing
      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.

    Java EE enterprise application testing
    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 an example scenario, a Test1EJB project 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. Expand MyEclipse, select Deployment Assembly, and click Add.
      Java EE enterprise application deployment assembly
      Adding a dependency to Test1
    3. Select Project, and click Next.
      Java EE enterprise application assembly
      Selecting the project directive
    4. Select the Logging project, 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. Expand MyEclipse, select Deployment Assembly, and click the Manifest Entries tab.
    7. Click Add, select Logging.jar, and click Finish. Click OK to close the Properties window.
      Java EE enterprise application deployment assembly
      Test1EJB MANIFEST.MF