Rajesh Devadas, Nandhini Arumugam
& Sujata De
Â
Introduction
Creating Web services from different kind of SW components and exposing them to the net remain one of the critical aspects of e-business, in this series we shall discuss the creation of Web service for EJB, Servlet, JSP, HTML and Java class.
What is a Web service?
As the name suggests, Web services are software processes, which can be accessed from the Internet. Often, software is exposed to the Web as a Web service because it needs to be available, reused and shared by users from across the world. Web services represent a new architectural paradigm for software applications. Web services expose capabilities that are available to universal user, applications and other Web services alike, via industry standard interfaces and protocols. The capabilities offered by a Web service can fall into a variety of categories, including:
Think of a business process as a conglomeration of several such capabilities. It can be inappropriate to integrate some of these capabilities within third-party applications. When these capabilities are exposed as Web services, they can be loosely coupled and dynamically discovered, thereby achieving the benefits of integration. Instead of tying all capabilities within a single application, Web services make their capabilities publicly available over the net where a client application can simply invoke them by the URL address, and compose integration. Since the client application is free to implement the capabilities, it can do so in any language and platform, which are compatible. This leads to a dramatic cut in the cost of development, maintenance and time-to-market.
Figure 1. A Distributed Application using Web services based Capabilities
Â
Creating Web services from all kinds of software components and exposing them to the net remain one of the critical aspects of e-business. The flexibility and the versatility of the business process depend on this. In this paper, we would like to address this issue from a programmer's point of view. We shall concentrate our efforts on a J2EE compliant architecture with BEA Weblogic as the application server. We shall take some of the basic building blocks of a J2EE based system, convert each to an individual Webservice and deploy on Weblogic.
We shall illustrate each with creating a Hello World Web service from each of these components.
Creating a Web service from EJB
Enterprise Java Beans or EJB is a component architecture for the development and deployment of enterprise level applications. Following are the Java classes that are needed for creating a Hello World Web service from an EJB.
package examples ;
public class HelloBean implements javax.ejb.SessionBean
{
......
public String hello()
{
System.out.println("hello()");
return "Hello, World";
}
}
Â
HelloBean.java
package examples;
public interface HelloHome extends javax.ejb.EJBHome
{
Hello create() throws java.rmi.RemoteException, javax.ejb.CreateException;
}
Â
HelloHome.java
package examples;
public interface Hello extends javax.ejb.EJBObject
{
public String hello() throws java.rmi.RemoteException;
}
Hello.java
Copy all these files to a directory called examples. Include in CLASSPATH and compile. The deployment descriptors that are necessary for deploying EJB Hello, ejb-jar.xml and Weblogic-ejb-jar.xml, are given below. These files should be available under META-INF directory under examples.
'-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN'
'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
ejb-jar.xml
Â
'-//BEA Systems, Inc.//DTD Weblogic 8.1.0 EJB//EN'
'http://www.bea.com/servers/wls810/dtd/Weblogic-ejb-jar.dtd'>
Weblogic-ejb-jar.xml
Once the class files and deployment descriptors are ready, a JAR file is to be created using the following command from examples.
>jar —cf HelloWorld.jar *
This command will create a HelloWorld.jar file which contains the following structure
META-INF/MANIFEST.MF
META-INF/ejb-jar.xml
META-INF/Weblogic-ejb-jar.xml
examples/Hello.class
examples/HelloHome.class
examples/HelloBean.class
MANIFEST.MF
will be generated automatically while running the jar command. HelloWorld.jar file is ready for deployment now.Start the Weblogic Server. In the administration console, on the left hand side panel, just select the EJB Modules under Deployments. Select Deploy a new EJB Module option on the Right hand side. In the directory tree structure displayed, select the directory in which HelloWorld.jar exists. Click the target module button. A screen will be displayed with "Review your choices and deploy". The EJB module name will be displayed which is editable. Click Deploy. Wait till the deployment ends and success message gets displayed.
For testing the EJB Module that is deployed, just click the Testing tab. If the test is successful, a message will be displayed: "The EJB Hello has been tested successfully with a JNDI name of ejb20-Hello-HelloHome ".
Â
**Watch out for the next tutorial, which will cover creating a Web service from a Servlet**
The authors are working as Software Engineers with Hewlett-Packard India Software Operations Ltd., in Bangalore and have expertise in the areas of Web services and other J2EE technologies.
Â
Â