Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
EJB
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Web Services

                         

Normally a Service represents any kind of feature or some piece of functionality that a specific kind of client can take it from. For example, a printer service, where the clients are either the applications or the programs using the printers. Likewise, consumers of an ATM service are Bank Customers. These are the list of real-world services goes on in our daily-life.

Web services was first time introduced in EJB2.1, while EJB3.0 made the web services development easy and more flexible. Here we are going to describe the general concept of web services and then explain them how the ejb supports to implement both the web services and web services client.

Web Services Concept: Web services are the mechanism to develop a Service-Oriented-Architecture (SOA). SOA is an architectural approach for designing large scale distributed systems to integrate heterogeneous application on the service interfaces. Web services technologies support to the Service-Oriented-Architecture in various ways. Some of them are illustrated below:

  • A service requestor uses the selection criteria to the query registry for finding the services description.
  • A service requestor can bind and use the service if it finds a suitable descriptor.  

Web services are used in various fields such converting a temperature value from Fahrenheit to Celsius. More realistic examples built using the web services are heterogeneous applications such as billing application and report generator, interconnected in-house architectures. A service interface is just like an object interface with a slight difference that the contract between the interface and the client is more flexible and the implementation of client and the service is not much tightly coupled as compared to EJB or other distributed platform. Looser coupling allows the client and service implementation to run on various platforms, independently such as Microsoft .NET is capable of using a Java EE application server to access a service running on it. From the client's point of view, web services's life cycle is more static as compared to average objects because web services stay around rather than pop-up and go away, even if the services are implemented using the object technology.

Features of Web Services:

Following are the unique features of a Web-Service based application.

  • Language Independent
  • Operating System Independent

Language Independent:

Let us assume that a Web-Service is running in a remote machine. Suppose an application (can be a Web-Service also) want to gain the functionality of the Web-Service by accessing it. It is not that both the Web-Service and the client application must be built in the same language or technology. They can be different. 

Web Services Standards: The de facto standardized set of  web services can be summarized by using an equation.

Web Services = WSDL + SOAP + UDDI 

Lets take a quick look over WSDL and UDDI, but we are not going to cover these topics here because these are less useful. The requestor does not necessarily have the registry to know the services and its end point address. Registry not only supports a simple naming service but also queries for the services that follow the given predicate.

WSDL: There is no need to write down the xml file. The tool you are using automatically creates an xml file. A number of things are worth nothing while using WSDL.

  • The service description includes an endpoint address: WSDL is similar to java interface and an object reference joined together or in other words, we can say web services don't have distinct identities. Since they are not objects therefore they must be viewed like objects. It does not have any client visible state therefore your are not able to compare the two references for equality. 
  • It uses larger number of concepts than in java: Service provides one or more ports at an address. Ports are the representation of the service interfaces that binds to protocols.
  • Operations result in terms of input and output messages rather than parameters and return values: 
  • Services are bind using SOAP binding: 

Building a Web Service with JEE: The creation of the portable and interoperable distributed components from the web services is not a trivial task. Either regular Java classes or stateless EJBs can be easily deployed as web services. Package the Java regular classes in a web module and EJB web services in normal ejb-jar modules.

Two options are there to deploy an application, you can use one of the two deployment options given below:

Java Class versus Stateless EJB: It depends upon you whether you should have a regular Java class or EJBs as your technology to build a Web Service. We can easily develop java classes than EJBs. Java classes are pure java objects and do not have the extra baggage that the EJBs do. One benefit of using EJB is that it supports the features such as declarative transaction and security. EJB leaves free to the developer just to concentrate only on applying the business logic without worrying about the infrastructure services.  

Packaging Requirement: Whatever you are using either a regular Java class or EJB, in both the conditions you need to package several artifacts into your WAR or ejb-jar accordingly, to expose components as a Java Web Service. Web Services use the following two packaging structures based on either regular Java classes or EJBs. 

Web app (.war) for a regular Java web service:

/WEB-INF/
          web.xml
          webservices.xml
          oracle-webservices.xml
          mapping-file.xml
          wsdl/               it is wsdl file
          /classes/(includes endpoint and bean classes)
         /lib/

ejb-jar for an EJB-based web service: 

/META-INF/
             ejb-jar.xml 
             webservices.xml
                         oracle-webservices.xml
             mapping-file.xml
                         wsdl/   the wsdl file
             ejb classes   (includes endpoint and bean classes)

Lets discuss each of the descriptors and the deployment-time artifacts one-by-one.

  • WSDL: We are not going to discuss about the WSDL any more as we have already discuss it in the previous section.
  • Web Services Deployment Descriptor: webservices.xml is the standard deployment descriptor that a JEE plateform requires. This descriptor specifies the description for deployment about the set of web services into the JEE application server and also their dependencies on the container resources and services. The mapping.xml file that contains Java-to-WSDL mapping and the service endpoint interface for the HelloWorld web service is also specified by this deployment descriptor.  
  • Endpoint interface: The Web Service endpoint implements the java.rmi.Remote interface therefore every method of web service endpoint interface must throw the java.rmi.RemoteException. The deployment descriptor registers to the end point for the module (ejb-jar.xml) or web.xml. The deployment descriptor (e.g. ejb-jar.xml) should have the following entry. 
<service-endpoint>
oracle.ejb21.ws.HelloServiceInf
</service-endpoint>

Following is the code for the Web Service endpoint for a HelloWorld web service:

public interface HelloServiceInf extends java.rmi.Remote {
java.lang.String sayHello(java.lang.String name)
throws java.rmi.RemoteException;
}
  • Vendor-specific deployment descriptors: We can not specify several implementation-specific reference, such as endpoint addresses, context root in the Web Services deployment descriptor but we can rather specify the vendor-specific deployment descriptor. For example if you are using OC4J then an oracle-webservices.xml file is required to package in WAR or ejb-jar to define the properties.
  • Java-WSDL mapping: Mapping between the WSDL and Java types is defined in this file. Mapping file does not have any standard name, web services deployment descriptor defines its name.

Before deploying your component as a web service, first you must package all these artifacts in the ejb-jar module or in the WAR. Most of the development tools like Oracle JDeveloper simplifies the development task of web services simply by mapping files, generating the deployment descriptor etc. Moreover most of the application servers provide Web Services assembly tools that fulfill the JEE web service packaging requirements.

Before understanding the components required to make up a Web Service and the associated packaging requirements, first you must deal with the architectural issues when developing a web service. 

Approaches to Construct Services: The main thing about building a Web Service is to identify the service along with its right granularity. Its depends upon you  either you can expose an existing component that is built as a Java class or EJB and expose it as a service or build the new service. You can use either top-down or bottom-up approach while building a new service.

  • Top-down approach: While building the services from scratch, this is the most appropriate approach. With this approach start describing the service with WSDL instead of jumping right into the implementation. This is the most preferable approach since services become maintainable, more usable and interoperable due to the control over the WSDL while deploying your web service, careful consideration of the operations and message exposed. Several JEE vendors provide tools to make the approach easier such as Oracle Application Server's web services assembler generates deployment descriptors, interfaces and skeleton implementation classes which can be used to build your application.
  • Bottom-up approach: This approach is used whenever an existing Java class or EJB is to be exposed as a Web Service. The reason behind the populalrity of this approach is that, this approach allows to reuse the existing business logic without rewriting the applications. While using this approach, you have to create a WSDL to describe the Web Service along with other deployment descriptors and add a web service end-point interface for the implementation to which you want to expose as a Web Service. Tools provided by application servers (such as Oracle Application Server's web services assembler tool) are used to make the life simpler by generating the descriptors such as webservices.xml, WSDL and mapping files for Web Services components free up the developer from manually creating these files.

Tips for developing Web Services: Here are the some points that must follow while developing Web Services.

  • Most of the conventional best practices are for JEE applications that are relevant to Web Services. for example, avoid exposing a component involve in the long-run transaction as a Web Service. 
  • Confirm design of your Web Service so that it can create minimum network traffic.
  • Do not overuse Web Services in your applications. Check the necessity to expose your application as a Web Service.
  • Compare your security requirements with the performance of your application as security comes with the higher cost. Performance of end-to-end security for web services are quite costly.

To build java based web services, J2EE Blueprint Application ( Java Adventure Builder) provides a nice blueprint application.

After designing, developing and deploying, we generally create the associated components to interact with the given service.

Invoking Web Services: Web Service can have the client of any of the following type: Dynamic Invocation Interface (DII) or dynamic proxy, static stub.

Building a Web Service client may be complex similar to build a simple web service. But JEE 1.4 makes it simple to use the web services for JEE developers from any type of JEE component such as EJB components or web clients.

Invoking a Web Service is similar to invoking any other resource using JNDI via the following:

  • First define your component by using a "service-ref" element in the deployment descriptor. For example, if you are accessing the HelloWorldService web service by using web module then the module's web.xml file may contain the following:
    <service-ref> 
    <service-ref-name>service/HelloWorldService</service-ref-name>
    <service-interface>oracle.ws.HelloWorldService</service-interface>
    <wsdl-file>META-INF/HelloWorldService.wsdl</wsdl-file>
    <service-qname>urn:oracle-ws</service-qname>
    </service-ref>
  • Allow your application to find Web Service just by specifying the location of the Web Service in the vendor-specific deployment descriptor. 
    <service-ref-mapping name="service/HelloWorldService">
    <port-info>
    <wsdl-port namespaceURI="urn: HelloWorldService" 
    localpart="HelloWorldServicePort"/>
    <stub-property>
    <name>javax.xml.rpc.service.endpoint.address</name>
    <value>http://localhost:8888/hello/HelloWorldInf</value>
    </stub-property>
    </port-info>
    </service-ref-mapping>

     

  • Package type classes and end-point interface with your application before deploying it to the server. Make the JNDI lookup to use a Web Service.
    InitialContext ctx= new InitialContext();
    HelloServiceInf hs = (HelloServiceInf)
    ctx.lookup("java:comp/env/service/HelloWorldService");
    HelloWorld hello= hs.getHelloWorldServicePort();
    String myhello = hs.sayHello("Zulfiqar") ;

Simplifying SOA Development with JEE 5.0: Building service-oriented applications are rather difficult with JEE, JEE 5.0 is designed to simplify the development by using Web Services Metadata annotations defined by JSR 181. Web Services Metadata and EJB 3.0 both are used to provide friendly environment to the developer. 

To develop a simple Java Web Service in JEE 1.4, several Web Service artifacts such as mapping files, WSDL, proprietary web services deployment descriptor and several verbose standard are designed in JEE 5.0. Web Services Metadata specification receives a by default configuration approach similar to EJB 3.0 for simplifying the development process. Web Services Metadata annotation process generates these files for you so just concentrate on the implementation class.

Following is the Java Web Service developed using Web Services Metadata:

package oracle.jr181.demo; 
import javax.jws.WebMethod; 
import javax.jws.WebService; 
@WebService(name = "HelloWorldService", 
targetNamespace = "http://hello/targetNamespace" ) 
public class HelloWorldService { 
@WebMethod public String sayhello(String name ) { 
return "Hello” +name+ “ from jws";

}

EJB 3.0 uses regular Java classes to simplify the development process. EJB-based Web Services developed using EJB 3.0 and Web Services Metadata, are much simpler. HelloWorld EJB is the example given below developed by using EJB 3.0 and Web Services Metadata. Don't worry while creating WSDL, deployment descriptors, etc., since the application server generates these artifacts during deployment.

package oracle.ejb30.ws;
import javax.ejb.Remote;
import javax.jws.WebService;
@WebService 
public interface HelloServiceInf extends java.rmi.Remote{
@WebMethod java.lang.String sayHello(java.lang.String name) 
throws java.rmi.RemoteException;
}

Implementation of HelloWorld EJB using EJB 3.0 is given below:

package oracle.ejb30.ws;
import java.rmi.RemoteException;
import javax.ejb.Stateless;
@Stateless(name="HelloServiceEJB")
public class HelloServiceBean implements HelloServiceInf {
public String sayHello(String name) {
return("Hello "+name +" from first EJB3.0 Web Service");
}
}

The above example demonstrates the simplification of service development while simplifying by using Web Services Metadata and EJB 3.0.

Conclusion

This articles gives you the understanding about the basics of building Web Services using the J2EE platform. You can build and deploy your Web Services in J2EE-compliant application servers such as Sun Java System Application Sever, Oracle Application Server 10g, etc.

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (post your own) View All Comments Latest 10 Comments:

There is only the top-down approach (contract first) to build SOA based applications. The WSDL is designed from a business point of view and is not driven by a service consumer or an existing application. The "WSDL to Java" tool generates java code according to JAX-RPC or JAX-WS. These classes can then adapt the existing business logic.


The bottom up approach does generate web services which design is driven from an application point of view (developer driven) and will not address the need for other consumers.

Posted by Chakradhar on Monday, 06.9.08 @ 04:43am | #62662

I want deploy to spring framework version 2.5.1 using IDE is oracle JDeveloper version 10.1.2, and using the server is Oracle Application Server.

Please let me know how can deploy spring framework in oracle Jdeveloper and give sime example codeing. Thanks

Regards
Rani

Posted by rani on Thursday, 02.14.08 @ 07:57am | #48215

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2007. All rights reserved.