DocsEntAppClientGF3

This document is a draft. The current version of this document is Creating and Running an Application Client on the GlassFish Server

This document demonstrates how to create and run an Application Client that accesses an EJB that is deployed to GlassFish Server 3

Contents


Requirements for This tutorial

You need the following software:

  • NetBeans IDE 6.9

Prerequisites

This document assumes you have some basic knowledge of, or programming experience with, the following technologies:

  • Java Programming
  • NetBeans IDE


Creating the Java Class Library

  1. Choose File > New Project and select Java Class Library in the Java category. Click Next.
  2. Type EJBRemoteInterface for the Project Name. Click Finish.

The IDE creates a Java Class Library project. You will create the remote interface for your session bean in this project and then add this as a library to the enterprise application containing the application client.

Creating the EJB

  1. Choose File > New Project and select Enterprise Application in the Java EE category. Click Next.
  2. Type EntAppEJB for the Project Name. Click Next.
  3. Select GlassFish Server 3 for the Server.
  4. Deselect Create Web Application Module. Click Finish.

The IDE creates an enterprise application and an EJB module.

  1. Right-click the EJB module project and choose New > Session Bean.
  2. Type MySession for the EJB Name.
  3. Type ejb for the Package.
  4. Select Stateless for the Session Type.
  5. Select Remote option for Create Interface.
  6. Select EJBRemoteInterface from the dropdown list. Click Finish.

When you click Finish the IDE creates the session bean in the ejb package in the EJB modules. The IDE also creates a remote interface named MySessionRemote in the ejb package in the EJBRemoteInterface project. The IDE automatically add the EJBRemoteInterface as a library of the EJB module. The IDE also adds the Java EE 6 API Library as a library of the EJBRemoteInterface project.

  1. Right-click in the editor of MySession and choose Add Business Method.
  2. Type getResult for the Method Name and String for the Return Type. Click OK.
  3. Modify the return statement.

The class should look like the following.


@Stateless
public class MySession implements MySessionRemote {

    public String getResult() {
        return "This is My Session Bean";
    }
}


Creating the Application Client

In this section you will create the enterprise application project that contains the application project. The Java class library that contains the remote interfaces is added as a library of the application client. When you run the enterprise application, the IDE will package the application client and the Java class library JAR in the EAR archive and the remote interfaces in the Java class library will be available to the application client. Library JARs must be packaged in an EAR with the application client if you want to access the JARs from the application client.

  1. Choose File > New Project and select Enterprise Application in the Java EE category. Click Next.
  2. Type EntAppClient for the Project Name. Click Next.
  3. Select GlassFish Server 3 for the Server.
  4. Deselect Create EJB Module and Create Web Application Module.
  5. Select Create Application Client. Click Finish.

The IDE creates an enterprise application and an application client project.

  1. Expand the Source Packages node of the EntAppClient project and open Main.java in the editor.
  2. Right-click in the source code and choose Call Enterprise Bean to open the Call Enterprise Bean dialog.
  3. Expand the EntAppEJB project and select MySessionBean. Click OK. The dialog automatically selects Remote as the interface type.

When you click OK, the IDE adds the following annotation to Main.java.

@EJB
private static MySessionRemote mySession;

The IDE also automatically adds EJBRemoteInterface as a project Library.

  1. Modify the main method to retrieve the String of the getResult method via the MySessionRemote interface.
public static void main(String[] args) {
        System.err.println("result = " + mySession.getResult());
    }

Running the Projects

  1. Right-click the EntAppEJB enterprise application project and choose Run.
  2. Right-click the EntAppClient enterprise application project and choose Run.

Italic text

Not logged in. Log in, Register

By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo