SE 370: Programming Web Services Week 4: SOAP & NetBeans Copyright © Steven W. Johnson February 1,...
-
Upload
kai-roscoe -
Category
Documents
-
view
218 -
download
0
Transcript of SE 370: Programming Web Services Week 4: SOAP & NetBeans Copyright © Steven W. Johnson February 1,...
SE 370: Programming Web Services
Week 4: SOAP & NetBeansCopyright © Steven W. Johnson
February 1, 2013
SOAP-based web services
Nusoap
SOAP web services in:
PHP
NetBeans (Java)
This week:
2
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
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
Conceptual web services:
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
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
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
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
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)
Ways to use SOAP:
HTTP
FTP
I/O
Jabber
SMTP
POP3
TCP
MQSeries
SOAP:
10
Has three elements:
<envelope>
<header>
<body>
may include fault element
CANNOT contain a DTD reference
CANNOT hold XML processing instructions
SOAP:
11
SOAP:
12
SOAP skeleton:
<?xml version="1.0"?><soap:Envelopexmlns:soap="http://www.w3.org/2001/12/soap-envelope"soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">
<soap:Header>...</soap:Header>
<soap:Body>... <soap:Fault> ... </soap:Fault></soap:Body>
</soap:Envelope>
http://www.w3schools.com/soap/soap_envelope.asp
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
SOAP:
14
SOAP envelope:
root element of SOAP message
holds one body element
SOAP body element: held in envelope<?xml version="1.0"?><soap:Envelopexmlns:soap="http://www.w3.org/2001/12/soap-envelope"soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Header> </soap:Header> <soap:Body> <m:GetStockPrice xmlns:m="http://www.example.org/stock"> <m:StockName>IBM</m:StockName> </m:GetStockPrice> </soap:Body></soap:Envelope>
Nusoap:
15
Used with PHP; a collection of PHP classes
Allows rpc/encoded & document/literal services
Has some similarites with XML-RPC
Dietrich Ayala
‘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 1.114 (2007)
Nusoap:
16
The process:
Nusoap:
17
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)
Nusoap:
19
server
Nusoap
client
Nusoap
serialize de-serialize
Create ‘client.php’ in Dreamweaver:
Prints ‘Hello Scott’ to web page
Nusoap:
20
<?php require_once('nusoap.php'); $client = new nusoap_client('http://127.0.0.1/server.php'); $result = $client->call('hello', array('name' => 'Scott')); echo '<h2>Result</h2><pre>'; print_r($result); echo '</pre>';?>
Uses ‘hello’ function
Sends data
Absolute address
Create ‘server.php’ in Dreamweaver:
Nusoap:
21
<?php require_once('nusoap.php'); $server = new soap_server; $server->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
Lab: Nusoap
22
POST /server.php HTTP/1.0 Host: 127.0.0.1 User-Agent: NuSOAP/0.7.3 (1.114) Content-Type: text/xml; charset=ISO-8859-1 SOAPAction: "" Content-Length: 500
<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body> <ns5932:hello xmlns:ns5932="http://tempuri.org"> <name xsi:type="xsd:string">Scott</name> </ns5932:hello></SOAP-ENV:Body></SOAP-ENV:Envelope>
Request: header/xml generated by ‘client’
Response: header/xml generated by ‘server’
Lab: Nusoap
23
HTTP/1.1 200 OK Date: Fri, 15 Mar 2013 13:38:22 GMT Server: Apache/2.2.16 (Win32) mod_ssl/2.2.16 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-8859-1
<?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body> <ns1:helloResponse xmlns:ns1="http://tempuri.org"> <return xsi:type="xsd:string">Hello, Scott</return> </ns1:helloResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
‘client’ calls ‘server’ for processing of data:
Processing function can be much more complex
Lab: Nusoap
24
<?php require_once('nusoap.php'); $client = new nusoap_client('http://127.0.0.1/server.php'); $result = $client->call('hello', array('name' => 'Scott')); echo '<h2>Result</h2><pre>'; print_r($result); echo '</pre>';?> <?php
require_once('nusoap.php'); $server = new soap_server; $server->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”
Copy/paste ‘nusoap’ into your web server
Fix the URL, change name to your name
Open page: localhost/nusoap/client.php
Lab: Nusoap
25
Copy/paste ‘dice’ into your web server
Holds:
client
server
library
6 images of a die (zar)
Lab: Dice
26
Working page:
Lab: Dice
27
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 efficientrand(1,6);
$client->call('hello');
$result = [];
3.png125px x 125px
Code required (client):
function arguments = parameters
remove messages
add form, images, text output
Lab: Dice
29
<img src="<?php echo $result[0]; ?>.png" width="125" height="125"><img src=" 1 .png" width="125" height="125">
Done!
Lab: Dice
30
Client code (PHP):
Lab: Dice
31
<?phpif(isset($_POST['button'])) {
require_once('nusoap.php'); $client = new nusoap_client('http://127.0.0.1/dice/server.php');
$err = $client->getError(); // Check for an error if ($err) { echo '<h2>Constructor error</h2><pre>' . $err . '</pre>'; }
$result = $client->call('hello'); }?>
32
<!DOCTYPE html><html> <head> <style> table {
margin-left: auto; margin-right: auto; }
</style> </head> <body> <p> </p> <form name=“RollDice" method="post" action=""> <table width="350" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="175" height="125"><div align="center"> <img src="<?php echo $result[0]; ?>.png" width="125" height="125"></div> </td> <td width="175" height="125"><div align="center"> <img src="<?php echo $result[1]; ?>.png" width="125" height="125"></div> </td> </tr> <tr> <td height="63" colspan="2"><div align="center"> <input type="submit" name="button" id="button" value="Roll the Dice"></div> </td> </tr> <tr> <td height="63" colspan="2"><div align="center"><h2>Your spin = <?php echo $result[0] + $result[1]; ?></h2> </div> </td> </tr> </table> </form> <p> </p> </body></html>
Server code (PHP):
Lab: Dice
33
<?php // Pull in the NuSOAP code require_once('nusoap.php'); // Create the server instance $server = new soap_server; // Register the method to expose $server->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);?>
Break 34
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
Database
Language Engines
Operating System
NetBeans: IDE for Java programming*
Download: http://netbeans.org/downloads/
NetBeans 7.3 (64-bit) includes:
GlassFish (application & web server)
Apache Tomcat 7.0.34 (Java servlets)
NetBeans:
36
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
Java Programming environment:
Java Server Pages (replaces PHP)
Tomcat
Glassfish server (Replaces Apache)
NetBeans:
38
Java Server Pages
Serves same function as .php and .asp
Java is compiled
NetBeans:
39
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
Biggest differences:
strictly data typed
requires an application server (compiler)
<% %>instead of <? ?>
NetBeans:
41
<h2>Some even numbers:</h2> <% int loop = 1; for (int i=0; i<11; i+=2) { %> <p>Even number <%= loop %>: <%= i %>.</p> <% } %> <p>Done.</p>
GlassFish:
an application server
also serves web pages
open source
manages JavaEE applications
NetBeans:
42*many IDEs offer support in many programming paradigms
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
What’s an application server?
NetBeans:
44
receives request
sends response
data/method calls
sends output
creates or locates htmlmethod of communication: httpinformation passed: markup
compiles executables/run processesmethod of communication: anythinginformation passed: program logic
Web Server
Application Server
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
Compiles servlets on .jsp
Effect: like PHP interpreter for Java
NetBeans:
46
Web Server
Web Server
Servlet Container
HTTP Request
HTTP Response
Client
NetBeans 7.4 (windows) includes JDK
Start Extract:
Installing NetBeans:
47
Start Install:
Installing NetBeans:
48
Do lots of thinking and installing
Place shortcut in Quick Launch
Installing NetBeans:
49
Close start page
Installing NetBeans:
50
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
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
Create a new project: ‘test’ (.html)
NetBeans:
53
NetBeans:
54
Create a new project: ‘test’ (.jsp)
NetBeans:
55
NetBeans:
56
Right click on Project name and ‘run’
NetBeans:
57
Check your ‘Output’ panel
NetBeans:
58
Start GlassFish manually (ONLY if needed):
Services window
Right-click server node
Start
NetBeans:
59
Unblock the port for Java
SOAP web services & NetBeans:
60
Generates a blank web page (port :8080)
NetBeans:
61
Two instances of Java running (after run)
One instance of NetBeans
SOAP web services & NetBeans:
62
Running netstat –ao to check ports:
SOAP web services & NetBeans:
63
PID of ports goes to ‘big Java’
SOAP web services & NetBeans:
64
If GlassFish doesn’t start…
Turkish locale seems to be an issue
SOAP web services & NetBeans:
65
Netbeans_default_options
Add to end:
SOAP web services & NetBeans:
66
"-J-Duser.language=en -J-Duser.region=US"
Location of saved work:
NetBeans:
67
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
Open NetBeans: (wizard approach)
File – New Project
Categories: “Java Web”
Projects: “Web Application”
Next
Hello NetBeans web service:
69http://javapapers.com/web-service/soap-web-service-introduction/
Project name: “HelloNetBeans” (folder name)
Next
70
Hello NetBeans web service:
Accept default settings
Finish
71http://javapapers.com/web-service/soap-web-service-introduction/
Hello NetBeans web service:
‘Client’ page is generated (index.jsp)
72http://javapapers.com/web-service/soap-web-service-introduction/
Hello NetBeans web service:
Right-click project, run (test the .jsp file)
73http://javapapers.com/web-service/soap-web-service-introduction/
Hello NetBeans web service:
Right click on project name
New – Web Service (order probably different)
74
Hello NetBeans web service:
Web Service Name: ‘HelloWorld’
Package: ‘com.javapapers.webservice’
Finish
75http://javapapers.com/web-service/soap-web-service-introduction/
Hello NetBeans web service:
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;
/** * * @author Steve */@WebService(serviceName = "Hello")public class Hello {
/** * This is a sample web service operation */ @WebMethod(operationName = "hello") public String hello(@WebParam(name = "name") String txt) { return "Hello " + txt + " !"; }}
Hello NetBeans web service:
Next step, update the .wsdl
View the current .wsdl: (works in IE, Chrome)
http://localhost:8080/HelloNetBeans/Hello?wsdl
77http://javapapers.com/web-service/soap-web-service-introduction/
Hello NetBeans web service:
78
Hello NetBeans web service:
Next step, set up ‘client’ (index.jsp)
Remove text “Hello World”
79http://javapapers.com/web-service/soap-web-service-introduction/
Hello NetBeans web service:
Right click on project name
Choose: New – ‘Web Service Client…’
80
Hello NetBeans web service:
WSDL URL: http://localhost:8080/HelloNetBeans/Hello?
wsdl
Package: com.javapapers.webserviceclient
Click ‘finish’; project rebuilt
‘localhost’ okay
81
Hello NetBeans web service:
Right click on .jsp page
Select ‘Web Service Client Resources’
Select ‘Call Web Service Operation…’
82
Hello NetBeans web service:
Select ‘Hello’ from this dialog box and ‘OK’
Expand out tree if necessary
83http://javapapers.com/web-service/soap-web-service-introduction/
Hello NetBeans web service:
Code brings in ‘Hello’ from server file
84http://javapapers.com/web-service/soap-web-service-introduction/
Hello NetBeans web service:
Update the text string:
‘Hello’ is text from XML
‘Steve’ is text from ‘index’
85http://javapapers.com/web-service/soap-web-service-introduction/
Hello NetBeans web service:
Save your files
Right click on project name, run code
86
Hello NetBeans web service:
Complete!
87http://javapapers.com/web-service/soap-web-service-introduction/
Hello NetBeans web service:
Files of interest:
index.jsp (‘client’)
HelloWorld.java (‘server’)
HelloWorld.wsdl
88http://javapapers.com/web-service/soap-web-service-introduction/
Hello NetBeans web service:
index.jsp (client):NetBeansProjects/HelloNetBeans/web/index.jsp
89
<%-- Document : index Created on : Mar 11, 2014, 10:06:36 PM Author : Steve--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1></h1> <%-- start web service invocation --%><hr/> <% 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 herejava.lang.String name = "Steve";// TODO process result herejava.lang.String result = port.hello(name);out.println("Result = "+result);
} catch (Exception ex) {// TODO handle custom exceptions here
} %> <%-- end web service invocation --%><hr/> </body></html>
Hello NetBeans web service:
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;
/** * * @author Steve */@WebService(serviceName = "Hello")public class Hello {
/** * This is a sample web service operation */ @WebMethod(operationName = "hello") public String hello(@WebParam(name = "name") String txt) { return "Hello " + txt + " !"; }}
Hello NetBeans web service:
HelloWorld.wsdl:NetBeansProjects/Wk4HelloWorld/build/web/WEB-INF/
wsdl/localhost_8080/HelloNetBeans/Hello.wsdl
91
<?xml version='1.0' encoding='UTF-8'?><!-- lots of legalese -->
<types><xsd:schema><xsd:import namespace="http://webservice.javapapers.com/" schemaLocation="http://localhost:8080/Wk4HelloWorld/HelloWorld?xsd=1"/></xsd:schema></types><message name="hello"><part name="parameters" element="tns:hello"/></message><message name="helloResponse"><part name="parameters" element="tns:helloResponse"/></message><portType name="HelloWorld"><operation name="hello"><input wsam:Action="http://webservice.javapapers.com/HelloWorld/helloRequest" message="tns:hello"/><output wsam:Action="http://webservice.javapapers.com/HelloWorld/helloResponse" message="tns:helloResponse"/></operation></portType>
Hello NetBeans web service:
HelloWorld.wsdl:NetBeansProjects/Wk4HelloWorld/build/web/WEB-INF/
wsdl/localhost_8080/Wk4HelloWorld/HelloWorld.wsdl
92
<binding name="HelloWorldPortBinding" type="tns:HelloWorld"><soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><operation name="hello"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation></binding><service name="HelloWorld"><port name="HelloWorldPort" binding="tns:HelloWorldPortBinding"><soap:address location="http://localhost:8080/Wk4HelloWorld/HelloWorld"/></port></service></definitions>
http://javapapers.com/web-service/soap-web-service-introduction/
Hello NetBeans web service:
1. SOAP messages are defined using an:
A. XML DTDB. XML SchemaC. XSLTD. XML CSS
Quiz:
93
2. XML-RPC compared to SOAP:
A. Is easier and more powerfulB. Is easier and less powerfulC. Is more complex and more powerfulD. Is more complex and less powerful
Quiz:
94
3. An XML-RPC message compared to a SOAP message:
A. The XML-RPC message is smaller and carries more dataB. The XML-RPC message is larger and cards more dataC. The SOAP message is larger and carries more dataD. The SOAP message is larger and it carries less data
Quiz:
95
4. Comparing XML-RPC to SOAP:
A. XML-RPC allows for the message to be customized B. SOAP allows for the message to be customizedC. XML-RPC is sent using HTTP only D. SOAP is sent using HTTP only
Quiz:
96
5. XML-RPC and SOAP:
A. XML-RPC can be made in NotepadB. SOAP can be made in NotepadC. Both must be made using an IDE
Quiz:
97
6. Java Server Pages (JSP):
A. Are a web services technologyB. Are a client-side technologyC. Are a server-side technologyD. Allow scripting to be mixed with HTMLE. Scripts are executed before the page is renderedF. Scripts are executed after the page is rendered
Quiz:
98
7. An application server:
A. Inserts the output from scripts into HTML contentB. Inserts the output from compiled code into HTML contentC. Executes compiled code on the client machineD. Executes scripted code on the client machine
Quiz:
99
SE 370: Programming Web Services
Week 4: SOAP & NetBeansCopyright © Steven W. Johnson
February 1, 2013
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
Create the project:
Lab: AddNumbers
http://www.roseindia.net/webservices/netbeans/Web-Service.shtml
103
Rt. click on project name – New – web service
Creates the *.java page (Name = filename)
http://www.roseindia.net/webservices/netbeans/Web-Service.shtml
Lab: AddNumbers
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;
/** * * @author Steve */@WebService(serviceName = "AddingMachine")public class AddingMachine {
/** * This is a sample web service operation */ @WebMethod(operationName = "hello") public String hello(@WebParam(name = "name") String txt) { return "Hello " + txt + " !"; }}
105
Run the .jsp file
Lab: AddNumbers
106
Now, new begins. Click on ‘Design’
Lab: AddNumbers
107
Modify ‘server’ method
@WebMethod(operationName = "hello") public String hello(@WebParam(name = "name") String txt) { return "Hello " + txt + " !"; }}
Lab: AddNumbers
108
Click ‘Add Operation…’
Lab: AddNumbers
109
Name = method name and method return type
Arguments used by the method and data types
Lab: AddNumbers
110
Method is built
@WebMethod(operationName = "Adder")public int Adder(@WebParam(name = "one") int one, @WebParam(name = "two") int two) { //TODO write your implementation code here: return 0; }
@WebMethod(operationName = "Adder")public int Adder(@WebParam(name = "one") int one, @WebParam(name = "two") int two) { //TODO write your implementation code here: return one + two; }
Lab: AddNumbers
111
Select ‘hello’ and ‘Remove Operation’
Lab: AddNumbers
112
Right click on project and deploy
http://www.roseindia.net/webservices/netbeans/Web-Service.shtml
Lab: AddNumbers
113
Test web Service
Lab: AddNumbers
114
Lab: AddNumbers
115
Rt click on project - New – Web Service Client…
Lab: AddNumbers
116
Click on Browse button for Project
Browse in wsdl
Lab: AddNumbers
117
Add a different package name at bottom
Finish
Lab: AddNumbers
118
Go to index.jsp file
Right click in Source Window,
Web Service Client – Web Service Operation
Lab: AddNumbers
119
Drill down method on .java page
OK
Lab: AddNumbers
120
Change the ‘int’ variables to values
Lab: AddNumbers
121
Delete ‘Hello World’
Save page
Right-click in window and Run File
http://www.roseindia.net/webservices/netbeans/Web-Service.shtml
Lab: AddNumbers
122
Output generated:
http://www.roseindia.net/webservices/netbeans/Web-Service.shtml
Lab: AddNumbers
123
Use text fields to get data
One text field, one int field
http://www.roseindia.net/webservices/netbeans/Web-Service.shtml
Lab: Adult
124
Create the project:
http://www.roseindia.net/webservices/netbeans/Web-Service.shtml
Lab: Adult
125
Open the Palette (Ctrl + Shift + 8)
Window – IDE Tools – Palette
Drag-and-drop support, no WYSIWYG yet
Lab: Adult
<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> </body></html>
126
Change title to “Age Status”
Delete <h1> element
Lab: Adult
<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <h1>Hello World!</h1> </body></html>
<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Age Status</title> </head> <body> </body></html>
127
Need four elements?
form
table?
2 text fields
1 submit button
Rules of HTML are the same in .jsp as .php
http://www.roseindia.net/webservices/netbeans/Web-Service.shtml
Lab: Adult
128
Lab: Adult <form name="Data" method="POST"></form><form name="Data" method="POST"> <table border="0" width="250"> <thead> <tr> <th></th> <th></th> </tr> </thead> <tbody> <tr> <td></td> <td></td> </tr> <tr> <td></td> <td></td> </tr> <tr> <td></td> <td></td> </tr> </tbody> </table></form>
Create form
Create table
129
Lab: Adult
<tr> <td></td> <td><input type="text" name="Name" value="" /></td> </tr> <tr> <td></td> <td><input type="text" name="Age" value="" /></td> </tr> <tr> <td></td> <td></td> </tr> </tbody> </table></form>
Add text fields:
130
Lab: Adult
<tr> <td>Name:</td> <td><input type="text" name="Name" value="" /></td> </tr> <tr> <td>Age:</td> <td><input type="text" name="Age" value="" /></td> </tr> <tr> <td></td> <td></td> </tr> </tbody> </table></form>
Add labels: (manually enter)
131
Lab: Adult
<tr> <td>Name:</td> <td><input type="text" name="Name" value="" /></td> </tr> <tr> <td>Age:</td> <td><input type="text" name="Age" value="" /></td> </tr> <tr> <td></td> <td><input type="submit" value="Check" name="Submit" /></td> </tr> </tbody> </table></form>
Add submit button:
132
Lab: AdultCheck work: save and run the page
Names of elements shown on page
NameAge
Submit
133
Lab: AdultBuild the web service
Rt. Click on project name – New – Web Service
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;
/** * * @author Steve */@WebService(serviceName = "AdultMinor")public class AdultMinor {
/** * This is a sample web service operation */ @WebMethod(operationName = "hello") public String hello(@WebParam(name = "name") String txt) { return "Hello " + txt + " !"; }}
AdultMinor.java
135
Update the web service method
Lab: Adult
136
Click ‘Add Operation…’
Lab: Adult
137
Name = method name and method return type
Arguments used by the method and data types
Lab: Adult
138
Select ‘hello’ and ‘Remove Operation’
Lab: Adult
139
Right click on project and deploy
http://www.roseindia.net/webservices/netbeans/Web-Service.shtml
Lab: Adult
140
Re-run index.java
http://www.roseindia.net/webservices/netbeans/Web-Service.shtml
Lab: Adult
141
AdultMinor.java:
Lab: Adult
package com.steve.johnson.adult;
import javax.jws.WebService;import javax.jws.WebMethod;import javax.jws.WebParam;
/** * * @author Steve */@WebService(serviceName = "AdultMinor")public class AdultMinor {
/** * Web service operation */ @WebMethod(operationName = "AgeTest") public String AgeTest(@WebParam(name = "Name") String Name, @WebParam(name = "Age") int Age) { //TODO write your implementation code here: return null; }}
142
Fix the ‘AgeTest’ method:
Lab: Adult
/** * Web service operation */ @WebMethod(operationName = "AgeTest") public String AgeTest(@WebParam(name = "Name") String Name, @WebParam(name = "Age") int Age) { if (Age < 18) {return Name + " is a minor";} else {return Name + " is an adult";} }}
143
Save .java file and test web service
Lab: Adult
144
Save .java file and test web service
Lab: Adult
145
Save .java file and test web service
Lab: Adult
146
Rt click on project - New – Web Service Client…
Lab: Adult
147
Click on Browse button for Project
browse in wsdl
add a different package name
Lab: Adult
148
Go to index.jsp file
Right click in Source Window,
Web Service Client – Web Service Operation
Lab: Adult
149
Lab: AdultConnect the form to the web service
150
Lab: AdultExtract text field values
151
Lab: AdultNo ‘isset’ for the submit button
Save ‘index.jsp’
Run
Break 152
Create a web service that finds the average offive 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
SE 370: Programming Web Services
Week 4: SOAP & NetBeansCopyright © Steven W. Johnson
February 1, 2013