Presentation is loading. Please wait.

Presentation is loading. Please wait.

SE 370: Programming Web Services Week 4: SOAP & NetBeans Copyright © Steven W. Johnson February 1, 2013.

Similar presentations


Presentation on theme: "SE 370: Programming Web Services Week 4: SOAP & NetBeans Copyright © Steven W. Johnson February 1, 2013."— Presentation transcript:

1 SE 370: Programming Web Services Week 4: SOAP & NetBeans Copyright © Steven W. Johnson February 1, 2013

2 SOAP-based web services Nusoap SOAP web services in: PHP NetBeans (Java) This week: 2

3 Client has limited alternatives: data is a single value data is an array data has multiple values (SOAP) data is constants (written into the code) data is variables (collected from a form) Conceptual web services: 3

4 Server has limited number of alternatives: simple calculation/print text (‘Hello World’) complex calculation using many variables writes query using variables; database read/update operation to a table return single variable return an array Conceptual web services: 4

5 5 server serialize de-serialize client Data appears as: single value array function parameters Data collected by: constants in code dorm inputs document Data processed: simple single calculation complex calculation prepare data as a query Data used by: function query/database table Data transferred as: variable array

6 SOAP: 6 Last week, XML-RPC; this week, SOAP XML-RPC is the BASIC of web services SOAP is the C of web services Rules Structure Format

7 SOAP: 7 SOAP more verbose; more capable SOAP is about document transfer SOAP requires an IDE If you like polymorphic accessors, enumerations, and orthogonal protocol binding, then SOAP is for you

8 SOAP: 8 SOAP adds to XML-RPC: user-defined data types specify recipient message specific processing control others NOT (as) easy to use Biggest advantage: customization of message

9 SOAP: 9 SOAP: Simple Object Access Protocol Platform and language independent HTTP and XML, like XML-RPC Difference: files instead of data Supports different protocols and formats: HTTP, SMTP, MIME W3C recommendation (2003) ≈44 pages long (not so short)

10 Ways to use SOAP: HTTP FTP I/O Jabber SMTP POP3 TCP MQSeries SOAP: 10

11 Has three elements: may include fault element CANNOT contain a DTD reference CANNOT hold XML processing instructions SOAP: 11

12 SOAP: 12 SOAP skeleton:

13 SOAP: 13 SOAP header: Optional; first child of Envelope if present Holds application-specific information Defines how to process SOAP message three defined attributes to default namespace: mustUnderstand Actor encodingStyle

14 SOAP: 14 SOAP envelope: root element of SOAP message holds one body element SOAP body element: held in envelope IBM

15 Nusoap: 15 Used with PHP; a collection of PHP classes Allows rpc/encoded & document/literal services Has some similarites with XML-RPC Dietrich Ayala

16 ‘nusoap’ is the library (collection of PHP classes) Converts PHP into XML (SOAP envelopes) SOAP 1.1, WSDL 1.1, and HTTP 1.0/1.1 Version (2007) Nusoap: 16

17 The process: Nusoap: 17

18 Again, three files are used: client, server, library All files placed in web folder Nusoap: 18 www client.php server.php nusoap.php (SOAP library)

19 Nusoap: 19 server Nusoap client Nusoap serialize de-serialize

20 Create ‘client.php’ in Dreamweaver : Prints ‘Hello Scott’ to web page Nusoap: 20 call('hello', array('name' => 'Scott')); echo ' Result '; print_r($result); echo ' '; ?> Uses ‘hello’ function Sends data Absolute address

21 Create ‘server.php’ in Dreamweaver: Nusoap: 21 register('hello'); function hello($name) { return 'Hello, '. $name; } $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service($HTTP_RAW_POST_DATA); ?> if

22 Lab: Nusoap 22 POST /server.php HTTP/1.0 Host: User-Agent: NuSOAP/0.7.3 (1.114) Content-Type: text/xml; charset=ISO SOAPAction: "" Content-Length: 500 Scott Request: header/xml generated by ‘client’

23 Response: header/xml generated by ‘server’ Lab: Nusoap 23 HTTP/ OK Date: Fri, 15 Mar :38:22 GMT Server: Apache/ (Win32) mod_ssl/ OpenSSL/0.9.8o PHP/5.3.3 DAV/2 X-Powered-By: PHP/5.3.3 X-SOAP-Server: NuSOAP/0.7.3 (1.114) Content-Length: 518 Vary: User-Agent,Accept-Encoding Connection: close Content-Type: text/xml; charset=ISO Hello, Scott

24 ‘client’ calls ‘server’ for processing of data: Processing function can be much more complex Lab: Nusoap 24 call('hello', array('name' => 'Scott')); echo ' Result '; print_r($result); echo ' '; ?> register('hello'); function hello($name) { return 'Hello, '. $name; } $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service($HTTP_RAW_POST_DATA); ?> “Scott” $name = “Scott”

25 Copy/paste ‘nusoap’ into your web server Fix the URL, change name to your name Open page: localhost/nusoap/client.php Lab: Nusoap 25

26 Copy/paste ‘dice’ into your web server Holds: client server library 6 images of a die (zar) Lab: Dice 26

27 Working page: Lab: Dice 27

28 Code required (server): ignore the passed variable (eliminate??) return is an array, must define random number generator: (returns int) Lab: Dice 28 mt_rand(1,6);//more efficient rand(1,6); $client->call('hello'); $result = []; 3.png 125px x 125px

29 Code required (client): function arguments = parameters remove messages add form, images, text output Lab: Dice 29.png" width="125" height="125">

30 Done! Lab: Dice 30

31 Client code (PHP): Lab: Dice 31 getError(); // Check for an error if ($err) { echo ' Constructor error '. $err. ' '; } $result = $client->call('hello'); } ?>

32 32 table { margin-left: auto; margin-right: auto; }.png" width="125" height="125">.png" width="125" height="125"> Your spin =

33 Server code (PHP): Lab: Dice 33 register('hello'); // Define the method as a PHP function function hello() { $result = []; $result[0] = mt_rand(1, 6); $result[1] = mt_rand(1, 6); return $result; } // Use the request to (try to) invoke the service $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $server->service($HTTP_RAW_POST_DATA); ?>

34 Break 34

35 SOAP best built in IDE Our IDE: NetBeans, IDE for Java programming* Default: UniServer isn’t Java enabled Time for new web stack NetBeans: 35 Programming Language Web Server DatabaseDatabase Language Engines Operating System

36 NetBeans: IDE for Java programming* Download: NetBeans 7.3 (64-bit) includes: GlassFish (application & web server) Apache Tomcat (Java servlets) NetBeans: 36

37 An open source IDE Primary aim: Java Supports: HTML/5, PHP, C/C++, etc. Started as a school project Purchased by Sun Purchased by Oracle What is NetBeans? 37

38 Java Programming environment: Java Server Pages (replaces PHP) Tomcat Glassfish server (Replaces Apache) NetBeans: 38

39 Java Server Pages Serves same function as.php and.asp Java is compiled NetBeans: 39

40 All three: are server side technologies allow scripting to be mixed HTML require server software (compiler/interpreter) ASP and JSP may require application server JSP is an extension on a web page; uses servlets PHP is a programming language; uses scripts NetBeans: 40

41 Biggest differences: strictly data typed requires an application server (compiler) instead of NetBeans: 41 Some even numbers: <% int loop = 1; for (int i=0; i Even number :. Done.

42 GlassFish: an application server also serves web pages open source manages JavaEE applications NetBeans: 42 *many IDEs offer support in many programming paradigms

43 What’s an application server? A server that hosts applications PHP: interprets and then sends appserver: runs code embedded in web pages connect to databases connect to middleware provides methods clients can call.asp and.jsp are big users NetBeans: 43 *many IDEs offer support in many programming paradigms

44 What’s an application server? NetBeans: 44 receives request sends response data/method calls sends output creates or locates html method of communication: http information passed: markup compiles executables/run processes method of communication: anything information passed: program logic Web Server Application Server

45 Tomcat: Servlet container Made by Apache Open source web server Components: Catalina: servlet container Coyote: HTTP connector Jasper: a JSP engine (compiler) NetBeans: 45 *many IDEs offer support in many programming paradigms

46 Compiles servlets on.jsp Effect: like PHP interpreter for Java NetBeans: 46 Web Server Servlet Container HTTP Request HTTP Response Client

47 NetBeans 7.4 (windows) includes JDK Start Extract: Installing NetBeans: 47

48 Start Install: Installing NetBeans: 48

49 Do lots of thinking and installing Place shortcut in Quick Launch Installing NetBeans: 49

50 Close start page Installing NetBeans: 50

51 Editor good for single-file solutions IDE good when solutions require several files Example: C code source code libraries object files linker files executable IDE versus editor: 51

52 Projects: Holds all files necessary for integrated solution “A folder” holding a multi-file solution A workspace holds many projects Projects hold many folders/files that complete a task IDE versus editors: 52

53 Create a new project: ‘test’ (.html) NetBeans: 53

54 NetBeans: 54

55 Create a new project: ‘test’ (.jsp) NetBeans: 55

56 NetBeans: 56

57 Right click on Project name and ‘run’ NetBeans: 57

58 Check your ‘Output’ panel NetBeans: 58

59 Start GlassFish manually (ONLY if needed): Services window Right-click server node Start NetBeans: 59

60 Unblock the port for Java SOAP web services & NetBeans: 60

61 Generates a blank web page (port :8080) NetBeans: 61

62 Two instances of Java running (after run) One instance of NetBeans SOAP web services & NetBeans: 62

63 Running netstat –ao to check ports: SOAP web services & NetBeans: 63

64 PID of ports goes to ‘big Java’ SOAP web services & NetBeans: 64

65 If GlassFish doesn’t start… Turkish locale seems to be an issue SOAP web services & NetBeans: 65

66 Netbeans_default_options Add to end: SOAP web services & NetBeans: 66 "-J-Duser.language=en -J-Duser.region=US"

67 Location of saved work: NetBeans: 67

68 Holds all files necessary for integrated solution “A folder” holding a multi-file solution A workspace holds many projects Projects hold many folders/files that complete a task Projects: 68

69 Open NetBeans: (wizard approach) File – New Project Categories: “Java Web” Projects: “Web Application” Next Hello NetBeans web service: 69

70 Project name: “HelloNetBeans” (folder name) Next 70 Hello NetBeans web service:

71 Accept default settings Finish 71 Hello NetBeans web service:

72 ‘Client’ page is generated (index.jsp) 72 Hello NetBeans web service:

73 Right-click project, run (test the.jsp file) 73 Hello NetBeans web service:

74 Right click on project name New – Web Service (order probably different) 74 Hello NetBeans web service:

75 Web Service Name: ‘HelloWorld’ Package: ‘com.javapapers.webservice’ Finish 75 Hello NetBeans web service:

76 HelloNetBeans.java (service code) 76 /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.javapapers.webservice; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; /** * Steve = "Hello") public class Hello { /** * This is a sample web service operation = "hello") public String = "name") String txt) { return "Hello " + txt + " !"; } Hello NetBeans web service:

77 Next step, update the.wsdl View the current.wsdl: (works in IE, Chrome) 77 Hello NetBeans web service:

78 78 Hello NetBeans web service:

79 Next step, set up ‘client’ (index.jsp) Remove text “Hello World” 79 Hello NetBeans web service:

80 Right click on project name Choose: New – ‘Web Service Client…’ 80 Hello NetBeans web service:

81 WSDL URL: Package: com.javapapers.webserviceclient Click ‘finish’; project rebuilt ‘localhost’ okay 81 Hello NetBeans web service:

82 Right click on.jsp page Select ‘Web Service Client Resources’ Select ‘Call Web Service Operation…’ 82 Hello NetBeans web service:

83 Select ‘Hello’ from this dialog box and ‘OK’ Expand out tree if necessary 83 Hello NetBeans web service:

84 Code brings in ‘Hello’ from server file 84 Hello NetBeans web service:

85 Update the text string: ‘Hello’ is text from XML ‘Steve’ is text from ‘index’ 85 Hello NetBeans web service:

86 Save your files Right click on project name, run code 86 Hello NetBeans web service:

87 Complete! 87 Hello NetBeans web service:

88 Files of interest: index.jsp (‘client’) HelloWorld.java (‘server’) HelloWorld.wsdl 88 Hello NetBeans web service:

89 index.jsp (client): NetBeansProjects/HelloNetBeans/web/index.jsp 89 <%-- Document : index Created on : Mar 11, 2014, 10:06:36 PM Author : Steve --%> JSP Page <% try { com.javapapers.webserviceclient.Hello_Service service = new com.javapapers.webserviceclient.Hello_Service(); com.javapapers.webserviceclient.Hello port = service.getHelloPort(); // TODO initialize WS operation arguments here java.lang.String name = "Steve"; // TODO process result here java.lang.String result = port.hello(name); out.println("Result = "+result); } catch (Exception ex) { // TODO handle custom exceptions here } %> Hello NetBeans web service:

90 Hello.java: NetBeansProjects/Wk4HelloWorld/src/java/com/javapapers/ webservice/Hello.java 90 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.javapapers.webservice; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; /** * Steve = "Hello") public class Hello { /** * This is a sample web service operation = "hello") public String = "name") String txt) { return "Hello " + txt + " !"; } Hello NetBeans web service:

91 HelloWorld.wsdl: NetBeansProjects/Wk4HelloWorld/build/web/WEB-INF/ wsdl/localhost_8080/HelloNetBeans/Hello.wsdl 91 Hello NetBeans web service:

92 HelloWorld.wsdl: NetBeansProjects/Wk4HelloWorld/build/web/WEB-INF/ wsdl/localhost_8080/Wk4HelloWorld/HelloWorld.wsdl 92 Hello NetBeans web service:

93 1. SOAP messages are defined using an: A.XML DTD B.XML Schema C.XSLT D.XML CSS Quiz: 93

94 2. XML-RPC compared to SOAP: A.Is easier and more powerful B.Is easier and less powerful C.Is more complex and more powerful D.Is more complex and less powerful Quiz: 94

95 3. An XML-RPC message compared to a SOAP message: A.The XML-RPC message is smaller and carries more data B.The XML-RPC message is larger and cards more data C.The SOAP message is larger and carries more data D.The SOAP message is larger and it carries less data Quiz: 95

96 4. Comparing XML-RPC to SOAP: A.XML-RPC allows for the message to be customized B.SOAP allows for the message to be customized C.XML-RPC is sent using HTTP only D.SOAP is sent using HTTP only Quiz: 96

97 5. XML-RPC and SOAP: A.XML-RPC can be made in Notepad B.SOAP can be made in Notepad C.Both must be made using an IDE Quiz: 97

98 6. Java Server Pages (JSP): A.Are a web services technology B.Are a client-side technology C.Are a server-side technology D.Allow scripting to be mixed with HTML E.Scripts are executed before the page is rendered F.Scripts are executed after the page is rendered Quiz: 98

99 7. An application server: A.Inserts the output from scripts into HTML content B.Inserts the output from compiled code into HTML content C.Executes compiled code on the client machine D.Executes scripted code on the client machine Quiz: 99

100 SE 370: Programming Web Services Week 4: SOAP & NetBeans Copyright © Steven W. Johnson February 1, 2013

101 101 Build a more complete service Build ‘client’: pass 2 integers to ‘server’ answer displayed on web page more customized approach (less “wizardy”) Lab: AddNumbers

102 102 Create the project: Lab: AddNumbers

103 103 Rt. click on project name – New – web service Creates the *.java page (Name = filename) Lab: AddNumbers

104 104 Lab: AddNumbers /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.steve.johnson.add; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; /** * Steve = "AddingMachine") public class AddingMachine { /** * This is a sample web service operation = "hello") public String = "name") String txt) { return "Hello " + txt + " !"; }

105 105 Run the.jsp file Lab: AddNumbers

106 106 Now, new begins. Click on ‘Design’ Lab: AddNumbers

107 107 Modify ‘server’ = "hello") public String = "name") String txt) { return "Hello " + txt + " !"; } Lab: AddNumbers

108 108 Click ‘Add Operation…’ Lab: AddNumbers

109 109 Name = method name and method return type Arguments used by the method and data types Lab: AddNumbers

110 110 Method is = "Adder") public int = "one") int = "two") int two) { //TODO write your implementation code here: return 0; = "Adder") public int = "one") int = "two") int two) { //TODO write your implementation code here: return one + two; } Lab: AddNumbers

111 111 Select ‘hello’ and ‘Remove Operation’ Lab: AddNumbers

112 112 Right click on project and deploy Lab: AddNumbers

113 113 Test web Service Lab: AddNumbers

114 114 Lab: AddNumbers

115 115 Rt click on project - New – Web Service Client… Lab: AddNumbers

116 116 Click on Browse button for Project Browse in wsdl Lab: AddNumbers

117 117 Add a different package name at bottom Finish Lab: AddNumbers

118 118 Go to index.jsp file Right click in Source Window, Web Service Client – Web Service Operation Lab: AddNumbers

119 119 Drill down method on.java page OK Lab: AddNumbers

120 120 Change the ‘int’ variables to values Lab: AddNumbers

121 121 Delete ‘Hello World’ Save page Right-click in window and Run File Lab: AddNumbers

122 122 Output generated: Lab: AddNumbers

123 123 Use text fields to get data One text field, one int field Lab: Adult

124 124 Create the project: Lab: Adult

125 125 Open the Palette (Ctrl + Shift + 8) Window – IDE Tools – Palette Drag-and-drop support, no WYSIWYG yet Lab: Adult JSP Page

126 126 Change title to “Age Status” Delete element Lab: Adult JSP Page Hello World! Age Status

127 127 Need four elements? form table? 2 text fields 1 submit button Rules of HTML are the same in.jsp as.php Lab: Adult

128 128 Lab: Adult Create form Create table

129 129 Lab: Adult Add text fields:

130 130 Lab: Adult Name: Age: Add labels: (manually enter)

131 131 Lab: Adult Name: Age: Add submit button:

132 132 Lab: Adult Check work: save and run the page Names of elements shown on page Name Age Submit

133 133 Lab: Adult Build the web service Rt. Click on project name – New – Web Service

134 134 Lab: Adult /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.steve.johnson.adult; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; /** * Steve = "AdultMinor") public class AdultMinor { /** * This is a sample web service operation = "hello") public String = "name") String txt) { return "Hello " + txt + " !"; } AdultMinor.java

135 135 Update the web service method Lab: Adult

136 136 Click ‘Add Operation…’ Lab: Adult

137 137 Name = method name and method return type Arguments used by the method and data types Lab: Adult

138 138 Select ‘hello’ and ‘Remove Operation’ Lab: Adult

139 139 Right click on project and deploy Lab: Adult

140 140 Re-run index.java Lab: Adult

141 141 AdultMinor.java: Lab: Adult package com.steve.johnson.adult; import javax.jws.WebService; import javax.jws.WebMethod; import javax.jws.WebParam; /** * Steve = "AdultMinor") public class AdultMinor { /** * Web service operation = "AgeTest") public String = "Name") String = "Age") int Age) { //TODO write your implementation code here: return null; }

142 142 Fix the ‘AgeTest’ method: Lab: Adult /** * Web service operation = "AgeTest") public String = "Name") String = "Age") int Age) { if (Age < 18) {return Name + " is a minor";} else {return Name + " is an adult";} } }

143 143 Save.java file and test web service Lab: Adult

144 144 Save.java file and test web service Lab: Adult

145 145 Save.java file and test web service Lab: Adult

146 146 Rt click on project - New – Web Service Client… Lab: Adult

147 147 Click on Browse button for Project browse in wsdl add a different package name Lab: Adult

148 148 Go to index.jsp file Right click in Source Window, Web Service Client – Web Service Operation Lab: Adult

149 149 Lab: Adult Connect the form to the web service

150 150 Lab: Adult Extract text field values

151 151 Lab: Adult No ‘isset’ for the submit button Save ‘index.jsp’ Run

152 Break 152

153 Create a web service that finds the average of five scores Print that average score to a text field Watch your data types Assignment: 153 Score 1: Score 2: Score 3: Score 4: Score 5: Average: Submit

154 SE 370: Programming Web Services Week 4: SOAP & NetBeans Copyright © Steven W. Johnson February 1, 2013


Download ppt "SE 370: Programming Web Services Week 4: SOAP & NetBeans Copyright © Steven W. Johnson February 1, 2013."

Similar presentations


Ads by Google