Web Services Part 2


Published on

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Web Services Part 2

  1. 1. Create a simple web-services (say hello) <ul><li>Choose web-project named it HelloWebService </li></ul><ul><li>Choose java class say “Hello” within “mypackage” package </li></ul><ul><li>package mypackage; </li></ul><ul><li>import javax.jws.WebService; </li></ul><ul><li>@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 { </li></ul><ul><li>public Hello() { } </li></ul><ul><li>// Business method we want to expose as Web service operation </li></ul><ul><li>public String sayHello(String name){ </li></ul><ul><li>return &quot;Hello &quot; + name + &quot;!&quot;; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  2. 2. <ul><li>Right click on the project and choose deploy for deploy the project. </li></ul><ul><li>Test the Web service : Expand Web services and right click Hello Web service node and select Test Web Service </li></ul><ul><li>Observe HelloService Web Service Tester page is displayed. </li></ul><ul><li>Type in your name, Niyaz Rao in this example. Click sayHello button </li></ul><ul><li>Observe that a new page is displayed with the result of the testing </li></ul>
  3. 3. <ul><li>Observe that the SOAP message request and response are also displayed </li></ul><ul><li>Click backward button of the browser to go the previous page </li></ul><ul><li>Click WSDL File link .observer it </li></ul><ul><li>Observe the URL of the WSDL document is http://localhost:8080/HelloWebService/HelloService?WSDL.  </li></ul><ul><li>We will use this URL later on when you create the corresponding Web service client. </li></ul>
  4. 4. Creating a client for consuming web-service <ul><li>Choose java project called “HelloWebServiceClient” </li></ul><ul><li>Right click HelloWebServiceClient and select Web Services and under File Types,select WebService Client. </li></ul><ul><li>Observe WSDL and Client Location pane gets displayed </li></ul><ul><li>Select WSDL URL and specify the location of the WSDL http://localhost:8080/HelloWebService/HelloService?WSDL </li></ul><ul><li>For the Package field, type in mypackage. </li></ul><ul><li>We can also select Project option (instead of WSDL URL option) </li></ul>
  5. 5. <ul><li>package hellowebserviceclient; public class Main {     public static void main(String[] args) {         try </li></ul><ul><li>{ </li></ul><ul><li>// Call Web Service Operation </li></ul><ul><li>mypackage.HelloService service = new mypackage.HelloService(); mypackage.Hello port = service.getHelloPort();   java.lang.String result = port.sayHello(“niyaz rao”);  System.out.println(&quot;Result = &quot;+result);         } </li></ul><ul><li>catch (Exception ex) {        }               } } </li></ul><ul><li>OR Move your cursor under the line of  within the main() method. </li></ul><ul><li>Right click and select Web Service Client Resources->Call Web Service Operation </li></ul>
  6. 6. Web Service Description language (WSDL 1.2)
  7. 7. Introduction <ul><li>WSDL (Web Services Description Language) is an XML-based language for describing Web services and how to access them. </li></ul><ul><li>WSDL is written in XML </li></ul><ul><li>WSDL is an XML document </li></ul><ul><li>WSDL is used to describe Web services </li></ul><ul><li>WSDL is also used to locate Web services </li></ul><ul><li>WSDL is not yet a W3C standard </li></ul>
  8. 9. The WSDL Document Structure <ul><li>A WSDL document describes a web service using these major elements </li></ul>DEFINES ELEMENTS The communication protocols used by the web service <binding> The data types used by the web service <types> The messages used by the web service <message> The operations performed by the web service <portType>
  9. 10. <ul><li>The main structure of the WSDL document is looks like : </li></ul><ul><li><definitions> </li></ul><ul><li><types> </li></ul><ul><li>definition of types........ </li></ul><ul><li></types> </li></ul><ul><li><message> </li></ul><ul><li>definition of a message.... </li></ul><ul><li></message> </li></ul><ul><li><portType> </li></ul><ul><ul><li> definition of a port....... </li></ul></ul><ul><li></portType> </li></ul><ul><li><binding> </li></ul><ul><ul><li> definition of a binding.... </li></ul></ul><ul><li></binding> </li></ul><ul><li></definitions> </li></ul>
  10. 11. <ul><li><portType> : </li></ul><ul><ul><li>It describes a web service, the operations that can be performed, and the messages that are involved. </li></ul></ul><ul><ul><li>The <portType> element can be compared to a function library (or a module, or a class) in a traditional programming language . </li></ul></ul><ul><li><message> </li></ul><ul><ul><li>The <message> element defines the data elements of an operation </li></ul></ul><ul><ul><li>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. </li></ul></ul><ul><li><types> </li></ul><ul><ul><li>The <types> element defines the data type that are used by the web service </li></ul></ul><ul><li><binding> </li></ul><ul><ul><li>The <binding> element defines the message format and protocol details for each port. </li></ul></ul>
  11. 12. <ul><li><message name=&quot;getTermRequest&quot;> </li></ul><ul><li><part name=&quot;term&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li></message> </li></ul><ul><li><message name=&quot;getTermResponse&quot;> </li></ul><ul><ul><li><part name=&quot;value&quot; type=&quot;xs:string&quot;/> </li></ul></ul><ul><li></message> </li></ul><ul><li><portType name=&quot;glossaryTerms&quot;> <operation name=&quot;getTerm&quot;> <input message=&quot;getTermRequest&quot;/> <output message=&quot;getTermResponse&quot;/> </operation> </portType> </li></ul><ul><li>In this example the <portType> element defines &quot;glossaryTerms&quot; as the name of a port , and &quot;getTerm&quot; as the name of an operation </li></ul>
  12. 13. WSDL Ports <ul><li>The <portType> element is the most important WSDL element. </li></ul><ul><li>It defines a web service , the operations that can be performed, and the messages that are involved </li></ul><ul><li>Operation type : The request-response type is the most common operation type, but WSDL defines four types: </li></ul><ul><li>One-way :The operation can receive a message but will not return response </li></ul><ul><li>Request-response :The operation can receive a request and will return a response </li></ul><ul><li>Solicit-response: The operation can send a request and will wait for a response </li></ul><ul><li>Notification: The operation can send a message but will not wait for a response </li></ul>
  13. 14. <ul><li><message name=&quot;newTermValues&quot;> </li></ul><ul><li><part name=&quot;term&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li><part name=&quot;value&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li></message> </li></ul><ul><li><portType name=&quot; glossaryTerms &quot;> </li></ul><ul><li><operation name=&quot; setTerm &quot;> </li></ul><ul><li><input name=&quot;newTerm&quot; message=&quot; newTermValues &quot;/> </operation> </li></ul><ul><li></portType > </li></ul><ul><li>In this example the port &quot;glossaryTerms&quot; defines a one-way operation called &quot;setTerm&quot;. The &quot;setTerm&quot; operation allows input of new glossary terms messages using a &quot;newTermValues&quot; message with the input parameters &quot;term&quot; and &quot;value&quot;. However, no output is defined for the operation. </li></ul>
  14. 15. <ul><li>Request-response model : </li></ul><ul><li><message name=&quot;getTermRequest&quot;> </li></ul><ul><li><part name=&quot;term&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li></message> </li></ul><ul><li><message name=&quot;getTermResponse&quot;> </li></ul><ul><li> <part name=&quot;value&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li></message> </li></ul><ul><li><portType name=&quot;glossaryTerms&quot;> <operation name=&quot;getTerm&quot;> <input message=&quot;getTermRequest&quot;/> <output message=&quot;getTermResponse&quot;/> </operation> </portType> </li></ul>
  15. 16. WSDL Binding <ul><li>WSDL bindings defines the message format and protocol details for a web service. </li></ul><ul><li>Consider a request-response model : </li></ul><ul><li><message name=&quot;getTermRequest&quot;> </li></ul><ul><li><part name=&quot;term&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li></message> </li></ul><ul><li><message name=&quot;getTermResponse&quot;> </li></ul><ul><li> <part name=&quot;value&quot; type=&quot;xs:string&quot;/> </li></ul><ul><li></message> </li></ul><ul><li><portType name=&quot;glossaryTerms&quot;> <operation name=&quot;getTerm&quot;> <input message=&quot;getTermRequest&quot;/> <output message=&quot;getTermResponse&quot;/> </operation> </portType> </li></ul>
  16. 17. <ul><li><binding type =&quot;glossaryTerms&quot; name =&quot;b1&quot;> </li></ul><ul><li> <soap:binding style =&quot;document&quot; transport =&quot;http://schemas.xmlsoap.org/soap/http&quot; /> </li></ul><ul><li><operation> <soap:operation soapAction =&quot;http://example.com/getTerm&quot;/> </li></ul><ul><li><input> <soap:body use=&quot;literal&quot;/> </input> </li></ul><ul><li><output> <soap:body use=&quot;literal&quot;/></ output> </li></ul><ul><li></operation> </li></ul><ul><li></binding> </li></ul><ul><li>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 &quot;glossaryTerms&quot; port. </li></ul>
  17. 18. <ul><li>Soap:binding : </li></ul><ul><li>style attribute : </li></ul><ul><ul><li>indicates whether the operation is RPCoriented(messages containing parameters and return values) or document-oriented (message containing document(s)).by default it is document </li></ul></ul><ul><li>transport attribute : </li></ul><ul><ul><li>indicates which transport to use : </li></ul></ul><ul><ul><ul><li>http://schemas.xmlsoap.org/soap/http (for HTTP) http://schemas.xmlsoap.org/soap/smtp (for SMTP) </li></ul></ul></ul><ul><li>soapAction attribute: </li></ul><ul><ul><li>specifies the value of the SOAPAction header for this operation </li></ul></ul>
  18. 19. <ul><li>Soap:body : </li></ul><ul><ul><li>Provides information on how to assemble the different message parts inside the Body element </li></ul></ul><ul><ul><li>Used in both RPC-oriented and document-oriented messages </li></ul></ul><ul><li>Use attribute : &quot;literal|encoded“ </li></ul><ul><ul><li>literal </li></ul></ul><ul><ul><ul><li>parts define the concrete schema of the message XML document fragment can be validated against its XML schema </li></ul></ul></ul><ul><ul><li>Encoded </li></ul></ul><ul><ul><ul><li>Indicates whether the message parts are encoded using some encoding rules </li></ul></ul></ul>
  19. 20. RPC vs. Document style Blind trust Trusted enviroment Between enterprise Within enterprise complex Simple point-to-point Over internet Typically within internet schema Function call Buisness document Procedural call DOCUMENT RPC
  20. 21. Creating WSDL File manually (using Net-Beans) <ul><li>In this example we create a WSDL document for operation add of two integers </li></ul><ul><li>Create web project say “MyWSDLProject” , in this choose new WSDL document(XML) called “MyWSDL” </li></ul><ul><li>In the folder option choose Web/WEB-INF folder </li></ul><ul><li>For the Operation Name, enter add. </li></ul><ul><li>For the Message Part Name, change part1 to x. </li></ul><ul><li>For the Element Or Type, click ... </li></ul><ul><li>Chose int from Built-in Types. </li></ul><ul><li>Add y as int type. </li></ul>
  21. 22. <ul><li>For Output, change part1 to result. </li></ul><ul><li>Change the Element Or Type field to xsd:int. </li></ul><ul><li>Click Next </li></ul><ul><li>Choose RPC Literal </li></ul><ul><li>Right click myWSDLPortType and select Add->Operation </li></ul><ul><li>For Operation Name, enter hello. </li></ul><ul><li>Change Message Part Name of the Input section to name. </li></ul><ul><li>Change the Element Or Type to xsd:string. </li></ul><ul><li>Change Message Part Name of the Output section to resultHello </li></ul><ul><li>Change the Element Or Type to xsd:string. </li></ul><ul><li>Click Source tab window and see the source of the WSDL file </li></ul>
  22. 23. Universal Description, Discovery and Integration (UDDI)
  23. 24. Introduction <ul><li>UDDI is a platform-independent framework for describing services, discovering businesses, and integrating business services by using the Internet. </li></ul><ul><ul><li>UDDI stands for Universal Description, Discovery and Integration </li></ul></ul><ul><ul><li>UDDI is a directory for storing information about web services </li></ul></ul><ul><ul><li>UDDI is a directory of web service interfaces described by WSDL </li></ul></ul><ul><ul><li>UDDI communicates via SOAP </li></ul></ul><ul><ul><li>UDDI is built into the Microsoft .NET platform </li></ul></ul>
  24. 25. <ul><li>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 </li></ul><ul><li>Problems the UDDI specification can help to solve: </li></ul><ul><ul><li>Making it possible to discover the right business from the millions currently online </li></ul></ul><ul><ul><li>Defining how to enable commerce once the preferred business is discovered </li></ul></ul><ul><ul><li>Reaching new customers and increasing access to current customers </li></ul></ul><ul><ul><li>Expanding offerings and extending market reach </li></ul></ul><ul><ul><li>Solving customer-driven need to remove barriers to allow for rapid participation in the global Internet economy </li></ul></ul><ul><ul><li>Describing services and business processes programmatically in a single, open, and secure environment </li></ul></ul>
  25. 26. How can UDDI be Used <ul><li>Example : </li></ul><ul><li>If the industry published an UDDI standard for flight rate checking and reservation, airlines could register their services into an UDDI directory. </li></ul><ul><li>Travel agencies could then search the UDDI directory to find the airline's reservation interface. </li></ul><ul><li>When the interface is found, the travel agency can communicate with the service immediately because it uses a well-defined reservation interface </li></ul>
  26. 27. Who is Supporting UDDI? <ul><li>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. </li></ul><ul><li>Over 220 companies are members of the UDDI community . </li></ul>
  27. 28. Business supporting data <ul><li>White pages </li></ul><ul><ul><li>address, contact, and known identifiers </li></ul></ul><ul><li>Yellow pages </li></ul><ul><ul><li>industrial categorizations </li></ul></ul><ul><ul><li>Industry: NAICS (Industry codes - US Govt.) </li></ul></ul><ul><ul><li>Product/Services: UN/SPSC (ECMA) </li></ul></ul><ul><ul><li>Location: Geographical taxonomy </li></ul></ul><ul><li>Green pages </li></ul><ul><ul><li>technical information about services </li></ul></ul>
  28. 29. UDDI Data type <ul><li>Buisness Entity Data type ( white page ): </li></ul><ul><ul><li>Top-level data structure that holds descriptive information about a business entity </li></ul></ul><ul><ul><li>Service descriptions and technical information are expressed within a businessEntity </li></ul></ul>
  29. 30. Annotations Used in JAX-WS <ul><li>@WebService </li></ul><ul><ul><li>Marks a Java class as implementing a Web Service, or a Java interface as defining a Web Service interface. </li></ul></ul><ul><ul><li>Attributes </li></ul></ul><ul><ul><ul><li>name </li></ul></ul></ul><ul><ul><ul><li>serviceName </li></ul></ul></ul><ul><ul><ul><li>targetNamespace </li></ul></ul></ul><ul><ul><ul><li>wsdlLocation </li></ul></ul></ul>
  30. 31. <ul><li>@WebMethod </li></ul><ul><ul><li>Customizes a method that is exposed as a Web Service operation </li></ul></ul><ul><ul><li>The method is not required to throw java.rmi.RemoteException. </li></ul></ul><ul><ul><li>Attributes </li></ul></ul><ul><ul><ul><li>Action </li></ul></ul></ul><ul><ul><ul><li>operationName </li></ul></ul></ul><ul><li>@WebParam </li></ul><ul><ul><li>Customizes the mapping of an individual parameter to a Web Service message part and XML element. </li></ul></ul><ul><ul><li>Attributes </li></ul></ul><ul><ul><ul><li>name </li></ul></ul></ul><ul><ul><ul><li>targetNamespace </li></ul></ul></ul>
  31. 32. Creating a web-service <ul><li>package org.me.calculator; </li></ul><ul><li>import javax.jws.WebMethod; </li></ul><ul><li>import javax.jws.WebParam; </li></ul><ul><li>import javax.jws.WebService; </li></ul><ul><li>@WebService() </li></ul><ul><li>public class CalculatorWS { </li></ul><ul><li>@WebMethod </li></ul><ul><li>public int add(@WebParam(name = &quot;i&quot;) int i, @WebParam(name = &quot;j&quot;) int j) { </li></ul><ul><li>int k = i + j; </li></ul><ul><li>return k; </li></ul><ul><li>} } </li></ul><ul><li>Same steps as before : </li></ul>
  32. 33. <ul><li>Create a servlet client: </li></ul><ul><li>import javax.xml.ws.WebServiceRef; </li></ul><ul><li>import org.me.calculator.CalculatorWSService; </li></ul><ul><li>public class myservlet extends HttpServlet { </li></ul><ul><li>@WebServiceRef(wsdlLocation = &quot;http://localhost:8080/CalculateWSapplication/CalculatorWSService?wsdl &quot;) </li></ul><ul><li>private CalculatorWSService service; </li></ul><ul><li>Public void doGet(…………. ) </li></ul><ul><li>{ </li></ul><ul><li>try { </li></ul><ul><li>org.me.calculator.CalculatorWS port = service.getCalculatorWSPort(); </li></ul><ul><li>int i = 0,,j=0; </li></ul><ul><li>int result = port.add(6, 4); </li></ul><ul><li>out.println(&quot;Result = &quot;+result); </li></ul><ul><li>} catch (Exception ex) { } </li></ul><ul><li>} </li></ul>
  33. 34. Problem <ul><li>Create a Web-Service for calculating interest ( principal, rate and time) as input parameter . </li></ul><ul><ul><li>Create a simple java client for accessing this service </li></ul></ul><ul><ul><li>Create a servlet client for access this service </li></ul></ul><ul><ul><li>Create a JSP client for access this service </li></ul></ul>
  34. 35. Java Api for XML registries (JAXR)
  35. 36. Registry and registry operation <ul><li>To submit and store shared information </li></ul><ul><li>To identify, name, describe, classify,relate, group, and associate shared information </li></ul><ul><li>To query, discover and retrieve shared information </li></ul>
  36. 37. Registry use-case Scenario in B2B
  37. 38. History of Registry specification eCo Framework UDDI JAXR ISO 11179 OASIS Registry ebXML Registry
  38. 39. What is JAXR <ul><li>Standard Java™ API for performing registry operations over diverse set of registries </li></ul><ul><li>A unified information model for describing business registry content </li></ul><ul><li>Provides multi-layered API abstractions </li></ul><ul><li>Enabling technology for web services in the J2EE™ platform </li></ul>
  39. 40. Any Client to Any Registry Interoperability Registry Browsers J2EE Components Desktop Applications ebXML UDDI JAXR API Other DIVERSE CLIENT DIVERSE REGISTRY PROVIDER
  40. 41. JAXR Architectural Roles <ul><li>Registry Provider </li></ul><ul><li>– Provides an implementation of a registry specification </li></ul><ul><li>– Is not expected to implement JAXR specification </li></ul><ul><li>– examples: UDDI registry, ebXML registry </li></ul><ul><li>JAXR Provider </li></ul><ul><li>– Implements JAXR specification </li></ul><ul><li>– Typically functions as facade to existing registry providers </li></ul><ul><li>– example: JAXR reference implementation </li></ul><ul><li>JAXR Client </li></ul><ul><li>– Uses JAXR APIs to access services provided by JAXR Provider </li></ul>
  42. 43. JAXR Architecture <ul><li>Remote communication occurs between JAXR provider and Registry providers </li></ul><ul><ul><li>Security concerns (over remote communication) need to be addressed </li></ul></ul><ul><ul><li>Use registry provider specific protocols </li></ul></ul><ul><ul><ul><li>UDDI message over SOAP for UDDI registry provider </li></ul></ul></ul><ul><ul><ul><li>ebXML MS for ebXML reg/rep </li></ul></ul></ul><ul><li>JAXR client and JAXR provider are expected to be co-located in a same JVM in most implementations </li></ul>
  43. 44. Java API for XML-based RPC (JAX-RPC)
  44. 45. Remote procedural call (RPC) <ul><li>RMI </li></ul><ul><li>CORBA </li></ul><ul><li>COM </li></ul><ul><li>Common agenda : </li></ul><ul><ul><li>Common Interface between client and Server </li></ul></ul><ul><ul><li>Stub for client, Tie/skeleton for server </li></ul></ul><ul><ul><li>On-the-wire protocol needs to be agreed upon </li></ul></ul><ul><li>RPC Example : Java RMI </li></ul>
  45. 46. <ul><li>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). </li></ul><ul><li>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. </li></ul>
  46. 47. RPC Example : JAX-RPC
  47. 48. <ul><li>Common Interface : Each client and server having a common interface in distributed environment </li></ul><ul><li>Service is described in IDL ( Interface Description Language ) </li></ul><ul><ul><li>IDL of CORBA service </li></ul></ul><ul><ul><li>Java RMI interface in RMI (Language specific) </li></ul></ul><ul><ul><li>WSDL for Web service </li></ul></ul><ul><li>Concept of XML based RPC : </li></ul><ul><ul><li>Uses Standards based on XML </li></ul></ul><ul><ul><ul><li>SOAP is the “protocol” </li></ul></ul></ul><ul><ul><ul><li>WSDL is the IDL </li></ul></ul></ul><ul><ul><li>Any communication protocol can be used as a transport </li></ul></ul><ul><ul><ul><li>e.g.) HTTP, SMTP, FTP etc. </li></ul></ul></ul>
  48. 49. What is JAX-RPC <ul><li>Java™ API for XML-based RPC </li></ul><ul><ul><li>Web Services operations are performed by exchanging SOAP 1.1 messages </li></ul></ul><ul><li>Services are described using WSDL </li></ul><ul><ul><li>WSDL is the contract between service provider and client (only contract needed) </li></ul></ul><ul><li>Web service endpoints and clients use JAX-RPC programming model </li></ul><ul><li>Key technology for Web Services in the J2EE 1.4 platform </li></ul>
  49. 50. JAX-RPC Architecture WSDL Document JAX-RPC Client Generated Code Container Container Client-side JAX-RPC Runtime System Server-side JAX-RPC Runtime System JAX-RPC Service Endpoint SOAP HTTP
  50. 51. Build an RPC service and client using JAX-RPC (using Apache Axis) <ul><li>This Example divided into following module : </li></ul><ul><ul><li>getting and installing JAX-RPC </li></ul></ul><ul><ul><li>configuring it in Tomcat server </li></ul></ul><ul><ul><li>building a server-side RPC receiver </li></ul></ul><ul><ul><li>client-side application. </li></ul></ul><ul><li>1. Install Tomcat 6.x </li></ul><ul><li>2. Check your tomcat installation. </li></ul>
  51. 52. <ul><li>3. Download Apache Axis 1.4 </li></ul><ul><ul><li>JAX-RPC isn't part of a standard Java distribution, you'll need to do a little work to get it installed and configured. </li></ul></ul><ul><ul><li>Download axis-bin-1_4.zip file </li></ul></ul><ul><ul><li>Expand the package you've downloaded. You'll get a directory named something like axis-1_4 . </li></ul></ul><ul><li>4. There is no need to install anything for JAX-RPC </li></ul><ul><ul><li>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 </li></ul></ul><ul><ul><li>The classes and interfaces included with JAX-RPC are all in the javax.xml.rpc package and several subpackages: </li></ul></ul>
  52. 53. <ul><li>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. </li></ul><ul><li>5 .Copy the Axis folder into web-apps directory of Tomcat installation </li></ul><ul><li>6. Restart your Tomcat Server </li></ul><ul><li>7. Open the browser and type : </li></ul><ul><li>http://localhost:8080/axis </li></ul>
  53. 55. <ul><li>Validation page for Apache Axis (with several errors) </li></ul>
  54. 56. <ul><li>This will display you all necessary jar file </li></ul><ul><li>Some error message and Warning message is also display </li></ul><ul><li>8. Download all missing jar files : </li></ul><ul><ul><li>Activation.jar </li></ul></ul><ul><ul><li>Mail.jar etc. </li></ul></ul><ul><li>9. Copy these jar files to the lib folder of servlet engine </li></ul><ul><ul><li>C:Program FilesApache Software FoundationTomcat 6.0lib </li></ul></ul><ul><li>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 </li></ul>
  55. 57. <ul><li>Here we are making sample application for searching books. </li></ul><ul><li>10 . First we create a java programme later we convert that programme into service. </li></ul><ul><li>Define Class and methods calls </li></ul><ul><li>import java.util.*; </li></ul><ul><li>public class BookSearcher </li></ul><ul><li>{ </li></ul><ul><li>private Map books; </li></ul><ul><li>public BookSearcher() </li></ul><ul><li>{ </li></ul><ul><li>books = new HashMap(); </li></ul><ul><li>// for example purposes </li></ul><ul><li>addBooks(); </li></ul><ul><li>} </li></ul><ul><li>public void setBooks(Map books) { </li></ul><ul><li>this.books = books; </li></ul><ul><li>} </li></ul>
  56. 58. <ul><li>public void addBook(String title, List keywords) { </li></ul><ul><li>books.put(title, keywords); </li></ul><ul><li>} </li></ul><ul><li>public void addKeyword(String title, String keyword) { </li></ul><ul><li>List keywords = (List)books.get(title); </li></ul><ul><li>if (keywords != null) { </li></ul><ul><li>keywords.add(keyword); </li></ul><ul><li>} else { </li></ul><ul><li>keywords = new LinkedList(); </li></ul><ul><li>keywords.add(keyword); </li></ul><ul><li>books.put(title, keywords); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>public List getKeywords(String title) { </li></ul><ul><li>return (List)books.get(title); </li></ul><ul><li>} </li></ul>
  57. 59. <ul><li>public List search(String keyword) { </li></ul><ul><li>List results = new LinkedList(); </li></ul><ul><li>for (Iterator i = books.keySet().iterator(); i.hasNext(); ) { </li></ul><ul><li>String title = (String)i.next(); </li></ul><ul><li>List keywords = (List)books.get(title); </li></ul><ul><li>if (keywords.contains(keyword)) { </li></ul><ul><li>results.add(title); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>return results; </li></ul><ul><li>} </li></ul><ul><li>private void addBooks() { // add some sample data </li></ul><ul><li>List keywords = new LinkedList(); </li></ul><ul><li>keywords.add(&quot;presentation&quot;); </li></ul><ul><li>keywords.add(&quot;Keynote&quot;); </li></ul><ul><li>keywords.add(&quot;PowerPoint&quot;); </li></ul><ul><li>keywords.add(&quot;design&quot;); </li></ul>
  58. 60. <ul><li>addBook(&quot;Presentation Zen&quot;, keywords); </li></ul><ul><li>List keywords2 = new LinkedList(); </li></ul><ul><li>keywords2.add(&quot;presentation&quot;); </li></ul><ul><li>keywords2.add(&quot;user interface design&quot;); </li></ul><ul><li>keywords2.add(&quot;pictures&quot;); </li></ul><ul><li>keywords2.add(&quot;visuals&quot;); </li></ul><ul><li>addBook(&quot;The Back of the Napkin&quot;, keywords2); </li></ul><ul><li>List keywords3 = new LinkedList(); </li></ul><ul><li>keywords3.add(&quot;marketing&quot;); </li></ul><ul><li>keywords3.add(&quot;business&quot;); </li></ul><ul><li>keywords3.add(&quot;commercials&quot;); </li></ul><ul><li>keywords3.add(&quot;consumers&quot;); </li></ul><ul><li>addBook(&quot;Purple Cow&quot;, keywords3); </li></ul><ul><li>List keywords4 = new LinkedList(); </li></ul><ul><li>keywords4.add(&quot;marketing&quot;); </li></ul><ul><li>keywords4.add(&quot;business&quot;); </li></ul>
  59. 61. <ul><li>keywords4.add(&quot;notecards&quot;); </li></ul><ul><li>keywords4.add(&quot;design&quot;); </li></ul><ul><li>keywords4.add(&quot;visuals&quot;); </li></ul><ul><li>keywords4.add(&quot;pictures&quot;); </li></ul><ul><li>keywords4.add(&quot;humor&quot;); </li></ul><ul><li>addBook(&quot;Indexed&quot;, keywords4); </li></ul><ul><li>List keywords5 = new LinkedList(); </li></ul><ul><li>keywords5.add(&quot;marketing&quot;); </li></ul><ul><li>keywords5.add(&quot;business&quot;); </li></ul><ul><li>keywords5.add(&quot;design&quot;); </li></ul><ul><li>keywords5.add(&quot;emotion&quot;); </li></ul><ul><li>keywords5.add(&quot;functionality&quot;); </li></ul><ul><li>keywords5.add(&quot;consumers&quot;); </li></ul><ul><li>addBook(&quot;Emotional Design&quot;, keywords5); </li></ul><ul><li>keywords.clear(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  60. 62. <ul><li>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 . </li></ul><ul><li>With JAX-RPC, you write a normal Java class, without server-side or Web service-specific calls, and apply the JAX-RPC later. </li></ul><ul><li>11. Turn your class to RPC Service </li></ul><ul><ul><li>making an RPC means to call a method on another machine somewhere. </li></ul></ul><ul><ul><li>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. </li></ul></ul>
  61. 63. <ul><li>Right now, we have a class (BookSearcher) that will be on the server side of our RPC setup. </li></ul><ul><li>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 . </li></ul><ul><li>12. Copy your .java file to a .jws file </li></ul><ul><li>In 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 </li></ul><ul><li>13. Copy .jws file into axis folder </li></ul><ul><li>C:Program FilesApache Software FoundationTomcat 6.0webappsaxis </li></ul>
  62. 64. <ul><li>As soon as the Axis Web application &quot;sees&quot; 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. </li></ul><ul><li>Start up your servlet engine. The Axis Web application is available at http:// hostname : port /axis . </li></ul><ul><ul><li>http://localhost:8080/axis/BookSearcher.jws </li></ul></ul>
  63. 65. Build a client to access a web-service <ul><li>13 . Update your classpath </li></ul><ul><li>Set enviroment variables of classpath </li></ul><ul><li>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. </li></ul><ul><ul><li>C:Program FilesApache Software FoundationTomcat 6.0lib mail.jar ; </li></ul></ul><ul><ul><li>C:Program FilesApache Software FoundationTomcat 6.0lib activation.jar ; </li></ul></ul>
  64. 66. <ul><li>Add the JAX-RPC and Axis JARs </li></ul><ul><li>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 </li></ul><ul><ul><li>C:Program FilesApache Software FoundationTomcat 6.0webappsaxisWEB-INFlib axis.jar ; </li></ul></ul><ul><ul><li>C:.......................lib axis-ant.jar ; </li></ul></ul><ul><ul><li>C:.......................lib jaxrpc.jar ; </li></ul></ul><ul><ul><li>C:.......................lib saaj.jar ; </li></ul></ul><ul><ul><li>C:.......................lib wsdl4j-1.5.1.jar ; </li></ul></ul><ul><ul><li>C:.......................lib commons-logging-1.0.4.jar ; </li></ul></ul><ul><ul><li>C:.......................lib commons-discovery-0.2.jar ; </li></ul></ul><ul><ul><li>C:.......................lib log4j-1.2.8.jar ; </li></ul></ul>
  65. 67. <ul><li>14 . Create Client file BookSearcherClient.java </li></ul><ul><li>import java.io.IOException; </li></ul><ul><li>import java.net.MalformedURLException; </li></ul><ul><li>import java.net.URL; </li></ul><ul><li>import javax.xml.namespace.QName; </li></ul><ul><li>import javax.xml.rpc.ServiceException; </li></ul><ul><li>import org.apache.axis.client.Call; </li></ul><ul><li>import org.apache.axis.client.Service; </li></ul><ul><li>public class BookSearcherClient { </li></ul><ul><li>public static final String SERVICE_URL =&quot;http://localhost:8080/axis/BookSearcher.jws&quot;; </li></ul><ul><li>private Service service; </li></ul><ul><li>private Call call; </li></ul><ul><li>private URL serviceUrl; </li></ul><ul><li>public BookSearcherClient() { } </li></ul>
  66. 68. <ul><li>public Object[] search(String keyword) throws IOException </li></ul><ul><li>{ </li></ul><ul><li>try { </li></ul><ul><li>if (service == null) { </li></ul><ul><li>service = new Service(); </li></ul><ul><li>} </li></ul><ul><li>if (call == null) { </li></ul><ul><li>call = (Call)service.createCall(); </li></ul><ul><li>} </li></ul><ul><li>if (serviceUrl == null) </li></ul><ul><li>{ </li></ul><ul><li>serviceUrl = new URL(SERVICE_URL); //create a Java URL object to store the target endpoint — the Web-accessible </li></ul><ul><li>URL where your Web service is published: </li></ul><ul><li>} </li></ul><ul><li>call.setTargetEndpointAddress(serviceUrl); //At this point, our Call knows which service to connect to. However, we also need </li></ul><ul><li>to indicate the specific operation you want to invoke </li></ul>
  67. 69. <ul><li>call.setOperationName(new QName(&quot;http://soapinterop.org/&quot;,&quot;search&quot;)); //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: </li></ul><ul><li>// Make call and get result </li></ul><ul><li>Object[] results = (Object[])call.invoke(new Object[] { keyword }); </li></ul><ul><li>return results; </li></ul><ul><li>} </li></ul><ul><li>catch (MalformedURLException e) { </li></ul><ul><li>throw new IOException(&quot;Error creating service URL at &quot; + SERVICE_URL); </li></ul><ul><li>} </li></ul><ul><li>catch (ServiceException e) { </li></ul><ul><li>throw new IOException(&quot;Error creating service call: &quot; + e.getMessage()); </li></ul><ul><li>} } </li></ul>
  68. 70. <ul><li>public static void main(String[] args) throws IOException { </li></ul><ul><li>if (args.length != 1) { </li></ul><ul><li>System.err.println(&quot;Usage: java BookSearcherClient [search keyword]&quot;); </li></ul><ul><li>return; </li></ul><ul><li>} </li></ul><ul><li>String keyword = args[0]; </li></ul><ul><li>BookSearcherClient client = new BookSearcherClient(); </li></ul><ul><li>Object[] results = client.search(keyword); </li></ul><ul><li>System.out.println(&quot;Returned books for keyword '&quot; + keyword + &quot;':&quot;); </li></ul><ul><li>for (int i = 0; i<results.length; i++) { </li></ul><ul><li>System.out.println(&quot; &quot; + results[i]); </li></ul><ul><li>} </li></ul><ul><li>} } </li></ul><ul><li>Compile and run : </li></ul><ul><ul><li>Java BookSearcherClient business/presentation/design etc </li></ul></ul>