The JavaTM Web Services Tutorial
Home
TOC
Index
PREV TOP NEXT
Divider

JAX-RPC on the J2EE SDK 1.3.1

In the example of this section, a stand-alone JAX-RPC client makes a remote call on a JAX-RPC service that is deployed as a servlet on the J2EE SDK. The servlet locates a stateless session bean and then invokes a method on the bean.


Note: The instructions in this section apply only to version 1.3.1 of the J2EE SDK.

Prerequisites

This section is for advanced users who are familiar with the following:

To learn about EJB and J2EE technologies, see the J2EE Tutorial:

http://java.sun.com/j2ee/tutorial/index.html
 

To run this example, you'll need to download and install the J2EE SDK, which is available at the following URL:

http://java.sun.com/j2ee/sdk_1.3/index.html
 

Note: On the page of the preceding URL, be sure to read the section, Supported Operating Systems and Required Software. The J2EE SDK 1.3.1 does not support Windows 95, 98, ME, or XP.

Example Code

The example files are located in the <JWSDP_HOME>/docs/tutorial/examples/jaxrpc/toejb directory. The greeting subdirectory contains the source code for the stateless session bean named GreetingEJB. You don't have to compile or package the bean, because it's already packaged in a J2EE application archive named GreetingApp.ear. This EAR file is in the provided-jars subdirectory.

At runtime, a JAX-RPC client named HelloClient makes a remote call to the sayHello method of the JAX-RPC Web service:

System.out.println(stub.sayHello("Buzz!"));
 

Next, the sayHello method of the HelloImpl class invokes the sayHey method of GreetingEJB:

public String sayHello(String name) {

        String result = null;

        try {
            Context initial = new InitialContext();
            Context myEnv =
               (Context)initial.lookup("java:comp/env");
            Object objref = myEnv.lookup("ejb/SimpleGreeting");
 
            GreetingHome home = 
                (GreetingHome)PortableRemoteObject.narrow
                (objref, GreetingHome.class);
 
            Greeting salutation = home.create();
            result = salutation.sayHey(name);
 
        } catch (Exception ex) {
            System.out.println("Exception in sayHello: " 
                + ex.getMessage());
        }

        return result;

}
 

Here is the sayHey method of the GreetingBean class of the GreetingEJB stateless session bean:

public String sayHey(String name) {
       return "Hey " + name + "!";
}
 

Packaging the JAX-RPC Client and Web Service

  1. If your PATH environment variable includes <J2EE_HOME>/bin, change the PATH so that <JWSDP_HOME>/bin precedes <J2EE_HOME>/bin.
  2. In a terminal window, go to the <JWSDP_HOME>/docs/tutorial/examples/jaxrpc/toejb directory.
  3. In a text editor, open the build.xml file and set the value of j2ee.home to the location of your J2EE SDK installation.
  4. Type the following commands:
      ant build
      ant build-static
     
    

The preceding commands will create the toejb-jaxrpc.war and toejb-client.jar files in the dist subdirectory.

Setting Up the J2EE SDK 1.3.1

  1. If Tomcat is running, shut it down.
  2. If the j2ee server is running, stop it.
  3. Set the PATH environment variable so that <J2EE_HOME>/bin precedes <JWSDP_HOME>/bin.

Note: In all subsequent steps, <J2EE_HOME>/bin must precede <JWSDP_HOME>/bin in the PATH environment variable.

  1. In a terminal window, run the following command:

UNIX:

  <JWSDP_HOME>/bin/jwsdponj2ee.sh $J2EE_HOME
 

Windows:

  <JWSDP_HOME>\bin\jwsdponj2ee.bat %J2EE_HOME%
 

The jwsdponj2ee script adds Java WSDP libraries to the J2EE SDK and then changes the Web server port of the J2EE SDK from 8000 to 8080. After you've finished running this example, you may want to follow the instructions in Undoing the Effects of jwsdponj2ee.

  1. In a terminal window, start the j2ee server:
      j2ee -verbose
     
    
  2. Both the Java WSDP and the J2EE SDK have utilities called deploytool. In the steps that follow, you must run the J2EE SDK's deploytool. To make sure that your PATH points to the J2EE SDK's deploytool, type this command:
      deploytool -version
     
    

The tool should display the following line:

  The deployment tool version is 1.3.1
 
  1. Run the J2EE SDK's deploytool:
      deploytool
     
    

Deploying the GreetingEJB Session Bean

  1. In the deploytool utility, open the GreetingApp.ear file, which is located in the directory named <JWSDP_HOME>/docs/tutorial/examples/jaxrpc/toejb/provided-jars.
  2. In the tree, expand GreetingApp. Note that it contains an enterprise bean named GreetingEJB and a J2EE application client named GreetingClient. This client was created to test the bean and will not be run in this example.
  3. Deploy the GreetingApp application.

Deploying the JAX-RPC Service

  1. In deploytool, create a new application named HelloApp.
  2. Add the toejb-jaxrpc.war file to HelloApp. This WAR file is in the directory named <JWSDP_HOME>/docs/tutorial/examples/jaxrpc/toejb/dist.
  3. Specify the reference to GreetingEJB.
    1. In the tree, select HelloWorldApplication.
    2. On the EJB Refs tab, add an entry with the values shown in the following table.

      Table 9-4 EJB Refs Tab of the HelloWorldApplication
      Field
      Value
      Coded Name
      ejb/SimpleGreeting
      Type
      Session
      Interfaces
      Remote
      Home Interface
      toejb.greeting.GreetingHome
      Local/Remote Interface
      toejb.greeting.Greeting
  4. Specify the JNDI name of GreetingEJB.
    1. In the tree, select HelloApp.
    2. On the JNDI Names tab, enter MyGreeting in the JNDI Name field at the bottom.
  5. Set the context root for the servlet.
    1. In the tree, select HelloApp.
    2. On the Web Context tab, enter toejb-jaxrpc in the Context-Root field.
  6. Deploy the HelloApp application.

If you have problems deploying the application, you may find it helpful to compare the HelloApp.ear file you created with the CompareHelloApp.ear file in the provided-jars subdirectory. The HelloWorldApplication in the CompareHelloApp.ear file has the correct settings and may be deployed as is.

Running the JAX-RPC Client

  1. In a terminal window, go to the directory named <JWSDP_HOME>/docs/tutorial/examples/jaxrpc/toejb.
  2. Type the following command:
      ant run
     
    

The client should display the following line:

  Hey Buzz!!
 

Undoing the Effects of jwsdponj2ee

In the section, Setting Up the J2EE SDK 1.3.1, you ran the jwsdponj2ee script, which made some changes to the J2EE SDK installation. To undo these changes, perform the following:

  1. Stop the j2ee server.
  2. In a text editor, open the <J2EE_HOME>/config/web.properties file and change the value of the http.port property from 8080 to 8000.
  3. In a text editor, open the userconfig script of the <J2EE_HOME>/bin directory and comment out the statement that sets the J2EE_HOME variable.
Divider
Home
TOC
Index
PREV TOP NEXT
Divider

This tutorial contains information on the 1.0 version of the Java Web Services Developer Pack.

All of the material in The Java Web Services Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.