Web Services Part 2

70
NIYAZ AHMAD RAO 1 Create a simple web-services (say hello) Choose web-project named it HelloWebService Choose java class say “Hello” within “mypackage” package package mypackage; import javax.jws.WebService; @WebService() // @WebService() annotation to the Hello.java will expose all public methods of the Hello class to be exposed as Web service operations. public class Hello { public Hello() { } // Business method we want to expose as Web service operation public String sayHello(String name){ return "Hello " + name + "!"; } }

Transcript of Web Services Part 2

Page 1: Web Services Part 2

NIYAZ AHMAD RAO 1

Create a simple web-services(say hello)

• Choose web-project named it HelloWebService• Choose java class say “Hello” within “mypackage” package

• package mypackage;• import javax.jws.WebService;• @WebService() // @WebService() annotation to the Hello.java will expose

all public methods of the Hello class to be exposed as Web service operations.public class Hello {

• public Hello() { } • // Business method we want to expose as Web service operation• public String sayHello(String name){• return "Hello " + name + "!";• }• }•

Page 2: Web Services Part 2

NIYAZ AHMAD RAO 2

• Right click on the project and choose deploy for deploy the project.

• Test the Web service : Expand Web services and right click Hello Web service node and select Test Web Service

• Observe HelloService Web Service Tester page is displayed.

• Type in your name, Niyaz Rao in this example. Click sayHello button

• Observe that a new page is displayed with the result of the testing

Page 3: Web Services Part 2

NIYAZ AHMAD RAO 3

• Observe that the SOAP message request and response are also displayed

• Click backward button of the browser to go the previous page

• Click WSDL File link .observer it

• Observe the URL of the WSDL document is http://localhost:8080/HelloWebService/HelloService?WSDL. 

• We will use this URL later on when you create the corresponding Web service client.

Page 4: Web Services Part 2

NIYAZ AHMAD RAO 4

Creating a client for consuming web-service

• Choose java project called “HelloWebServiceClient”

• Right click HelloWebServiceClient and select Web Services and under File Types,select WebService Client.

• Observe WSDL and Client Location pane gets displayed • Select WSDL URL and specify the location of the WSDL

http://localhost:8080/HelloWebService/HelloService?WSDL

For the Package field, type in mypackage.

We can also select Project option (instead of WSDL URL option)

Page 5: Web Services Part 2

NIYAZ AHMAD RAO 5

package hellowebserviceclient;public class Main {    public static void main(String[] args) {        try { // Call Web Service Operation

mypackage.HelloService service = new mypackage.HelloService(); mypackage.Hello port = service.getHelloPort();  java.lang.String result = port.sayHello(“niyaz rao”); System.out.println("Result = "+result);        } catch (Exception ex) {        }               } }

• OR Move your cursor under the line of  within the main() method. • Right click and select Web Service Client Resources->Call Web

Service Operation

Page 6: Web Services Part 2

NIYAZ AHMAD RAO 6

Web Service Description language (WSDL 1.2)

Page 7: Web Services Part 2

NIYAZ AHMAD RAO 7

Introduction

• WSDL (Web Services Description Language) is an XML-based language for describing Web services and how to access them.

• WSDL is written in XML

• WSDL is an XML document

• WSDL is used to describe Web services

• WSDL is also used to locate Web services

• WSDL is not yet a W3C standard

Page 8: Web Services Part 2

NIYAZ AHMAD RAO 8

Page 9: Web Services Part 2

NIYAZ AHMAD RAO 9

The WSDL Document Structure

• A WSDL document describes a web service using these major elements

ELEMENTS DEFINES

<portType> The operations performed by the web service

<message> The messages used by the web service

<types> The data types used by the web service

<binding> The communication protocols used by the web service

Page 10: Web Services Part 2

NIYAZ AHMAD RAO 10

• The main structure of the WSDL document is looks like :

• <definitions>• <types>• definition of types........• </types> • <message>• definition of a message.... • </message> • <portType>

definition of a port....... • </portType>• <binding>

definition of a binding.... • </binding>• </definitions>

Page 11: Web Services Part 2

NIYAZ AHMAD RAO 11

• <portType> :– It describes a web service, the operations that can be

performed, and the messages that are involved.– The <portType> element can be compared to a function library

(or a module, or a class) in a traditional programming language.• <message>

– The <message> element defines the data elements of an operation

– Each message can consist of one or more parts. The parts can be compared to the parameters of a function call in a traditional programming language.

• <types>– The <types> element defines the data type that are used by the

web service • <binding>

– The <binding> element defines the message format and protocol details for each port.

Page 12: Web Services Part 2

NIYAZ AHMAD RAO 12

• <message name="getTermRequest">• <part name="term" type="xs:string"/>• </message> • <message name="getTermResponse">

<part name="value" type="xs:string"/>• </message>• <portType name="glossaryTerms">

<operation name="getTerm"> <input message="getTermRequest"/> <output message="getTermResponse"/> </operation></portType>

• In this example the <portType> element defines "glossaryTerms" as the name of a port, and "getTerm" as the name of an operation

Page 13: Web Services Part 2

NIYAZ AHMAD RAO 13

WSDL Ports

• The <portType> element is the most important WSDL element. • It defines a web service, the operations that can be performed,

and the messages that are involved

• Operation type : The request-response type is the most common operation type, but WSDL defines four types:

• One-way :The operation can receive a message but will not return response

• Request-response :The operation can receive a request and will return a response

• Solicit-response: The operation can send a request and will wait for a response

• Notification: The operation can send a message but will not wait for a response

Page 14: Web Services Part 2

NIYAZ AHMAD RAO 14

• <message name="newTermValues">• <part name="term" type="xs:string"/>• <part name="value" type="xs:string"/>• </message>• <portType name="glossaryTerms">

<operation name="setTerm">

<input name="newTerm" message="newTermValues"/> </operation>

• </portType >

• In this example the port "glossaryTerms" defines a one-way operation called "setTerm". The "setTerm" operation allows input of new glossary terms messages using a "newTermValues" message with the input parameters "term" and "value". However, no output is defined for the operation.

Page 15: Web Services Part 2

NIYAZ AHMAD RAO 15

• Request-response model :

• <message name="getTermRequest">• <part name="term" type="xs:string"/>• </message>• <message name="getTermResponse">

<part name="value" type="xs:string"/>

</message>• <portType name="glossaryTerms">

<operation name="getTerm"><input message="getTermRequest"/><output message="getTermResponse"/>

</operation></portType>

Page 16: Web Services Part 2

NIYAZ AHMAD RAO 16

WSDL Binding

• WSDL bindings defines the message format and protocol details for a web service.

• Consider a request-response model :

• <message name="getTermRequest">• <part name="term" type="xs:string"/>• </message>• <message name="getTermResponse">

<part name="value" type="xs:string"/> </message>• <portType name="glossaryTerms">

<operation name="getTerm"><input message="getTermRequest"/><output message="getTermResponse"/>

</operation></portType>

Page 17: Web Services Part 2

NIYAZ AHMAD RAO 17

• <binding type="glossaryTerms" name="b1">

<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />

• <operation> <soap:operation soapAction="http://example.com/getTerm"/>

<input> <soap:body use="literal"/> </input>

<output> <soap:body use="literal"/></output>• </operation>• </binding>

• The binding element has two attributes - the name attribute and the type attribute.The name attribute (you can use any name you want) defines the name of the binding, and the type attribute points to the port for the binding, in this case the "glossaryTerms" port.

Page 18: Web Services Part 2

NIYAZ AHMAD RAO 18

• Soap:binding :

• style attribute : – indicates whether the operation is RPCoriented(messages

containing parameters and return values) or document-oriented (message containing document(s)).by default it is document

• transport attribute :– indicates which transport to use :

• http://schemas.xmlsoap.org/soap/http (for HTTP) http://schemas.xmlsoap.org/soap/smtp (for SMTP)

• soapAction attribute:– specifies the value of the SOAPAction header for this operation

Page 19: Web Services Part 2

NIYAZ AHMAD RAO 19

Soap:body :– Provides information on how to assemble the different message

parts inside the Body element– Used in both RPC-oriented and document-oriented messages

• Use attribute : "literal|encoded“– literal

• parts define the concrete schema of the message XML document fragment can be validated against its XML schema

– Encoded• Indicates whether the message parts are encoded using

some encoding rules

Page 20: Web Services Part 2

NIYAZ AHMAD RAO 20

RPC vs. Document style

RPC DOCUMENT

Procedural call Buisness document

Function call schema

Typically within internet Over internet

Simple point-to-point complex

Within enterprise Between enterprise

Trusted enviroment Blind trust

Page 21: Web Services Part 2

NIYAZ AHMAD RAO 21

Creating WSDL File manually(using Net-Beans)

• In this example we create a WSDL document for operation add of two integers

• Create web project say “MyWSDLProject” , in this choose new WSDL document(XML) called “MyWSDL”

• In the folder option choose Web/WEB-INF folder

• For the Operation Name, enter add. • For the Message Part Name, change part1 to x. • For the Element Or Type, click ... • Chose int from Built-in Types. • Add y as int type.

Page 22: Web Services Part 2

NIYAZ AHMAD RAO 22

• For Output, change part1 to result. • Change the Element Or Type field to xsd:int. • Click Next

• Choose RPC Literal

• Right click myWSDLPortType and select Add->Operation

• For Operation Name, enter hello. • Change Message Part Name of the Input section to name. • Change the Element Or Type to xsd:string.

• Change Message Part Name of the Output section to resultHello • Change the Element Or Type to xsd:string.

• Click Source tab window and see the source of the WSDL file

Page 23: Web Services Part 2

NIYAZ AHMAD RAO 23

Universal Description, Discovery and Integration

(UDDI)

Page 24: Web Services Part 2

NIYAZ AHMAD RAO 24

Introduction

• UDDI is a platform-independent framework for describing services, discovering businesses, and integrating business services by using the Internet.

– UDDI stands for Universal Description, Discovery and Integration

– UDDI is a directory for storing information about web services – UDDI is a directory of web service interfaces described by

WSDL – UDDI communicates via SOAP – UDDI is built into the Microsoft .NET platform

Page 25: Web Services Part 2

NIYAZ AHMAD RAO 25

• Before UDDI, there was no Internet standard for businesses to reach their customers and partners with information about their products and services. Nor was there a method of how to integrate into each other's systems and processes

• Problems the UDDI specification can help to solve:

– Making it possible to discover the right business from the millions currently online

– Defining how to enable commerce once the preferred business is discovered

– Reaching new customers and increasing access to current customers

– Expanding offerings and extending market reach – Solving customer-driven need to remove barriers to allow for

rapid participation in the global Internet economy – Describing services and business processes programmatically in

a single, open, and secure environment

Page 26: Web Services Part 2

NIYAZ AHMAD RAO 26

How can UDDI be Used

• Example :

If the industry published an UDDI standard for flight rate checking and reservation, airlines could register their services into an UDDI directory.

Travel agencies could then search the UDDI directory to find the airline's reservation interface.

When the interface is found, the travel agency can communicate with the service immediately because it uses a well-defined reservation interface

Page 27: Web Services Part 2

NIYAZ AHMAD RAO 27

Who is Supporting UDDI?

• UDDI is a cross-industry effort driven by all major platform and software providers like Dell, Fujitsu, HP, Hitachi, IBM, Intel, Microsoft, Oracle, SAP, and Sun, as well as a large community of marketplace operators, and e-business leaders.

• Over 220 companies are members of the UDDI

community.

Page 28: Web Services Part 2

NIYAZ AHMAD RAO 28

Business supporting data

• White pages– address, contact, and known identifiers

• Yellow pages– industrial categorizations– Industry: NAICS (Industry codes - US Govt.)– Product/Services: UN/SPSC (ECMA)– Location: Geographical taxonomy

• Green pages– technical information about services

Page 29: Web Services Part 2

NIYAZ AHMAD RAO 29

UDDI Data type

• Buisness Entity Data type (white page):

– Top-level data structure that holds descriptive information about a business entity

– Service descriptions and technical information are expressed within a businessEntity

Page 30: Web Services Part 2

NIYAZ AHMAD RAO 30

Annotations Used in JAX-WS

• @WebService

– Marks a Java class as implementing a Web Service, or a Java interface as defining a Web Service interface.

– Attributes• name• serviceName• targetNamespace• wsdlLocation

Page 31: Web Services Part 2

NIYAZ AHMAD RAO 31

• @WebMethod– Customizes a method that is exposed as a Web Service

operation– The method is not required to throw java.rmi.RemoteException.– Attributes

• Action• operationName

• @WebParam– Customizes the mapping of an individual parameter to a Web

Service message part and XML element.– Attributes

• name• targetNamespace

Page 32: Web Services Part 2

NIYAZ AHMAD RAO 32

Creating a web-service

• package org.me.calculator;• import javax.jws.WebMethod;• import javax.jws.WebParam;• import javax.jws.WebService;• @WebService()

• public class CalculatorWS {• @WebMethod public int add(@WebParam(name = "i") int i, @WebParam(name = "j")

int j) {• int k = i + j;• return k;• } }• Same steps as before :

Page 33: Web Services Part 2

NIYAZ AHMAD RAO 33

• Create a servlet client:

• import javax.xml.ws.WebServiceRef;• import org.me.calculator.CalculatorWSService;• public class myservlet extends HttpServlet {• @WebServiceRef(wsdlLocation =

"http://localhost:8080/CalculateWSapplication/CalculatorWSService?wsdl")• private CalculatorWSService service;• Public void doGet(…………. )• {• try {• org.me.calculator.CalculatorWS port = service.getCalculatorWSPort();• int i = 0,,j=0;• int result = port.add(6, 4); out.println("Result = "+result);• } catch (Exception ex) { } • }

Page 34: Web Services Part 2

NIYAZ AHMAD RAO 34

Problem

• Create a Web-Service for calculating interest ( principal, rate and time) as input parameter.– Create a simple java client for accessing this service– Create a servlet client for access this service– Create a JSP client for access this service

Page 35: Web Services Part 2

NIYAZ AHMAD RAO 35

Java Api for XML registries(JAXR)

Page 36: Web Services Part 2

NIYAZ AHMAD RAO 36

Registry and registry operation

• To submit and store shared information

• To identify, name, describe, classify,relate, group, and associate shared information

• To query, discover and retrieve shared information

Page 37: Web Services Part 2

NIYAZ AHMAD RAO 37

Registry use-case Scenario in B2B

Page 38: Web Services Part 2

NIYAZ AHMAD RAO 38

History of Registry specification

eCo Framework

UDDI

JAXR

ISO 11179

OASIS Registry

ebXML Registry

Page 39: Web Services Part 2

NIYAZ AHMAD RAO 39

What is JAXR

• Standard Java™ API for performing registry operations over diverse set of registries

• A unified information model for describing business registry content

• Provides multi-layered API abstractions

• Enabling technology for web services in the J2EE™ platform

Page 40: Web Services Part 2

NIYAZ AHMAD RAO 40

Any Client to Any RegistryInteroperability

RegistryBrowsers

J2EEComponents

DesktopApplications

ebXML UDDI

JAXRAPI

Other

DIVERSE CLIENT

DIVERSE REGISTRY PROVIDER

Page 41: Web Services Part 2

NIYAZ AHMAD RAO 41

JAXR Architectural Roles

• Registry Provider• – Provides an implementation of a registry specification• – Is not expected to implement JAXR specification• – examples: UDDI registry, ebXML registry

• JAXR Provider• – Implements JAXR specification• – Typically functions as facade to existing registry providers• – example: JAXR reference implementation

• JAXR Client• – Uses JAXR APIs to access services provided by JAXR Provider

Page 42: Web Services Part 2

NIYAZ AHMAD RAO 42

JAXR Architecture

JAXR Client

UDDI Provider Other ProvidereBXML Provider

JAXR PLUGGABLE PROVIDER

UDDIeBXML OTHER

CAPABILITY SPECIFIC INTERFACE

DIVERSE REGISTRIES

Page 43: Web Services Part 2

NIYAZ AHMAD RAO 43

JAXR Architecture

• Remote communication occurs between JAXR provider and Registry providers– Security concerns (over remote communication) need to be

addressed – Use registry provider specific protocols

• UDDI message over SOAP for UDDI registry provider • ebXML MS for ebXML reg/rep

• JAXR client and JAXR provider are expected to be co-located in a same JVM in most implementations

Page 44: Web Services Part 2

NIYAZ AHMAD RAO 44

Java API for XML-based RPC(JAX-RPC)

Page 45: Web Services Part 2

NIYAZ AHMAD RAO 45

Remote procedural call (RPC)

• RMI• CORBA• COM

• Common agenda : – Common Interface between client and Server– Stub for client, Tie/skeleton for server– On-the-wire protocol needs to be agreed upon

• RPC Example : Java RMI

Page 46: Web Services Part 2

NIYAZ AHMAD RAO 46

• Remote procedure calls (RPCs) are the precursors to modern Web services that are based on the Simple Object Access Protocol (SOAP) or Representational State Transfer (REST).

• Because all of the Java™ platform's Web service APIs are built on the concepts introduced in RPC, understanding the Java APIs for XML-Based RPC (JAX-RPC) is an almost mandatory step for writing efficient and effective Web services in the Java language.

Page 47: Web Services Part 2

NIYAZ AHMAD RAO 47

RPC Example : JAX-RPC

Page 48: Web Services Part 2

NIYAZ AHMAD RAO 48

• Common Interface : Each client and server having a common interface in distributed environment

• Service is described in IDL (Interface Description Language)– IDL of CORBA service– Java RMI interface in RMI (Language specific)– WSDL for Web service

• Concept of XML based RPC :– Uses Standards based on XML

• SOAP is the “protocol”• WSDL is the IDL

– Any communication protocol can be used as a transport• e.g.) HTTP, SMTP, FTP etc.

Page 49: Web Services Part 2

NIYAZ AHMAD RAO 49

What is JAX-RPC

• Java™ API for XML-based RPC– Web Services operations are performed by exchanging SOAP

1.1 messages

• Services are described using WSDL– WSDL is the contract between service provider and client (only

contract needed)

• Web service endpoints and clients use JAX-RPC programming model

• Key technology for Web Services in the J2EE 1.4 platform

Page 50: Web Services Part 2

NIYAZ AHMAD RAO 50

JAX-RPC Architecture

WSDL DocumentJAX-RPCClient

Generated Code

Container Container

Client-side JAX-RPCRuntime System

Server-side JAX-RPCRuntime System

JAX-RPCService Endpoint

SOAP

HTTP

Page 51: Web Services Part 2

NIYAZ AHMAD RAO 51

Build an RPC service and client using JAX-RPC (using Apache Axis)

• This Example divided into following module :– getting and installing JAX-RPC– configuring it in Tomcat server– building a server-side RPC receiver– client-side application.

1. Install Tomcat 6.x

2. Check your tomcat installation.

Page 52: Web Services Part 2

NIYAZ AHMAD RAO 52

3. Download Apache Axis 1.4 – JAX-RPC isn't part of a standard Java distribution, you'll need to

do a little work to get it installed and configured.– Download axis-bin-1_4.zip file– Expand the package you've downloaded. You'll get a directory

named something like axis-1_4.

4. There is no need to install anything for JAX-RPC– Like the Java API for XML Binding (JAXB) or the Java API for

XML Processing (JAXP), and even standard APIs like JDBC, JAX-RPC is really an API specification

– The classes and interfaces included with JAX-RPC are all in the javax.xml.rpc package and several subpackages:

Page 53: Web Services Part 2

NIYAZ AHMAD RAO 53

• Apache Axis comes with a sample Web application that can be deployed on any servlet container. You simply need to copy the Axis Web application into the location in your servlet container where Web applications reside, and test out Axis.

5.Copy the Axis folder into web-apps directory of Tomcat installation

6. Restart your Tomcat Server

7. Open the browser and type :

http://localhost:8080/axis

Page 54: Web Services Part 2

NIYAZ AHMAD RAO 54

Page 55: Web Services Part 2

NIYAZ AHMAD RAO 55

• Validation page for Apache Axis (with several errors)

Page 56: Web Services Part 2

NIYAZ AHMAD RAO 56

• This will display you all necessary jar file

• Some error message and Warning message is also display

• 8. Download all missing jar files :– Activation.jar– Mail.jar etc.

• 9. Copy these jar files to the lib folder of servlet engine– C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib

• Again restart your server and do the same , this time make sure there is no errors. Also copy the tools.jar from jdk1.6 installation to the above lib folder

Page 57: Web Services Part 2

NIYAZ AHMAD RAO 57

• Here we are making sample application for searching books.• 10 . First we create a java programme later we convert that

programme into service.• Define Class and methods calls

• import java.util.*;• public class BookSearcher• {• private Map books;• public BookSearcher()• {• books = new HashMap();• // for example purposes• addBooks();• }• public void setBooks(Map books) { • this.books = books;• }

Page 58: Web Services Part 2

NIYAZ AHMAD RAO 58

• public void addBook(String title, List keywords) {• books.put(title, keywords);• }• public void addKeyword(String title, String keyword) {• List keywords = (List)books.get(title);• if (keywords != null) {• keywords.add(keyword);• } else {• keywords = new LinkedList();• keywords.add(keyword);• books.put(title, keywords);• } • }• public List getKeywords(String title) {• return (List)books.get(title);• }•

Page 59: Web Services Part 2

NIYAZ AHMAD RAO 59

• public List search(String keyword) {• List results = new LinkedList();• for (Iterator i = books.keySet().iterator(); i.hasNext(); ) {• String title = (String)i.next(); • List keywords = (List)books.get(title);• if (keywords.contains(keyword)) {• results.add(title);• }• }• return results;• }• private void addBooks() { // add some sample data • List keywords = new LinkedList();• keywords.add("presentation"); • keywords.add("Keynote");• keywords.add("PowerPoint"); • keywords.add("design");

Page 60: Web Services Part 2

NIYAZ AHMAD RAO 60

• addBook("Presentation Zen", keywords);• List keywords2 = new LinkedList();• keywords2.add("presentation");• keywords2.add("user interface design");• keywords2.add("pictures"); • keywords2.add("visuals");• addBook("The Back of the Napkin", keywords2);• List keywords3 = new LinkedList();• keywords3.add("marketing");• keywords3.add("business");• keywords3.add("commercials");• keywords3.add("consumers"); addBook("Purple Cow", keywords3);• List keywords4 = new LinkedList();• keywords4.add("marketing");• keywords4.add("business");•

Page 61: Web Services Part 2

NIYAZ AHMAD RAO 61

• keywords4.add("notecards");• keywords4.add("design");• keywords4.add("visuals");• keywords4.add("pictures");• keywords4.add("humor"); addBook("Indexed", keywords4);• List keywords5 = new LinkedList();• keywords5.add("marketing"); • keywords5.add("business"); • keywords5.add("design");• keywords5.add("emotion");• keywords5.add("functionality"); • keywords5.add("consumers"); addBook("Emotional Design", keywords5);• keywords.clear(); • }• }

Page 62: Web Services Part 2

NIYAZ AHMAD RAO 62

• At this point, you've got a working program without any JAX-RPC code. That's the beauty of using an API like JAX-RPC compared to, other technique .

• With JAX-RPC, you write a normal Java class, without server-side or Web service-specific calls, and apply the JAX-RPC later.

• 11. Turn your class to RPC Service

– making an RPC means to call a method on another machine somewhere.

– In the case of the BookSearcher, we put the BookSearcher class on a Web server somewhere and run a program that uses that class on your local machine.

Page 63: Web Services Part 2

NIYAZ AHMAD RAO 63

• Right now, we have a class (BookSearcher) that will be on the server side of our RPC setup.

• It's just an ordinary Java class now, but once we make certain methods available via RPC, our class is referred to as a service. It serves clients by providing them functions (methods) that can be called. The calling code is referred to as the client or caller.

12. Copy your .java file to a .jws fileIn the case of Axis, the easiest and fastest way to publish your service is to use JWS files. All you need to do is copy your Java source file (ending with .java) to a file with the same name but a .jws extension

13. Copy .jws file into axis folder C:\Program Files\Apache Software Foundation\Tomcat 6.0\

webapps\axis

Page 64: Web Services Part 2

NIYAZ AHMAD RAO 64

• As soon as the Axis Web application "sees" a .jws file in its directory, it compiles the file into a Java Web Service and builds all the SOAP access code needed to allow a client to access the class. Axis even deploys the service right away.

• Start up your servlet engine. The Axis Web application is available at http://hostname:port/axis.– http://localhost:8080/axis/BookSearcher.jws

Page 65: Web Services Part 2

NIYAZ AHMAD RAO 65

Build a client to access a web-service

13 . Update your classpath

Set enviroment variables of classpath

• Earlier, you dropped several JAR files into your servlet engine's lib/directory and used the Axis validation JSP to ensure all those JARs were correctly located.– C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\

mail.jar;– C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\

activation.jar;

Page 66: Web Services Part 2

NIYAZ AHMAD RAO 66

• Add the JAX-RPC and Axis JARs

• First, navigate back to your Axis installation and take a look at the lib directory. You should see something It's easiest to add all of these JAR files to your classpath

– C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\axis\WEB-INF\lib\axis.jar;

– C:\.......................\lib\axis-ant.jar;– C:\.......................\lib\jaxrpc.jar;– C:\.......................\lib\saaj.jar;– C:\.......................\lib\wsdl4j-1.5.1.jar;– C:\.......................\lib\commons-logging-1.0.4.jar;– C:\.......................\lib\commons-discovery-0.2.jar;– C:\.......................\lib\log4j-1.2.8.jar;

Page 67: Web Services Part 2

NIYAZ AHMAD RAO 67

• 14 . Create Client file BookSearcherClient.java

• import java.io.IOException;• import java.net.MalformedURLException;• import java.net.URL;• import javax.xml.namespace.QName;• import javax.xml.rpc.ServiceException;• import org.apache.axis.client.Call;• import org.apache.axis.client.Service;• public class BookSearcherClient {• public static final String SERVICE_URL

="http://localhost:8080/axis/BookSearcher.jws";• private Service service;• private Call call;• private URL serviceUrl;• public BookSearcherClient() { }

Page 68: Web Services Part 2

NIYAZ AHMAD RAO 68

• public Object[] search(String keyword) throws IOException• {• try {• if (service == null) {• service = new Service();• }• if (call == null) {• call = (Call)service.createCall();• }• if (serviceUrl == null)• {• serviceUrl = new URL(SERVICE_URL); //create a Java URL

object to store the target endpoint — the Web-accessible• URL where your Web service is published:• }• call.setTargetEndpointAddress(serviceUrl); //At this point, our

Call knows which service to connect to. However, we also need• to indicate the specific operation you want to invoke•

Page 69: Web Services Part 2

NIYAZ AHMAD RAO 69

• call.setOperationName(new QName("http://soapinterop.org/","search")); //method for that, and you can't just pass in a simple string.Instead, you must pass that method a QName, indicate that we're working with SOAP encoding, and then provide the string name of the operation you want to call:

• // Make call and get result• Object[] results = (Object[])call.invoke(new Object[] { keyword });• return results;• }• catch (MalformedURLException e) {• throw new IOException("Error creating service URL at " +

SERVICE_URL);• } • catch (ServiceException e) {• throw new IOException("Error creating service call: " +

e.getMessage());• } }•

Page 70: Web Services Part 2

NIYAZ AHMAD RAO 70

• public static void main(String[] args) throws IOException {• if (args.length != 1) {• System.err.println("Usage: java BookSearcherClient [search

keyword]");• return;• }• String keyword = args[0];• BookSearcherClient client = new BookSearcherClient();• Object[] results = client.search(keyword);• System.out.println("Returned books for keyword '" + keyword +

"':");• for (int i = 0; i<results.length; i++) {• System.out.println(" " + results[i]); • } • } }

• Compile and run :– Java BookSearcherClient business/presentation/design etc