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
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 Consumer Producer
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?
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 Java Function in C
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)
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
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 Proxy classes Servere Web Service method SOAP
XML Format Simple example: web service to compute k = i + j
REST Services Alternative: REST (Representational State Transfer) –Information transmitted in JSON (JavaScript Object Notation) –Can be directly consumed by JavaScript Good if AJAX used
Creating a Web Service in NetBeans Reference: 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
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
Adding a Web Service Operation Note that NetBeans can handle complex parameter classes –Must implement serializable interface
Adding a Web Service Operation Source code will now contain “skeleton” of method Modify as necessary
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
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
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
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