You are on page 1of 13

Learning Material J2EE Web Services

Telecom : BNP_eBPP SIVAKUMAR.R sivakumar.r@tcs.com

TCS Public

Web Services

1. Web Services Introduction 2. Service Oriented Architecture (SOA)


2.1 Services 2.2 Connections

3. Web Service Standards


3.1 UDDI 3.2 SOAP 3.3 WSDL 3.4 ebXML

4. Implementing Web Services

5. Building the Web Service


5.1 Implementation Class & Client class 5.2 Client Class

6. Conclusion

Internal Use

Web Services

1. Web Services Introduction


Web services have promised genuine interoperability by transmitting XML data using platform and language-independent protocols such as SOAP over HTTP. Web services have matured and standardized enough to have been incorporated into the J2EE. J2EE web services provides for two types of endpoints. If you think of a web service as a platform-independent invocation layer, then the endpoint is the object you are exposing the operations of and invoking operations on. Naturally, J2EE web services support exposing EJBs as web services, but only stateless session beans can be used. That makes sense given the stateless nature of web services requests. Additionally, J2EE web services provide for JAX-RPC service endpoints, (JSEs) which are nothing more than simple Java classes. Here we use the EJB end points. Web services constitute a distributed computer architecture made up of many different computers trying to communicate over the network to form one system. They consist of a set of standards that allow developers to implement distributed applications - using radically different tools provided by many different vendors - to create applications that use a combination of software modules called from systems in disparate departments or from other companies. A Web service contains some number of classes, interfaces, enumerations and structures that provide black box functionality to remote clients. Web services typically define business objects that execute a unit of work (e.g., perform a calculation, read a data source, etc.) for the consumer and wait for the next request. Web service consumer does not necessarily need to be a browser-based client. Console-based and Windows Forms-based clients can consume a Web service. In each case, the client indirectly interacts with the Web service through an intervening proxy. The proxy looks and feels like the real remote type and exposes the same set of methods. Under the hood, the proxy code really forwards the request to the Web service using standard HTTP or optionally SOAP messages.

Internal Use

Web Services

Web Services Architecture

Internal Use

Web Services

2.Service Oriented Architecture


Before having a look on the web services, the SOA (Service Oriented Architecture) plays a vital role in the web services. Service Oriented Architecture (SOA) for short is a new architecture for the development of loosely coupled distributed applications. In fact service-oriented architecture is collection of many services in the network. These services communicate with each other and the communications involves data exchange & even service coordination. Earlier SOA was based on the DCOM or Object Request Brokers. Nowadays SOA is based on the Web Services. SOA is classified into two terms. 1. Services 2. Connections 2.1Services A service is a function or some processing logic or business processing that is welldefined, self-contained, and does not depend on the context or state of other services. 2.2 Connections: Connections means the link connecting these self-contained distributed services with each other, it enable client to Services communications. In case of Web services SOAP over HTTP is used to communicate the between services.

3. Web Service Standards


Web services are registered and announced using the following services and protocols. Many of these and other standards are being worked out by the UDDI project, a group of industry leaders that is spearheading the early creation and design efforts. 3.1 UDDI Universal Description, Discovery, and Integration (UDDI) is a protocol for describing available Web services components. This standard allows businesses to register with an Internet directory that will help them advertise their services, so companies can find one another and conduct transactions over the Web. This registration and lookup task is done using XML and HTTP(S)-based mechanisms.

Internal Use

Web Services

3.2 Simple Object Protocol Simple Object Access Protocol (SOAP) is a protocol for initiating conversations with a UDDI Service. SOAP makes object access simple by allowing applications to invoke object methods or functions, residing on remote servers. A SOAP application creates a request block in XML, supplying the data needed by the remote method as well as the location of the remote object itself. 3.3 WSDL Web Service Description Language (WSDL), the proposed standard for how a Web service is described, is an XML-based service IDL (Interface Definition Language) that defines the service interface and its implementation characteristics. WSDL is referenced by UDDI entries and describes the SOAP messages that define a particular Web service. 3.4 E-Business XML ebXML (e-business XML) defines core components, business processes, registry and repository, messaging services, trading partner agreements, and security.

4. Implementing Web Services


Brief step-by-step on how a Web service is implemented.

A service provider creates a Web service The service provider uses WSDL to describe the service to a UDDI registry The service provider registers the service in a UDDI registry and/or ebXML registry/repository.

Another service or consumer locates and requests the registered service by querying UDDI and/or ebXML registries.

The requesting service or user writes an application to bind the registered service using SOAP in the case of UDDI and/or ebXML

Internal Use

Web Services

5. Building the Web Service


With the initial setup done, it's time to build a web service. In this example, the web service is developed from a Java class.

Write an endpoint implementation class. Compile the endpoint implementation class. Optionally generate portable artifacts required for web service execution. Package the web service as a WAR files and deploy it.

5.1 Implementation class & Client Class package endpoint;

import javax.jws.WebService; import javax.jws.WebMethod;

@WebService( name="Calculator", serviceName="CalculatorService", targetNamespace="http://techtip.com/jaxws/sample" ) public class Calculator { public Calculator() {}

@WebMethod(operationName="add", action="urn:Add") public int add(int i, int j) { int k = i +j ; System.out.println(i + "+" + j +" = " + k);

return k;

Internal Use

Web Services

} }

Client Side Class package client; import javax.xml.ws.WebServiceRef; import com.techtip.jaxws.sample.CalculatorService; import com.techtip.jaxws.sample.Calculator;

public class JAXWSClient { @WebServiceRef(wsdlLocation= "http://localhost:8080/jaxws-webservice/CalculatorService?WSDL")

static CalculatorService service;

public static void main(String[] args) { try { JAXWSClient client = new JAXWSClient(); client.doTest(args); } catch(Exception e) { e.printStackTrace(); } }

public void doTest(String[] args) { try { System.out.println( " Retrieving port from the service " + service);

Internal Use

Web Services

Calculator port = service.getCalculatorPort(); System.out.println( " Invoking add operation on the calculator port"); for (int i=0;i>10;i++) { int ret = port.add(i, 10); if(ret != (i + 10)) { System.out.println("Unexpected greeting " + ret); return; } System.out.println( " Adding : " + i + " + 10 = " + ret); } } catch(Exception e) { e.printStackTrace(); } }

Output :
runtest-jaxws: Executing appclient with client class as client.JAXWSClient ] Retrieving port from the service com.techtip.jaxws.sample.CalculatorService@162522b Invoking add operation on the calculator port Adding : 0 + 10 = 10 Adding : 1 + 10 = 11 Adding : 2 + 10 = 12 Adding : 3 + 10 = 13 Adding : 4 + 10 = 14 Adding: 5 + 10 = 15 Adding : 6 + 10 = 16

Internal Use

Web Services

Adding: 7 + 10 = 17 Adding : 8 + 10 = 18 [exec] Adding : 9 + 10 = 19

5.2 Example II Client Side Class

import import import import

javax.xml.soap.*; java.util.Iterator; java.net.URL; java.io.*;

public class Client { public static void main(String [] args) { try { SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = soapConnectionFactory.createConnection(); SOAPFactory soapFactory = SOAPFactory.newInstance(); MessageFactory factory = MessageFactory.newInstance(); SOAPMessage message = factory.createMessage(); SOAPBody body = message.getSOAPBody(); Name bodyName = soapFactory.createName("getActiveStations", "", "urn:ActiveStations"); body.addBodyElement(bodyName); System.out.print("\nPrinting the message that is being sent: \n\n"); message.writeTo(System.out); System.out.println("\n\n"); URL endpoint = new URL ("http://opendap.coops.nos.noaa.gov/axis/services/ActiveStations"); SOAPMessage response = connection.call(message, endpoint); connection.close(); System.out.println("\n\nIterating through the response object to get the values:\n\n"); SOAPBody responseBody = response.getSOAPBody(); //Checking for errors if (responseBody.hasFault()) { SOAPFault newFault = responseBody.getFault(); System.out.println("SAOP FAULT:\n"); System.out.println("code = " + newFault.getFaultCodeAsName());

Internal Use

10

Web Services

System.out.println("message = " + newFault.getFaultString()); System.out.println("actor = " + newFault.getFaultActor()); }else{ Iterator iterator = responseBody.getChildElements(); Iterator iterator2, iterator3, iterator4, iterator5; String tagName = null; SOAPElement se = null; Name attributeName = null; if (iterator.hasNext()){ se = (SOAPElement)iterator.next();//Stations Tag iterator2 = se.getChildElements(); se = (SOAPElement)iterator2.next();//Station Tag iterator2 = se.getChildElements(); while (iterator2.hasNext()){ se = (SOAPElement)iterator2.next(); tagName = se.getElementName().getLocalName(); System.out.println("\n\n********************************************* *********"); System.out.println("Station Name\t ID\t\t Latitude\t Longitude\t State\t Date"); if(tagName!=null && tagName.equals("station")){ //attributes for a station attributeName = soapFactory.createName("name"); System.out.print(se.getAttributeValue(attributeName));//Station Name attributeName = soapFactory.createName("ID"); System.out.print("\t" + se.getAttributeValue(attributeName));//Station ID iterator3 = se.getChildElements(); se = (SOAPElement)iterator3.next();//Metadata Tag iterator4 = se.getChildElements(); se = (SOAPElement)iterator4.next();//Location Tag iterator5 = se.getChildElements(); while (iterator5.hasNext()){ se = (SOAPElement)iterator5.next(); tagName = se.getElementName().getLocalName(); if(tagName!=null){ //The name of the tag in this case is 'lat' if(tagName.equals("lat")){ System.out.print("\t\t" + se.getValue());//Latitude }else if(tagName.equals("long")){ System.out.print("\t" + se.getValue()); //longitude

Internal Use

11

Web Services

} else if(tagName.equals("state")){ System.out.print("\t" + se.getValue()); //State } } } se = (SOAPElement)iterator4.next();//Date tagName = se.getElementName().getLocalName(); if(tagName!=null){ System.out.println("\t" + se.getValue()); } System.out.println("Parameters\t DCP\t SensorId\t Status"); while (iterator3.hasNext()){ se = (SOAPElement)iterator3.next(); attributeName = soapFactory.createName("name"); System.out.print(se.getAttributeValue(attributeName)); attributeName = soapFactory.createName("DCP"); System.out.print("\t" + se.getAttributeValue(attributeName)); attributeName = soapFactory.createName("sensorID"); System.out.print("\t" + se.getAttributeValue(attributeName)); attributeName = soapFactory.createName("status"); System.out.println("\t" + se.getAttributeValue(attributeName)); } } } } } } catch (SOAPException e) { System.err.println("ERROR: ******* " + e.toString()); } catch (IOException io) { System.err.println("ERROR: ******* " + io.toString()); } } }

Output
/**************************************************** SAMPLE RUN >java Client Sample Output (The entire output is omitted for it is too long) ****************************************************** Station Name ID Latitude Longitude State Date Nikiski 9455760 60 41.0 N 151 23.9 W 1971-05-21 Parameters Winds DCP 1 SensorId C1 Status 1

AK

Internal Use

12

Web Services

Air Temp Water Temp Air Pressure Water Level

1 1 1 1

D1 E1 F1 NT

1 0 1 1

****************************************************** Station Name ID Latitude Longitude State Date Anchorage 9455920 61 14.3 N 149 53.4 W AK 1964-04-24 Parameters Winds Air Temp Water Temp Air Pressure Water Level DCP 1 1 1 1 1 SensorId C1 D1 E1 F1 NT Status 1 1 1 1 1

********************************************************/

Conclusion
Web Services Plays a vital role in the distributed web application by making secure machine to machine interaction .It makes more flexible and portable for application users.

************************ End Of Learning ***************************

Internal Use

13

You might also like