October 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Creating Web Services from J2EE Components

  • September 20, 2004
  • By Nandhini Arumugam and Sujata De
  • Send Email »
  • More Articles »

Introduction

Web services are software processes that can be accessed and used from the Internet. Often, a 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 offerred by a Web service can fall into a variety of categories, including:

  1. To execute functions, such as calculating the interest on the balance of a credit card
  2. To retrieve data, such as fetching the balance of the credit card

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 an integration. Because the client application is free to implement the capabilities, it can do so in any language and platform that are compatible. This leads to a dramatic cut in the cost of development, maintanence, and time-to-market.



Click here for a larger image.

Figure 1: A Distributed Application Using Web Services-Based Capabilities

Aim of the Article

Creating Web services from various J2EE components and exposing them to the Net remain one of the critical aspects of a J2EE-compliant e-business. The flexibility and the versatility of the business process depend on this. In this article, we would like to address this issue from a programmer's point of view. We shall concentrate our efforts on a similar 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 Web service, and deploy on Weblogic.

In the following sections, we shall discuss the creation of a Web service for EJB, Servlet, JSP, HTML, and Java class. We shall illustrate each by creating a "Hello World" Web service from each of these components.

Creating a Web service from EJB

Enterprise Java Beans (EJB) is a component architecture for the development and deployment of enterprise level applications. Following are the Java classes that are needed to create 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 them 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 the META-INF directory under examples.

<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC
'-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN'
'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>

<ejb-jar>
   <enterprise-beans>
      <session>
         <ejb-name>Hello</ejb-name>
         <home>examples.HelloHome</home>
         <remote>examples.Hello</remote>
         <ejb-class>examples.HelloBean</ejb-class>
         <session-type>Stateless</session-type>
         <transaction-type>Container</transaction-type>
     </session>
   </enterprise-beans>
</ejb-jar>

ejb-jar.xml

<?xml version="1.0"?>
<!DOCTYPE Weblogic-ejb-jar PUBLIC 
'-//BEA Systems, Inc.//DTD Weblogic 8.1.0 EJB//EN'
'http://www.bea.com/servers/wls810/dtd/Weblogic-ejb-jar.dtd'>
<Weblogic-ejb-jar>
   <Weblogic-enterprise-bean>
      <ejb-name>Hello</ejb-name>
      <jndi-name>ejb20-Hello-HelloHome</jndi-name>
   </Weblogic-enterprise-bean>
</Weblogic-ejb-jar>

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 that 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. The HelloWorld.jar file is now ready for deployment.

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, stating "Review your choices and deploy." The EJB module name that is editable will be displayed. Click Deploy. Wait till the deployment ends and a success message is displayed.

To test 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."





Page 1 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel