Chapter 29- Introduction to Web Services and SOAP.ppt

上传人:cleanass300 文档编号:379700 上传时间:2018-10-09 格式:PPT 页数:20 大小:340KB
下载 相关 举报
Chapter 29- Introduction to Web Services and SOAP.ppt_第1页
第1页 / 共20页
Chapter 29- Introduction to Web Services and SOAP.ppt_第2页
第2页 / 共20页
Chapter 29- Introduction to Web Services and SOAP.ppt_第3页
第3页 / 共20页
Chapter 29- Introduction to Web Services and SOAP.ppt_第4页
第4页 / 共20页
Chapter 29- Introduction to Web Services and SOAP.ppt_第5页
第5页 / 共20页
亲,该文档总共20页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、Chapter 29: Introduction to Web Services and SOAP,Outline 29.1 Introduction 29.2 Simple Object Access Protocol (SOAP) 29.3 SOAP Weather Service,29.1 Introduction,SOAP Simple Object Access Protocol Promote interoperability Communication among different software systems. Provides XML communication in

2、many Web services Web Services Exposes public interfaces usable by Web applications,29.2 Simple Object Access Protocol (SOAP),SOAP HTTP-XML-based protocol Enables application to communicate over Internet Uses XML documents called messages SOAP message contains an envelope Describes messages content

3、and intended recipient Ability to make a Remote Procedure Call (RPC) Request to another machine to run a task,Fig. 29.1 Class SimpleService. Line 4 Lines 6-12,1 / Fig. 29.1: SimpleService.java 2 / Implementation for the requested method on the server 3 4 public class SimpleService 5 6 public String

4、getWelcome( String message ) throws Exception 7 8 String text = 9 “Welcome to SOAP!nHere is your message: “ + message; 10 11 return text; / response 12 13 ,29.2 Simple Object Access Protocol (SOAP) (cont.),Fig. 29.2 SOAP package administration tool.,29.2 Simple Object Access Protocol (SOAP) (cont.),

5、Fig. 29.3 Description of deployed service.,Fig. 29.4 Client making a SOAP request (part 1). Lines 10-11 Line 13 Line 17 Lines 27-28 Lines 31-33,1 / Fig. 29.4 : GetMessage.java 2 / Program that makes a SOAP RPC 3 4 / import Java packages 5 import java.io.*; 6 import .*; 7 import java.util.*; 8 9 / im

6、port third-party packages 10 import org.apache.soap.*; 11 import org.apache.soap.rpc.*; 12 13 public class GetMessage 14 15 / main method 16 public static void main( String args ) 17 String encodingStyleURI = Constants.NS_URI_SOAP_ENC; 18 String message; 19 20 if ( args.length != 0 ) 21 message = ar

7、gs 0 ; 22 else 23 message = “Thanks!“; 24 25 / attempt SOAP remote procedure call 26 try 27 URL url = new URL( 28 “http:/localhost:8080/soap/servlet/rpcrouter“ ); 29 30 / build call 31 Call remoteMethod = new Call(); 32 remoteMethod.setTargetObjectURI( 33 “urn:xml-simple-message“ ); 34,Fig. 29.4 Cli

8、ent making a SOAP request (part 2). Lines 40-44 Line 48 Lines 51-57 Lines 60-63,35 / set name of remote method to be invoked 36 remoteMethod.setMethodName( “getWelcome“ ); 37 remoteMethod.setEncodingStyleURI( encodingStyleURI ); 38 39 / set parameters for remote method 40 Vector parameters = new Vec

9、tor(); 41 42 parameters.addElement( new Parameter( “message“, 43 String.class, message, null ) ); 44 remoteMethod.setParams( parameters ); 45 Response response; 46 47 / invoke remote method 48 response = remoteMethod.invoke( url, “ ); 49 50 / get response 51 if ( response.generatedFault() ) 52 Fault

10、 fault = response.getFault(); 53 54 System.err.println( “CALL FAILED:nFault Code = “ 55 + fault.getFaultCode()+ “nFault String = “ 56 + fault.getFaultString() ); 57 58 59 else 60 Parameter result = response.getReturnValue(); 61 62 / display result of call 63 System.out.println( result.getValue() );

11、64 65 66,Fig. 29.4 Client making a SOAP request (part 3).,67 / catch malformed URL exception 68 catch ( MalformedURLException malformedURLException ) 69 malformedURLException.printStackTrace(); 70 System.exit( 1 ); 71 72 73 / catch SOAPException 74 catch ( SOAPException soapException ) 75 System.err

12、.println( “Error message: “ + 76 soapException.getMessage() ); 77 System.exit( 1 ); 78 79 80 ,java GetMessage Welcome to SOAP! Here is your message: Thanks!,java GetMessage “my message” Welcome to SOAP! Here is your message: my message,29.3 SOAP Weather Service,SOAP Weather Service Web service Modif

13、ication of RMI-based Weather Service (Chapter 13) Use SOAP RPC instead of Java RMI Send information from server to client,Fig. 29.5 SOAP implementation of class Weather-Service (part 1). Line 11,1 / Fig. 29.5: WeatherService.java 2 / WeatherService provides a method to retrieve weather 3 / informati

14、on from the National Weather Service. 4 package com.deitel.advjhtp1.soap.weather; 5 6 / Java core packages 7 import java.io.*; 8 import .URL; 9 import java.util.*; 10 11 public class WeatherService 12 13 private Vector weatherInformation; / WeatherBean objects 14 15 / get weather information from NW

15、S 16 private void updateWeatherConditions() 17 18 try 19 System.out.println( “Update weather information.“ ); 20 21 / National Weather Service Travelers Forecast page 22 URL url = new URL( 23 “http:/iwin.nws.noaa.gov/iwin/us/traveler.html“ ); 24 25 / set up text input stream to read Web page content

16、s 26 BufferedReader in = new BufferedReader( 27 new InputStreamReader( url.openStream() ) ); 28 29 / helps determine starting point of data on Web page 30 String separator = “TAV12“; 31 32 / locate separator string in Web page 33 while ( !in.readLine().startsWith( separator ) ) 34 ; / do nothing 35,

17、Fig. 29.5 SOAP implementation of class Weather-Service (part 2). Lines 66-71,36 / strings representing headers on Travelers Forecast 37 / Web page for daytime and nighttime weather 38 String dayHeader = 39 “CITY WEA HI/LO WEA HI/LO“; 40 String nightHeader = 41 “CITY WEA LO/HI WEA LO/HI“; 42 43 Strin

18、g inputLine = “; 44 45 / locate header that begins weather information 46 do 47 inputLine = in.readLine(); 48 while ( !inputLine.equals( dayHeader ) ,Fig. 29.5 SOAP implementation of class Weather-Service (part 3). Lines 95-100,70 weatherInformation.add( 71 inputLine.substring( 23, 29 ) ); 72 73 inp

19、utLine = in.readLine(); / get next citys data 74 75 76 in.close(); / close connection to NWS Web server 77 78 System.out.println( “Weather information updated.“ ); 79 80 81 / process failure to connect to National Weather Service 82 catch( .ConnectException connectException ) 83 connectException.pri

20、ntStackTrace(); 84 System.exit( 1 ); 85 86 87 / process other exceptions 88 catch( Exception exception ) 89 exception.printStackTrace(); 90 System.exit( 1 ); 91 92 93 94 / implementation for WeatherService interface method 95 public Vector getWeatherInformation() 96 97 updateWeatherConditions(); 98

21、99 return weatherInformation; 100 101 ,Fig. 29.6 SOAP implementation of class Weather-ServiceClient (part 1). Lines 31-32,1 / Fig. 29.6: WeatherServiceClient.java 2 / WeatherServiceClient accesses the WeatherService remote 3 / object via SOAP to retrieve weather information. 4 package com.deitel.adv

22、jhtp1.soap.weather; 5 6 / Java core packages 7 import java.util.*; 8 import .*; 9 10 / Java extension packages 11 import javax.swing.*; 12 13 / third-party packages 14 import org.apache.soap.*; 15 import org.apache.soap.rpc.*; 16 17 / Deitel packages 18 import com.deitel.advjhtp1.rmi.weather.*; 19 2

23、0 public class WeatherServiceClient extends JFrame 21 22 / WeatherServiceClient constructor 23 public WeatherServiceClient( String server ) 24 25 super( “SOAP WeatherService Client“ ); 26 27 / connect to server and get weather information 28 try 29 30 / URL of remote SOAP object 31 URL url = new URL

24、( “http:/“ + server + “:8080/soap/“ 32 + “servlet/rpcrouter“ ); 33,Fig. 29.6 SOAP implementation of class Weather-ServiceClient (part 2). Lines 35-37 Line 46 Lines 49-55 Lines 58-65,34 / build SOAP RPC call 35 Call remoteMethod = new Call(); 36 remoteMethod.setTargetObjectURI( 37 “urn:xml-weather-se

25、rvice“ ); 38 39 / set name of remote method to be invoked 40 remoteMethod.setMethodName( 41 “getWeatherInformation“ ); 42 remoteMethod.setEncodingStyleURI( 43 Constants.NS_URI_SOAP_ENC ); 44 45 / invoke remote method 46 Response response = remoteMethod.invoke( url, “ ); 47 48 / get response 49 if (

26、response.generatedFault() ) 50 Fault fault = response.getFault(); 51 52 System.err.println( “CALL FAILED:nFault Code = “ 53 + fault.getFaultCode() + “nFault String = “ 54 + fault.getFaultString() ); 55 56 57 else 58 Parameter result = response.getReturnValue(); 59 60 Vector weatherStrings = ( Vector

27、 ) 61 result.getValue(); 62 63 / get weather information from result object 64 List weatherInformation = createBeans( 65 weatherStrings ); 66,Fig. 29.6 SOAP implementation of class Weather-ServiceClient (part 3). Line 95,67 / create WeatherListModel for weather information 68 ListModel weatherListMo

28、del = 69 new WeatherListModel( weatherInformation ); 70 71 / create JList, set its CellRenderer and add to 72 / layout 73 JList weatherJList = new JList( weatherListModel ); 74 weatherJList.setCellRenderer( new 75 WeatherCellRenderer() ); 76 getContentPane().add( new 77 JScrollPane( weatherJList ) )

29、; 78 79 80 / end try 81 82 / handle bad URL 83 catch ( MalformedURLException malformedURLException ) 84 malformedURLException.printStackTrace(); 85 86 87 / handle SOAP exception 88 catch ( SOAPException soapException ) 89 soapException.printStackTrace(); 90 91 92 / end WeatherServiceClient construct

30、or 93 94 / create List of WeatherBeans from Vector of Strings 95 public List createBeans( Vector weatherStrings ) 96 97 List list = new ArrayList();,Fig. 29.6 SOAP implementation of class Weather-ServiceClient (part 4).,98 for ( int i = 0; ( weatherStrings.size() - 1 ) i; 99 i += 3 ) 100 list.add( n

31、ew WeatherBean( 101 ( String ) weatherStrings.elementAt( i ), 102 ( String ) weatherStrings.elementAt( i + 1 ), 103 ( String ) weatherStrings.elementAt( i + 2 ) ) ); 104 105 106 return list; 107 108 109 / execute WeatherServiceClient 110 public static void main( String args ) 111 112 WeatherServiceC

32、lient client = null; 113 114 / if no server IP address or host name specified, 115 / use “localhost“; otherwise use specified host 116 if ( args.length = 0 ) 117 client = new WeatherServiceClient( “localhost“ ); 118 else 119 client = new WeatherServiceClient( args 0 ); 120 121 / configure and displa

33、y application window 122 client.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); 123 client.pack(); 124 client.setResizable( false ); 125 client.setVisible( true ); 126 127 ,29.3 SOAP Weather Service (cont.),Fig. 29.7 Apache SOAP Admin page.,29.3 SOAP Weather Service (cont.),Fig. 29.8 Apache SOAP Service Deployment Descriptor Template.,29.3 SOAP Weather Service (cont.),Fig. 29.9 SOAP WeatherService Client.,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 教学课件 > 大学教育

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1