18
CSCI 6962: Server-side Design and Programming Web Services

CSCI 6962: Server-side Design and Programming Web Services

Embed Size (px)

Citation preview

Page 1: CSCI 6962: Server-side Design and Programming Web Services

CSCI 6962: Server-side Design and Programming

Web Services

Page 2: CSCI 6962: Server-side Design and Programming Web Services

Remote Function Calls

• Function calls:– Caller passes parameters to function– Function returns value

• Advantages– Much cleaner syntax than passing parameters in request

public int add (int a, int b) { return a + b; }

Function

public int whatever () { … x = 7; y = 5; z = add(x, y); … }

Call

Page 3: CSCI 6962: Server-side Design and Programming Web Services

Remote Function Calls

Key idea:• Function call should still work even if caller, callee at different

locations in network• Example: RMI for Java (Remote Method Invocation)

– Passed “serializable” objects with built-in methods to convert to/from string

public int add (int a, int b) { return a + b; }

public int whatever () { … z = add(x, y); … }

Call over network

ConsumerProducer

Page 4: CSCI 6962: Server-side Design and Programming Web Services

Remote Function Calls• Problem: Passing complex parameters over network

– Information sent as strings over network

public int add (int a, int b) { return a + b; }

public void whatever () { x = 7; y = 5; z = add(x, y); }

Easy to pass 5 and 7 as strings

public void storeCart (ShoppingCart c) { … }

public void whatever () { storeCart(myCart) } What does a

“ShoppingCart” look like when sent over network?

Page 5: CSCI 6962: Server-side Design and Programming Web Services

Remote Method Call Problems

• Requires client and server to use same language– Should be more flexible

• Remote calls often seen as attacks by firewalls– Html preferable for sending information

int add (int a, int b) { return a + b; }

whatever () { cin >> x >> y; z = add(x, y); }

Method in JavaFunction in C

Page 6: CSCI 6962: Server-side Design and Programming Web Services

Web Services Solution

• Simulate remote method calls with existing client/server structure– Method call = request– Return value = response– Not considered attack by firewall– Also supports https: for secure transmissions

• Common standards:– SOAP (Simple Object Access Protocol)– REST (Representational State Transfer)

Page 7: CSCI 6962: Server-side Design and Programming Web Services

Web Services Solution

• Simulate remote method calls with existing client/server structure– Method call = request– Return value = response– Not considered attack by firewall– Also supports https: for secure transmissions

• Format of calls based on XML – Common language specification for internet

Page 8: CSCI 6962: Server-side Design and Programming Web Services

SOAP

• Format of calls based on XML – Common language specification for internet– Generally requires proxy classes in client to translate

objects into XML format• JAX-WS libraries for Java• WCF (Windows Communication Foundation) libraries in ASP

Cliente

Client code

Proxyclasses

Servere

Web Service

methodSOAP

Page 9: CSCI 6962: Server-side Design and Programming Web Services

XML Format• Simple example: web service to compute k = i + j

Page 10: CSCI 6962: Server-side Design and Programming Web Services

REST Services

• Alternative: REST (Representational State Transfer)– Information transmitted in JSON (JavaScript Object Notation)– Can be directly consumed

by JavaScript• Good if AJAX used

Page 11: CSCI 6962: Server-side Design and Programming Web Services

Creating a Web Service in NetBeans

• Reference: https://netbeans.org/kb/docs/websvc/jax-ws.html#extschema

• Will probably need to modify configuration files of IDE/Glassfish

• Create new web site project• Add new web service to the project

– Must have a package in URL format (fake address of org.me.greeter)

• Added to project as web service node

Page 12: CSCI 6962: Server-side Design and Programming Web Services

Adding a Web Service Operation• Right click service node and choose Add Operation• Will be prompted for:

– Return type– Types and names of

parameters • New operation added as

node to project

Page 13: CSCI 6962: Server-side Design and Programming Web Services

Adding a Web Service Operation• Note that NetBeans can handle complex parameter classes

– Must implement serializable interface

Page 14: CSCI 6962: Server-side Design and Programming Web Services

Adding a Web Service Operation• Source code will now contain “skeleton” of method• Modify as necessary

Page 15: CSCI 6962: Server-side Design and Programming Web Services

Testing a Web Service Operation• Right click project and deploy• Right click web service and choose Test Web Service• Will get “test window” for each method to enter parameters

• Can view return value and underlying XML

Page 16: CSCI 6962: Server-side Design and Programming Web Services

Creating a Web Client• Create as Java application• Add new Web Service Client

– Will be prompted for the web service in projects

• New (empty) method automatically generated in source packages

Page 17: CSCI 6962: Server-side Design and Programming Web Services

Creating a Web Client• Expand Web Service References

– Will have nodes for all operations in the corresponding web service• Drag node of operation to call into the class

Page 18: CSCI 6962: Server-side Design and Programming Web Services

Creating a Web Client• Add code (probably in main) to call that method with desired

parameters

• The method then invokes the corresponding method in the web service