Download
FAQ History |
API
Search Feedback |
A Web Service Example: HelloServiceBean
This example demonstrates a simple Web service that generates a response based on information received from the client.
HelloServiceBean
is a stateless session bean that implements a single method,sayHello
. This method matches thesayHello
method invoked by the clients described in Static Stub Client. Later in this section, you'll test theHelloServiceBean
by running one of these JAX-RPC clients.Web Service Endpoint Interface
HelloService
is the bean's Web service endpoint interface. It provides the client's view of the Web service, hiding the stateless session bean from the client. A Web service endpoint interface must conform to the rules of a JAX-RPC service definition interface. For a summary of these rules, see Coding the Service Endpoint Interface and Implementation Class. Here is the source code for theHelloService
interface:package helloservice; import java.rmi.RemoteException; import java.rmi.Remote; public interface HelloService extends Remote { public String sayHello(String name) throws RemoteException; }Stateless Session Bean Implementation Class
The
HelloServiceBean
class implements thesayHello
method defined by theHelloService
interface. The interface decouples the implementation class from the type of client access. For example, if you added remote and home interfaces toHelloServiceBean
, the methods of theHelloServiceBean
class could also be accessed by remote clients. No changes to theHelloServiceBean
class would be necessary. The source code for theHelloServiceBean
class follows:package helloservice; import java.rmi.RemoteException; import javax.ejb.SessionBean; import javax.ejb.SessionContext; public class HelloServiceBean implements SessionBean { public String sayHello(String name) { return "Hello "+ name + " from HelloServiceBean"; } public HelloServiceBean() {} public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} public void setSessionContext(SessionContext sc) {} }Building HelloServiceBean
In a terminal window, go to the
<INSTALL>
/j2eetutorial14/examples/ejb/helloservice/
directory. To buildHelloServiceBean
, type the following command:This command performs the following tasks:
The
wscompile
tool writes theMyHelloService.wsdl
file to the<INSTALL>
/j2eetutorial14/examples/ejb/helloservice/build/
subdirectory. For more information about thewscompile
tool, see Chapter 8.Use
deploytool
to package and deploy this example.Creating the Application
In this section, you'll create a J2EE application named
HelloService
, storing it in the fileHelloService.ear
.
- In
deploytool
, select FileNewApplication.- Click Browse.
- In the file chooser, navigate to
<INSTALL>
/j2eetutorial14/examples/ejb/helloservice/
.- In the File Name field, enter
HelloServiceApp
.- Click New Application.
- Click OK.
- Verify that the
HelloServiceApp.ear
file resides in<
INSTALL
>/j2eetutorial14/examples/ejb/helloservice/
.Packaging the Enterprise Bean
Start the Edit Enterprise Bean wizard by selecting FileNewEnterprise Bean. The wizard displays the following dialog boxes.
- Introduction dialog box
- EJB JAR dialog box
- Select the button labeled Create New JAR Module in Application.
- In the combo box below this button, select
HelloService
.- In the JAR Display Name field, enter
HelloServiceJAR
.- Click Edit Contents.
- In the tree under Available Files, locate the
<
INSTALL
>/j2eetutorial14/examples/ejb/helloservice/build/
directory.- In the Available Files tree select the
helloservice
directory andmapping.xml
andMyHelloService.wsdl
.- Click Add.
- Click OK.
- Click Next.
- General dialog box
- In the Configuration Options dialog box, click Next. The wizard will automatically select the Yes button for Expose Bean as Web Service Endpoint.
- In the Choose Service dialog box:
- In the Web Service Endpoint dialog box:
- Click Finish.
- Select FileSave.
Deploying the Enterprise Application
Now that the J2EE application contains the enterprise bean, it is ready for deployment.
Building the Web Service Client
In the next section, to test the Web service implemented by
HelloServiceBean
, you will run the JAX-RPC client described in Chapter 8.To verify that
HelloServiceBean
has been deployed, click on the target application server in the Servers tree indeploytool
. In the Deployed Objects tree you should seeHelloServiceApp
.To build the static stub client, perform these steps:
- In a terminal go to the
<INSTALL>
/j2eetutorial14/examples/jaxrpc/helloservice/
directory and type
asant build
- In a terminal go to the
<INSTALL>
/j2eetutorial14/examples/jaxrpc/staticstub/
directory.- Open
config-wsdl.xml
in a text editor and change the line that reads
<wsdl location="http://localhost:8080/hello-jaxrpc/hello?WSDL"
to
<wsdl location="http://localhost:8080/hello-ejb/hello?WSDL"
- Type
asant build
- Edit the
build.properties
file and change theendpoint.address
property to
http://localhost:8080/hello-ejb/hello
For details about creating the JAX-RPC service and client, see these sections: Creating a Simple Web Service and Client with JAX-RPC and Static Stub Client.
Running the Web Service Client
To run the client, go to the
<INSTALL>
/j2eetutorial14/examples/jaxrpc/staticstub/
directory and enterThe client should display the following line:
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.