99
20 Maart 2006 ISS - Internet Applicatio ns Part 2 1 Internet Applications part 2 René de Vries Based on slides by M.L. Liu and Marko van Eekelen

Internet Applications part 2

  • Upload
    abia

  • View
    31

  • Download
    0

Embed Size (px)

DESCRIPTION

Internet Applications part 2. Ren é de Vries. Based on slides by M.L. Liu and Marko van Eekelen. Overview. Applets Servlets Web Services SOAP References, Exercises. 1. Applets. Introduction. Three kinds of Java programs: - PowerPoint PPT Presentation

Citation preview

Page 1: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 1

Internet Applicationspart 2

René de Vries

Based on slides by M.L. Liu and Marko van Eekelen

Page 2: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 2

Overview

1. Applets

2. Servlets

3. Web Services

4. SOAP

References, Exercises

Page 3: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 3

1. Applets

Page 4: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 4

Introduction

Three kinds of Java programs:– An application is a standalone program that

can be invoked from the command line.– An applet is a program that runs in the

context of a browser session.– A servlet is a program that is invoked on

demand on a server program and that runs in the context of a web server process.

Page 5: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 5

Applets, web page, client, server• Applets are programs stored on a web server, similar to web pages.

• When an applet is referred to in a web page that has been fetched and processed by a browser, the browser generates a request to fetch (or download) the applet program, then executes the program in the browser’s execution context, on the client host.

< a pple t co de = H e llo W o rld.c la s s < /a pple t>......

H e llo W o rld.c la s s

s e r ve r ho s t

we b s e rv e r

m y W e bPa g e .h tm l

br o w s e r ho s t

bro ws e r re qe u s t fo rm y W e bPa g e .h tm l

m y W e bPa g e .h tm l

re qu e s t fo rH e llo W o rldcla s s

H e llo W o rld.c la s s

H e llo W o rld.c la s s

Page 6: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 6

Java Applet Execution - 1

• An applet program is written as a subclass of the java.Applet class or the javax.swing.Japplet class.

• There is no main method: you must override the start method.

• Applet objects uses AWT for graphics. JApplet uses SWING.

• An applet is a Graphics object that runs in a Thread object, so every applet can perform graphics, and runs in parallel to the browser process.

Page 7: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 7

Java Applet Execution - 2

• When the applet is loaded, these methods are automatically invoked in order:– The init( ) method is invoked by the Java Virtual Machine.– The start( ) method – The paint( ) method.

• The applet is now running and rendered on the web page.• You program the start( ) method and the paint( ) method for

your application, and invoke a repaint call to re-render the graphics, if necessary.

• At the end of the execution, the stop( ) method is invoked, followed by the destroy( ) method to deallocate the applet’s resources.

Page 8: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 8

Applet Security http://java.sun.com/docs/books/tutorial/applet/overview/

For security reasons, applets that are loaded over the network have several restrictions. – an applet cannot ordinarily read or write

files on the computer that it's executing on

– an applet cannot make network connections except to the host that it came from

Page 9: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 9

Advanced Applets• You can use threads in an applet.

• You can make socket calls in an applet, subject to the security constraints.Se r ve r ho s t C l i e nt ho s t

H TTP s e rv e r bro ws e r

a pple t

H o s t X

ap p le t d o w n lo ad

c o n n ec tio n r eq u es t

c o n n ec tio n r eq u es tf o r b id d en

allo w eds er v er Y

s er v er Z

Page 10: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 10

Proxy serverA proxy server can be used to circumvent the security

constraints.Se r ve r ho s t C l i e nt ho s t

H TTP s e rv e r bro ws e r

a pple t

H o s t X

ap p le t d o w n lo ad

c o n n ec tio n r eq u es ts er v er Y

s er v er Zc o n n ec tio n r eq u es t

Page 11: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 11

HTML tags for applets - 1

<APPLET specifies the beginning of the HTML applet code

CODE="demoxx.class" is the actual name of the applet (usually a 'class' file)

CODEBASE="demos/" is the location of the applet (relative as here, or a full URL)

NAME="smily" the name you want to give to this instance of the applet on this page

WIDTH="100" the physical width of the applet on your page HEIGHT="50"  the physical height of the applet on your

page ALIGN="Top" where to align the applet within its page space

(top, bottom, center)

Page 12: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 12

HTML tags for applets - 2

<PARAM  specifies a parameter that can be passed to the applet (applet specific)

NAME=“name1" the name known internally by the applet in order to receive this parameter

VALUE="000000" the value you want to pass for this parameter

> end of this parameter

</APPLET> specifies the end of the HTML applet code

Page 13: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 13

The HelloWorld Applet<HTML><BODY><APPLET code=hello.class width=900

height=300></APPLET></BODY></HTML>

// applet to display a message in a window

import java.awt.*;import java.applet.*;

public class hello extends Applet{public void init( ){

setBackground(Color.yellow);}

public void paint(Graphics g){ final int FONT_SIZE = 42;

Font font = new Font("Serif", Font.BOLD, FONT_SIZE);

// set font, and color and display message on// the screen at position 250,150

g.setFont(font);g.setColor(Color.blue);

// The message in the next line is the one you will see

g.drawString("Hello, world!",250,150);}

}

Page 14: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 14

Applets Summary

• An applet is a Java class • Its code is downloaded from a web server • It is run in the browser’s environment on the client host • It is invoked by the browser when it scans a web page

and encounters a class specified with the APPLET tag • For security reasons, the execution of an applet is

normally subject to restrictions:– applets cannot access files in the file system on the

client host – applets cannot make network connection except to

the server host from which it originated

Page 15: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 15

2. Servlets

Page 16: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 16

Introduction

• Servlets are modules that extend request/response-oriented servers, such as Java-enabled web servers.

• Servlets can be embedded in many different servers because the servlet API, which you use to write servlets, assumes nothing about the server's environment or protocol.

• Servlets are portable.

Page 17: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 17

Java Servlet Basics

• A servlet is an object of the javax.servlet class, which is not part of the JDK.

• A servlet runs inside a Java Virtual Machine (JVM) on a server host.

• A servlet is an object. It is loaded and runs in an object called a servlet engine, or a servlet container.

Page 18: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 18

Uses for Servletshttp://java.sun.com/docs/books/tutorial/servlets/overview/index.html

• Providing the functionalities of CGI scripts with a better API and enhanced capabilities.

• Allowing collaboration between people. A servlet can handle multiple requests concurrently, and can synchronize requests. This allows servlets to support systems such as on-line conferencing.  

• Forwarding requests. Servlets can forward requests to other servers and servlets. Thus servlets can be used to balance load among several servers that mirror the same content, and to partition a single logical service over several servers, according to task type or organizational boundaries.

Page 19: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 19

The Servlet class

• The Servlet class is not part of the Java Development Kit (JDK). There are many packages that support servlets, including– The JSWDK (Java Server Web Development Kit) – the API

documentation can also be downloaded from the same site – freeware provided by Sun for demonstration of the technology, downloadable from http://java.sun.com/products/servlet/archive.htmlhttp://java.sun.com/products/servlet/download.html

– The Tomcat : a free, open-source implementation of Java Servlet and JavaServer Pages technologies developed under the Jakarta project at the Apache Software Foundation.

– Application servers such as WebLogic, iPlanet, WebSphere.

Page 20: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 20

The architecture for servlet support

serv let1 c o de

s e rv le t e n g in eo r s e rv le t co n ta in e r

C lie n t1S e rv e r1

serv let2 co de

C lie n t2S e rv e r2

C lie n t3

serv let3 co de

A s e rv le t co n ta in e r o r s e rv le t e n g in e is re qu ire d.Th e s e rv le t e n g in e ca n be pa rt o f a s e rv e r, o r am o du le e x te rn a l to a s e rv e r.

Page 21: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 21

The Life Cycle of an HTTP Servlet

• The web server loads a servlet when it is called for in a web page.

• The web server invokes the init( ) method of the servlet.

• The servlet handles client responses.• The server destroys the servlet (at the

request of the system administrator). A servlet is normally not destroyed once it is loaded.

Page 22: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 22

The Servlet Life Cycle http://java.sun.com/docs/books/tutorial/servlets/lifecycle/index.html

serv let c o de

s e rv le t e n g in e

C lie n t

s e rv e r lo a ds s e rv le t co de a n d in it ia lize s a s e rv le t , po s s ibly a s a re s u lt o f a c lie n t 's re qu e s t

S e rv e r

serv let c o de

s e rv le t e n g in eC lie n t

S e rv e r

C lie n tV ia th e s e rv e r, th e s e rv le t h a n dle s ze ro o r m o re clie n t re qu e s t s

Th e s e rv e r re m o v e s th e s e rv le t wh e n th e re is n o m o re clie n t re qu e s t fo r it .( s o m e s e rv e rs do th is s t e p o n ly wh e n th e y s h u t do wn )

serv let c o de

s e rv le t e n g in e

S e rv e r

Page 23: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 23

A simplified sequence diagram

clie n t2 s e rv e rs e rv le tco n ta in e r

s e rv le t

in it ( )

s e rv ice ( )

c lie n t 1

H TTP re s po n s e

H TTP re qu e s t

s e rv ice ( )H TTP re qu e s t

t e rm in a tede s tro y ( )

lo a ds s e rv le t

H TTP re s po n s e

s h u t do wn

Page 24: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 24

The Servlet Interface

S e rv le t s

g e n e ric s e rv le t s

H TTP s e rv le t s

y o u r s e rv le t

" Th e ce n tra l a bs tra ct io n in th eS e rv le t A PI is a S e rv le t in te rfa ce .A ll s e rv le t s im ple m e n t th isin te rfa ce e ith e r dire ct ly o r, m o reco m m o n ly , by e x te n din g a cla s sth a t im ple m e n ts it s u ch a s H TTPS e rv le t

Th e S e rv le t in te rfa ce de cla re s , bu t do e s n o t im ple m e n t , m e th o ds th a t m a n a g eth e s e rv le t a n d it s co m m u n ica t io n s with clie n t s .S e rv le t writ e rs pro v ide s o m e o r a ll o f th e s em e th o ds wh e n de v e lo pin g a s e rv le t ."

h t t p : //j a va .s u n .c o m /d o c s /b o o k s /t u t o r i a l /s e r vl e t s /

Page 25: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 25

Generic Servlets and HTTP ServletsJava Servlet Programming, O’Reilley Press

• Every servlet must implement the javax.servlet.Servlet interface

• Most servlets implement the interface by extending one of these classes– javax.servlet.GenericServlet: most general– javax.servlet.http.HttpServlet: an extension of HTTP

servers.

• A generic servlet should override the service( ) method to process requests and generate appropriate responses.

• An HTTP servlet overrides the doPost( ) and/or doGet( ) method.

Page 26: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 26

Generic and HTTP Servlets

G en er ic S er v le t

s er v ic e ( )

S er v erC lien t

r eq u es t

r es p o n s e

HT T P S er v le t

s er v ic e ( )

HT T P S er v erBr o w s er

r eq u es t

r es p o n s ed o G et ( )

d o P o s t( )

Page 27: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 27

A simple Servlet, from http://java.sun.com/docs/books/tutorial/servlets/overview/simple.html

public class SimpleServlet extends HttpServlet { /** * Handle the HTTP GET method by building a simple web page. */

public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

PrintWriter out; String title = "Simple Servlet Output"; // set content type and other response header fields first response.setContentType("text/html"); // then write the data of the response out =

response.getWriter(); out.println("<HTML><HEAD><TITLE>"); out.println(title); out.println("</TITLE></HEAD><BODY>"); out.println("<H1>" + title + "</H1>"); out.println("<P>This is output from SimpleServlet."); out.println("</BODY></HTML>"); out.close(); } //end method

} // end class

Page 28: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 28

Using HTTP Servlet to process web formshttp://java.sun.com/docs/books/tutorial/servlets/client-interaction/req-res.html

Requests and Responses Methods in the HttpServlet class that handle client requests

take two arguments: – An HttpServletRequest object, which encapsulates

the data from the client   – An HttpServletResponse object, which

encapsulates the response to the client

The methods for handling HTTP requests and responses are:

– public void doGet (HttpServletRequest request, HttpServletResponse response) for requests sent using the GET method.

– public void doPost(HttpServletRequest request, HttpServletResponse response) for requests sent using the POST method.

Page 29: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 29

HttpServletRequest Objectshttp://java.sun.com/docs/books/tutorial/servlets/client-interaction/req-res.html

An HttpServletRequest object – provides access to HTTP header data, such as any cookies

found in the request and the HTTP method with which the request was made.

– allows you to obtain the arguments that the client sent as part of the request.

To access client data sent with an HTTP request:   • The getQueryString method returns the query

string. • The getParameter method returns the value of a

named parameter.• The getParameterValues method returns an

array of values for the named parameter.

Page 30: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 30

HttpServletRequest Interface

• public String ServletRequest.getQueryString( ); returns the query string of the request.

• public String GetParameter(String name): given the name of a parameter in the query string of the request, this method returns the value.

• public String[ ] GetParameterValues(String name): returns multiple values for the named parameter – use for parameters which may have multiple values, such as from checkboxes.

• public Enumeration getParameterNames( ): returns an enumeration object with a list of all of the parameter names in the query string of the request.

Page 31: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 31

HttpServletResponse Objectshttp://java.sun.com/docs/books/tutorial/servlets/client-interaction/req-res.html

• An HttpServletResponse object provides two ways of returning data to the user:   – The getWriter method returns a Writer   – The getOutputStream method

returns a ServletOutputStream  

• Use the getWriter method to return text data to the user, and the getOutputStream method for binary data.

• Closing the Writer or ServletOutputStream after you send the response allows the server to know when the response is complete.

Page 32: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 32

HttpServletResponse Interface• public interface HttpServletResponse extends

ServletResponse: “The servlet engine provides an object that implements this interface and passes it into the servlet through the service method” – “Java Server Programming”

• public void setContentType(String type) : this method must be called to generate the first line of the HTTP response:

• public PrintWriter getWriter( ) throws IOException: returns an object which can be used for writing the responses, one line at a time using out.println().

Page 33: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 33

Servlets are Concurrent http://java.sun.com/docs/books/tutorial/servlets/client-interaction/req-res.html

HTTP servlets are typically capable of serving multiple clients concurrently: the servlet engine uses a separate thread to invoke each method.

Hence servlet methods should be thread-safe: If the methods in your servlet do work for clients by accessing a shared resource, then you must either:   – Synchronize access to that resource, or   – Ensure that the servlet can handle only one

client request at a time (by using semaphores or locks). 

Page 34: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 34

Handling GET requestshttp://java.sun.com/docs/books/tutorial/servlets/client-interaction/http-

methods.html

public class BookDetailServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException,

IOException { ... // set content-type header before accessing the Writer

response.setContentType("text/html"); PrintWriter out = response.getWriter();  // then write the response out.println("<html>" + "<head><title>Book Description</title></head>" + ... ); 

//Get the identifier of the book to display String bookId = request.getParameter("bookId"); if (bookId != null) { // fetch the information about the book and print it ... }

out.println("</body></html>"); out.close(); } ...}

Page 35: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 35

Handling POST Requestshttp://java.sun.com/docs/books/tutorial/servlets/client-interaction/http-

methods.html

public class ReceiptServlet extends HttpServlet { public void doPost(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

... // set content type header before accessing the Writer

response.setContentType("text/html"); PrintWriter out = response.getWriter( ); // then write the response out.println("<html>" + "<head><title> Receipt </title>"

+ ...); out.println("Thank you for purchasing your books from us " +

request.getParameter("cardname") + ...); out.close(); } ... }

Page 36: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 36

Servlet Examples

See Servlet\simple folder in code sample:• HelloWorld.java: a simple servlet• Counter.java: illustrates that a servlet is persistent• Counter2.java: illustrates the use of synchronized

method with a servlet• GetForm.html, GetForm.java: illustrates the

processing of data sent with an HTTP request via the GET method

• PostForm.html, PostForm.java: illustrates the processing of data sent with an HTTP request via the POST method

Page 37: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 37

Session State Information

• The mechanisms for state information maintenance with CGI can also be used for servlets: hidden-tag, URL suffix, file/database, cookies.

• In addition, a session tracking mechanism is provided, using an HttpSession object.

Page 38: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 38

Cookies in Java http://java.sun.com/products/servlet/2.2/javadoc/index.html

• A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number. Some Web browsers have bugs in how they handle the optional attributes, so use them sparingly to improve the interoperability of your servlets.

• The servlet sends cookies to the browser by using the HttpServletResponse.addCookie(javax.servelet.http.Cookie) method, which adds fields to HTTP response headers to send cookies to the browser, one at a time. The browser is expected to support 20 cookies for each Web server, 300 cookies total, and may limit cookie size to 4 KB each.

• The browser returns cookies to the servlet by adding fields to HTTP request headers. Cookies can be retrieved from a request by using the HttpServletRequest.getCookies( ) method. Several cookies might have the same name but different path attributes.

Page 39: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 39

Processing Cookies with JavaJava Server Programming – Wrox press

• A cookie is an object of the javax.servlet.http.cookie class.• Methods to use with a cookie object:• public Cookie(String name, String value): creates a cookie

with the name-value pair in the arguments.• import javax.servlet.http.*• Cookie oreo = new Cookie(“id”,”12345”);

• public string getName( ) : returns the name of the cookie• public string getValue( ) : returns the value of the cookie• public void setValue(String _val) : sets the value of the

cookie• public void setMaxAge(int expiry) : sets the

maximum age of the cookie in seconds.

Page 40: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 40

Processing Cookies with Java – 2Java Server Programming – Wrox press

• public void setPath(java.lang.String uri) : Specifies a path for the cookie to which the client should return the cookie. The cookie is visible to all the pages in the directory you specify, and all the pages in that directory's subdirectories. A cookie's path must include the servlet that set the cookie, for example, /catalog, which makes the cookie visible to all directories on the server under /catalog.

• public java.lang.String getPath() : Returns the path on the server to which the browser returns this cookie. The cookie is visible to all subpaths on the server.

• public String getDomain( ) : returns the domain of the cookie.

• if orea.getDomain.equals(“.foo.com”)

• … // do something related to golf

• public void setDomain(String _domain): sets the cookie’s domain.

Page 41: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 41

doGet/doPost Method using cookies

Public void doGet(HttpServletResponse req, HttpServletResponse res)

throws ServletException, IOExceiption{

res.setContentType(“text/html”);

PrintWriter out = res.getWriter( );

out.println (“<H1>Contents of your shopping cart:</H1>”);

Cookie cookies[ ];

cookies = req.getCookies( );

if (cookies != null) {

for ( int i = 0; i < cookies.length; i++ ) {

if (cookies[i].getName( ).startWith(“Item”))

out.println( cookies[i].getName( ) + “: “ + cookies[i].getValue( ));

out.close( );

}

Page 42: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 42

Servlet & Cookies Example

See Servlet\cookies folder in code sample:• Cart.html: web page to allow selection of items• Cart.java: Servlet invoked by Cart.html; it

instantiates a cookie object for each items selected.

• Cart2.html: web page to allow viewing of items currently in cart

• Cart2.java: Servlet to scan cookies received with the HTTP request and display the contents of each cookie.

Page 43: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 43

HTTP Session Objectshttp://java.sun.com/products/servlet/2.2/javadoc/index.html

• The javax.servlet.http package provides apublic interface HttpSession: Provides a way to identify a user across more than one page request or visit to a Web site and to store information about that user.

• The servlet container uses this interface to create a session between an HTTP client and an HTTP server. The session persists for a specified time period, across more than one connection or page request from the user. A session usually corresponds to one user, who may visit a site many times.

Page 44: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 44

HTTP Session Object - 2http://java.sun.com/products/servlet/2.2/javadoc/index.html

• This interface allows servlets to – View and manipulate information about a session, such

as the session identifier, creation time, and last accessed time

– Bind objects to sessions, allowing user information to persist across multiple user connections

• Session object allows session state information to be maintained without depending on the use of cookies (which can be disabled by a browser user.)

• Session information is scoped only to the current web application (ServletContext), so information stored in one context will not be directly visible in another.

Page 45: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 45

The Session objectA S e s s io n o bje c t

s e rve le t e ngine

we b s e rve r

Se r ve r ho s t

C l i e nt ho s t

r e que st /r e sp o n se

s er v le t

Page 46: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 46

Obtaining an HTTPSession ObjectA session object is obtained using the getSession( ) method of the

HttpServletRequest object (from doPost or doGet)public HTTPSession getSession(boolean create): Returns the

current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session. If create is false and the request has no valid HttpSession, this method returns null.

To make sure the session is properly maintained, you must call this method before the response is committed.

public class ShoppingCart extends HttpServlet { public void doPost(HttpServletRequest req, HttpServletRespnse

res) throws ServletException, IOException … // get session object HttpSession session = req.getSession(true) if (session != null) { … } …

Page 47: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 47

The HTTPSession Object

• public java.lang.String getId( ): returns a string containing the unique identifier assigned to this session. The identifier is assigned by the servlet container and is implementation dependent.

• public java.lang.Object getAttribute(java.lang.String name): returns the object bound with the specified name in this session, or null if no object is bound under the name.

• public java.util.Enumeration getAttributeNames( ): returns an Enumeration of String objects containing the names of all the objects bound to this session.

• public void removeAttribute(java.lang.String name): removes the object bound with the specified name from this session. If the session does not have an object bound with the specified name, this method does nothing.

Page 48: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 48

Session Object examplebased on an example in “Java Server Programming”, Wrox Press

// Get item count from session object Integer itemCount = (Integer)session.getAttribute(“itemCount); // if this is the first call during the session, no attribute exists if (itemCount == null) { itemCount = new Integer(0); // process form data String[ ] itemSelected; String itemName; itemSelected = req.getParameterValues(“item”); // if user has selected items on form, add them to the session object if (itemSelected != null) { for ( int i=0; i < itemSelected.length; i ++) { itemName = itemSelected[I]; itemCount = new Integer(itemCount.intValue( ) +1); session.setAttribue(“Item” + itemCount, itemName); }// end for session.setAttribtue(“itemCount”, itemCount); } // end if

Page 49: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 49

Session object example - continued

PrintWriter out = res.getWriter( );

res.setContentType(“text/html”);

// … output HTML for web page heading

// Output current contents of the shopping cart

out.println(“<H1>Current Shopping Cart Contents</H1>”);

for (int i = 1; i <= ItemCount.intValue( ); i++) (

// retrieve objects from the session object

String item = (String) session.getAttribute(“”Item” + i);

out.println(“item<br>”);

}

// … output HTML for web page footing

out.close( );

Page 50: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 50

Session Object example

See Servlet\Session folder in code sample:• Cart.html: web page to allow selection of items• Cart.java: Servlet invoked by Cart.html; it

instantiates a session object which contains descriptions of items selected.

• Cart2.html: web page to allow viewing of items currently in cart

• Cart2.java: Servlet to display items in the shopping cart, as recorded by the use a session object in the Cart servlet

Page 51: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 51

Servlets Summary - 1

• A servlet is a Java class.

• Its code is loaded to a servlet container on the server host.

• It is initiated by the server in response to a client’s request.

• Once loaded, a servlet is persistent.

Page 52: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 52

Servlets Summary - 2

• For state information maintenance:– hidden form fields – cookies – the servlet’s instance variables may hold

global data – a session object can be used to hold session

data

Page 53: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 53

3. Web Services

Page 54: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 54

Introduction Web Services

• Network services provided over HTTP – “wired services”

• It is promoted as a new way to build network applications from distributed components that are language- and platform-independent

• The technologies are still evolving• We are more interested in the concept and

principles, but we will look into one API (Apache SOAP).

Page 55: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 55

Web Services

Th e we b(H TTP-ba s e d

n e two rk )

we b s e rv ice

we b s e rv ice

we b s e rv ice

clie n t

Page 56: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 56

Web Service Software Components

• A web service is a message-based network service.

• Messages are sent via a remote procedure call mechanism using SOAP for the parameter format.

a pplica t io nlo g ic

s e rv icepro x y

s e rv icelis t e n e r

s e rv icere qu e s t

Page 57: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 57

Just-in-time integration• Network services can be integrated

dynamically, on an as-needed basis.• SunMicro’s jini is a framework that

supports the idea.• Network services are registered with a

service registry; a service consumer/client looks up the registry to fulfill its needs.

• The binding of a client to the service can occur at runtime.

Page 58: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 58

Web service protocol stack

t ra n s po rt

n e two rk

m e s s a g in g

s e rv ice de s cript io n

s e rv ice dis co v e ry

tra n s po rt

n e two rk

m e s s a g in g

s e rv ice de s cript io n

s e rv ice dis co v e ry

a pplica t io n a pplica t io n

Page 59: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 59

Web service protocols

t ra n s po rt

n e two rk

m e s s a g in g

s e rv ice de s cript io n

s e rv ice dis co v e ry

a pplica t io n

UD D I (Un iv e rs a l D e s cript io n , D is co v e ry , a n d I n te g ra t io n )

W S D L (W e b S e rv ice D e s cript io n L a n g u a g e )

X M L , S O A P (S im ple O bje ct A cce s s Pro to co l)

TC P, H TTP, S M TP, J a bbe r

I P

Page 60: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 60

Webservices Summary

• A web service is a message-based network service.

• Web services can be integrated dynamically, on an as-needed basis.

• Network services are registered with a service registry.

• Webservices use a remote procedure protocol over HTTP using SOAP messages.

Page 61: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 61

4. SOAP

Page 62: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 62

SOAP

• SOAP is a protocol which applies XML for message exchange in support of remote method calls over the Internet.

• Compared to remote method invocation or CORBA-based facilities: – SOAP is web-based or “wired” and hence is

not subject to firewall restrictions– Language-independent– Can provide just-in-time service integration

Page 63: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 63

Introduction“SOAP is a Remote Procedure Calling protocol that works over HTTP (and TCP/SMTP etc.).

The body of the request is in XML. A procedure executes on the server and the value it returns is also formatted in XML.

Procedure parameters and returned values can be scalars, numbers, strings, dates, etc.; and can also be complex record and list structures.”

(- A Busy Developer’s Guide To Soap1.1)

Page 64: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 64

Remote Procedure Call using HTTP

we bs e rv e r

we bclie n t

s e rvi ceobje ct

H TTP re qu e s t

H TTP re s po n s e

m e t h o d n a m e ,p a r a m e t e r l i s t

r e t u r n va l u e

Page 65: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 65

SOAP Messages

m e s s a g e bo dy

S O A P bo dy

h e a de r blo ck

h e a de r blo ck

S O A P h e a de r

S O A P e n v e lo pe

op

tio

na

lre

qu

ired

Page 66: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 66

An XML-encoded SOAP RPC

< s o a p:En v e lo pe x m ln s : s o a p= 'h t tp: //www.w3 .o rg /2 0 0 1 /1 0 /s o a p-e n v e lo pe '> < s o a p:H e a de r>

< - - H e a de rs g o h e re - ->

< /s o a p:H e a de r> < s o a p:B o dy >

< - - R e qu e s t g o e s h e re - ->

< /s o a p:B o dy >< /s o a p:En v e lo pe >

S o u rce : h t tp: // www.x m l.co m /

Page 67: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 67

Example of a SOAP message(xml)

< s o a p:En v e lo pe x m ln s : s o a p= 'h t tp: //www.w3 .o rg /2 0 0 1 /1 0 /s o a p-e n v e lo pe '>< s o a p:H e a de r>

< h :L o g x m ln s :h = 'h t tp: //e x a m ple .o rg /cv s /lo g g in g '> < tra ce > 4 < /tra ce > < /h :L o g > < h : PS e rv e r x m ln s :h = 'h t tp: //e x a m ple .o rg /cv s /ps e rv e r' s o a p: m u s tUn de rs ta n d= 'tru e ' > < u s e rn a m e > a n o n cv s @ e x a m ple .o rg < /u s e rn a m e > < pa s s wo rd> a n o n cv s < /pa s s wo rd> < /h : PS e rv e r> < /s o a p:H e a de r>

< s o a p:B o dy > < m :C h e ck o u t x m ln s :m = 'h t tp: //e x a m ple .o rg /cv s '> < s o u rce > /x m l/s o a p/we bs e rv ice s /cv s < /s o u rce > < re v is io n > 1 .1 < /re v is io n > < de s t in a t io n > /e tc/u s r/m a rt in g /s o u rce /x m l< /de s t in a t io n > < /m :C h e ck o u t> < /s o a p:B o dy >< /s o a p:En v e lo pe >

s o u rce : h t tp: //www.x m l.co m

Page 68: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 68

A SOAP Message that contains a remote procedure callsource: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle= "http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"

xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">

<SOAP-ENV:Body>       <m:getStateName xmlns:m="http://www.soapware.org/">         <statenum xsi:type="xsd:int">41</statenum>         </m:getStateName> </SOAP-ENV:Body>

</SOAP-ENV:Envelope>

Page 69: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 69

An Example SOAP Requestsource: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1

< ? x m l v e rs io n = " 1 .0 " ? >< S O A P-ENV :En v e lo pe S O A P- ENV :e n co din g S ty le = " h t tp: //s ch e m a s .x m ls o a p.o rg /s o a p/e n co din g /"x m ln s :S O A P- ENC = " h t tp: //s ch e m a s .x m ls o a p.o rg /s o a p/e n co din g /"x m ln s :S O A P- ENV = " h t tp: //s ch e m a s .x m ls o a p.o rg /s o a p/e n v e lo pe /"x m ln s :x s d= " h t tp: //www.w3 .o rg /1 9 9 9 /X M L S ch e m a "x m ln s :x s i= " h t tp: //www.w3 .o rg /1 9 9 9 /X M L S ch e m a - in s ta n ce " >

< S O A P- ENV :B o dy > < m :g e tS ta te Na m e x m ln s :m = " h t tp: //www.s o a pwa re .o rg /" > < s ta te n u m x s i: ty pe = " x s d: in t" > 4 1 < /s ta te n u m > < /m :g e tS ta te Na m e > < /S O A P- ENV :B o dy > < /S O A P- ENV :En v e lo pe >

pa ra m e te r o f ty pe in t a n d v a lu e 4 1

pro ce du ren a m e

n a m e o f s e rv e r

Page 70: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 70

Response example source: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To

Soap1.1

< ? x m l v e rs io n = " 1 .0 " ? >< S O A P-ENV :En v e lo pe S O A P- ENV :

e n co din g S ty le = " h t tp: //s ch e m a s .x m ls o a p.o rg /s o a p/e n co din g /"x m ln s :S O A P-ENC = " h t tp: //s ch e m a s .x m ls o a p.o rg /s o a p/e n co din g /"x m ln s :S O A P-ENV = " h t tp: //s ch e m a s .x m ls o a p.o rg /s o a p/e n v e lo pe /"x m ln s :x s d= " h t tp: //www.w3 .o rg /1 9 9 9 /X M L S ch e m a "x m ln s :x s i= " h t tp: //www.w3 .o rg /1 9 9 9 /X M L S ch e m a - in s ta n ce " >

< S O A P- ENV :B o dy > < m :g e tS ta te Na m e R e s po n s e x m ln s :m = " h t tp: //www.s o a pwa re .o rg /" > < R e s u lt x s i: ty pe = " x s d:s trin g " > S o u th D a k o ta < /R e s u lt> < /m :g e tS ta te Na m e R e s po n s e > < /S O A P-ENV :B o dy >< /S O A P-ENV :En v e lo pe >

n a m e o f s e rv e r

r e tu r n ed v a lu e

pro ce du re n a m e

Page 71: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 71

Example of a SOAP message for an error RPC response

source: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1< ? x m l v e rs io n = " 1 .0 " ? >

< S O A P-ENV :En v e lo pe S O A P- ENV :e n co din g S ty le = " h t tp: //s ch e m a s .x m ls o a p.o rg /s o a p/e n co din g /"x m ln s :S O A P-ENV = " h t tp: //s ch e m a s .x m ls o a p.o rg /s o a p/e n v e lo pe /"x m ln s :x s d= " h t tp: //www.w3 .o rg /1 9 9 9 /X M L S ch e m a "x m ln s :x s i= " h t tp: //www.w3 .o rg /1 9 9 9 /X M L S ch e m a - in s ta n ce " >

< S O A P- ENV :B o dy > < S O A P- ENV :Fa u lt> < fa u lt co de > S O A P- ENV :C lie n t< /fa u lt co de > < fa u lt s t rin g > C a n 't ca ll g e tS ta te Na m e be ca u s e th e re a re t o o m a n y pa ra m e te rs . < /fa u lt s t rin g > < /S O A P- ENV :Fa u lt> < /S O A P- ENV :B o dy >< /S O A P- ENV :En v e lo pe >

Page 72: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 72

HTTP and SOAP RPC Request

source: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1

A SOAP message can be used to transport a SOAP remote procedure request/response, as follows: POST /examples HTTP/1.1

User-Agent: Radio UserLand/7.0 (WinNT)Host: localhost:81Content-Type: text/xml; charset=utf-8Content-length: 474SOAPAction: "/examples"<blank line>

<text for SOAP message>

Page 73: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 73

An HTTP request that carries a SOAP RPC request source: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To

Soap1.1 POST /examples HTTP/1.1

User-Agent: Radio UserLand/7.0 (WinNT)Host: localhost:81Content-Type: text/xml; charset=utf-8Content-length: 474SOAPAction: "/examples"

<?xml version="1.0"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle=

"http://schemas.xmlsoap.org/soap/encoding/" xmlns: SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns: SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance"><SOAP-ENV:Body>       <m:getStateName xmlns:m="http://www.soapware.org/">         <statenum xsi:type="xsd:int">41</statenum>         </m:getStateName> </SOAP-ENV:Body> </SOAP-ENV:Envelope>

Page 74: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 74

An HTTP request that carries a SOAP RPC responsesource: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1

HTTP/1.1 200 OKConnection: closeContent-Length: 499Content-Type: text/xml; charset=utf-8Date: Wed, 28 Mar 2001 05:05:04 GMTServer: UserLand Frontier/7.0-WinNT

<?xml version="1.0"?><SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">   <SOAP-ENV:Body>      <m:getStateNameResponse xmlns:m="http://www.soapware.org/">         <Result xsi:type="xsd:string">South Dakota</Result>         </m:getStateNameResponse>      </SOAP-ENV:Body>   </SOAP-ENV:Envelope>

Page 75: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 75

Error Examplesource: (http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1

HTTP/1.1 500 Server ErrorConnection: closeContent-Length: 511Content-Type: text/xml; charset=utf-8Date: Wed, 28 Mar 2001 05:06:32 GMTServer: UserLand Frontier/7.0-WinNT

<?xml version="1.0"?><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/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">   <SOAP-ENV:Body>      <SOAP-ENV:Fault>         <faultcode>SOAP-ENV:Client</faultcode>         <faultstring>Can't call getStateName because there are too many parameters.</faultstring>         </SOAP-ENV:Fault>      </SOAP-ENV:Body>   </SOAP-ENV:Envelope>

Page 76: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 76

SOAP Packagessource: http://www.soapuser.com

• Apache SOAP for Java

• Apache Axis for Java

• Idoox WASP for C++

• Microsoft SOAP Toolkit (part of the .net framework)

• SOAP::Lite for Perl

Page 77: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 77

Apache SOAP

• Allows clients and services to be written in Java

• Part of the Apache-XML project (http://xml.apache.org/)

• SOAP package downloadable: http://xml.apache.org/dist/soap/

• Installation instruction:

http://www.xmethods.com/gettingstarted/apache.html

Page 78: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 78

Apache SOAP installation

% T O M C AT _ HO M E %

w eb ap p s

s o ap

W E B- I N F

c las s es

o n jav a

C alc S er v ic e . jav aC alc S er v ic e . c las sD ep lo y m en tD es c r ip to r .x m l

s o ap .w ar

s o ap

C :

s o ap - 2 _ 2

lib

x er c es , ja r m ail. ja r s o ap . ja r ac tiv a tio n . ja r

Page 79: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 79

Classpath setting

set CLASSPATH=C:\soap\soap-2_2\lib\xerces.jar;

C:\jdk1.3\bin;

C:\jdk1.3\lib\tools.jar;

C:\soap\soap-2_2\lib\mail.jar;

C:\soap\soap-2_2\lib\soap.jar;

C:\soap\soap-2_2\lib\activation.jar;

C:\tomcat\lib\servlet.jar;

.;

Page 80: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 80

Writing a Client Application using Apache SOAP

source: http://www.xmethods.com/gettingstarted/apache.html

Classpath Your CLASSPATH environment variable should have both the "soap.jar" and "xerces.jar" JAR files included. Importing packages For basic SOAP method invocation, you should import the following at minimum:

// Required due to use of URL class , required by Call class import java.net.*;

// Required due to use of Vector class import java.util.*;

// Apache SOAP classes used by client import org.apache.soap.util.xml.*; import org.apache.soap.*; import org.apache.soap.rpc.*;

Page 81: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 81

Ready-made SOAP Services

• A number of SOAP ready-made services are available at http://www.xmethods.com/

XMethods Service Weather Temperature

ID8

Service Owner:xmethods.net

Contact Email:[email protected]

Service Description:Current temperature in a given U.S. zipcode region.

SOAP Implementation:Apache SOAP

Page 82: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 82

Sample SOAP service

An aly ze W S D L | View R P C P r o f ile | h t tp :/ /w w w .x m eth o d s .n e t /s d /2 0 0 1 /T em p era tu r eS erv ic e .w s d lX M eth o d s I D 8S er v ic e O w n er : x m eth o d s .n e tC o n tac t Em ail: s u p p o r t@ x m eth o d s .n e tS er v ic e Ho m e P ag e:D es c r ip tio n : C u r r en t tem p era tu r e in a g iv en U.S . z ip c o d e r eg io n .S O AP Im p lem en ta tio n : Ap ac h e S O AP

W e a the r - T e m p e ra tu re

Found at http://www.xmethods.com

Page 83: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 83

RPC Profile for Service

R PC Pro f ile fo r S e rv ice " W e a th e r - Te m pe ra tu re "

M e th o d Na m e g e tTe m pEn dpo in t UR L h t tp: //s e rv ice s .x m e th o ds .n e t :8 0 /s o a p/s e rv le t /rpcro u te rS O A PA ct io nM e th o d Na m e s pa ce UR I u rn :x m e th o ds -Te m pe ra tu reI n pu t Pa ra m e te rs z ip c o d e s t rin g

O u tpu t Pa ra m e te rs r e tu r n f lo a t

See sample: TempClient.java

Page 84: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 84

Client program samples

• See samples in client folder:– TempClient.java– StockQuoteClient.java– CurrencyClient.java

Page 85: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 85

SOAP data types

SOAP Data Types, http://www.sdc.iup.edu/outreach/spring2002/webservices/datatypes.html

• Uses XML Schema data types• Primitive Types

string, boolean, decimal, float, double, duration, dateTime, time, date, gYearMonth, gYear, gMonthDay, gDay, gMonth, hexBinary, base64Binary, anyURI, QName, NOTATION

Page 86: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 86

SOAP data types

Derived Types • Simple types (derived from a single primitive

type)* integer is derived from decimal* int (-2147483648 <= int <= 2147483647) is derived from long which is derived from integer * 5-digit zip code can be derived from int* may use regular expressions to specify derived types, such as ([A-Z]){2,3}-\d{5}

Page 87: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 87

Complex Type

Complex types (struct or array)Struct example

<instructor> <firstname xsi:type="xsd:string">Ed</firstname>

<lastname xsi:type="xsd:string">Donley</lastname> </instructor>

• Array example <mathcourses xsi:type=

"SOAP-ENC:Array" SOAP ENC:arrayType="se:string[3]"> <se:string>10452C</se:string> <se:string>10454C</se:string> <se:string>11123T</se:string>

</mathcourses>

Page 88: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 88

Creating Web Services (server-side SOAP)

• O'Reilly Network: Using SOAP with Tomcat [Feb. 27, 2002] http://www.onjava.com/pub/a/onjava/2002/02/27/tomcat.htm

• Apache SOAP allows you to create and deploy a SOAP web service.

• You must install some .jar files on your system and set the CLASSPATH to them:

Algorithm:1. Write a class for providing the service.2. Create a deployment descriptor in XML.3. Deploy the service with the service manager.

Page 89: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 89

The Apache SOAP service manager

• The service manager is itself implemented as a SOAP service.

• To see what services are deployed on your system:

java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter list

• To deploy a service:

java org.apache.soap.server.ServiceManagerClient http://localhost:8080/soap/servlet/rpcrouter deploy foo.xml

Page 90: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 90

Creating a SOAP ServiceO'Reilly Network: Using SOAP with Tomcat [Feb. 27, 2002]

http://www.onjava.com/pub/a/onjava/2002/02/27/tomcat.html

• A SOAP service can be just about any Java class that exposes public methods for invocation. The class does not need to know anything about SOAP, or even that it is being executed as a SOAP service.

• The method parameters of a SOAP service must be serializable. The available types that can be used as SOAP service parameters are shown in (Listing 2) (shown on next slide)

Page 91: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 91

SOAP Service Parameter Types

• All Java primitive types and their corresponding wrapper classes

• Java arrays • java.lang.String • java.util.Date • java.util.GregorianCalendar • java.util.Vector • java.util.Hashtable • java.util.Map

Page 92: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 92

SOAP Service Parameter Types

• java.math.BigDecimal • javax.mail.internet.MimeBodyPart • java.io.InputStream • javax.activation.DataSource • javax.activation.DataHandler • org.apache.soap.util.xml.QName • org.apache.soap.rpc.Parameter • java.lang.Object (must be a JavaBean)

Page 93: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 93

Sample SOAP Service Implementations

See sample in SOAP folder:

• TempService: A temperature service

• Exchange: currency exchange service and client

Page 94: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 94

Sample SOAP Service Class// A sample SOAP service class// source: http://www.onjava.com/pub/a/onjava/2002/02/27/tomcat.html

package onjava;public class CalcService { public int add(int p1, int p2) { return p1 + p2; }

public int subtract(int p1, int p2) {

return p1 - p2; }}

Page 95: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 95

Deployment Descriptor<isd:service

xmlns:isd="http://xml.apache.org/xml-soap/deployment" id="urn:onjavaserver"> <isd:provider type="java" scope="Application" methods="add subtract"> <isd:java class="onjava.CalcService"/> </isd:provider>

<isd:faultListener>org.apache.soap.server.DOMFaultListener</isd:faultListener>

</isd:service>

Page 96: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 96

SOAP Summary

• SOAP is a protocol that makes use of HTTP requests and responses to effect remote method calls to web services.

• A SOAP method call is encoded in XML and is embedded in an HTTP request

• The return value of a method call is likewise embedded and encoded in an HTTP response

• A number of SOAP APIs are available for programming web services and client method calls. The Apache API was introduced.

Page 97: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 97

References (1)• Programming Web Services with SOAP, by

Snell et al, O’Reilly• SOAP Tutorial,

http://www.w3schools.com/soap/default.asp• SoapRPC.com: Tutorials,

(http://www.soaprpc.com/tutorials/) A Busy Developer’s Guide To Soap1.1

• DaveNet : XML-RPC for Newbies, http://davenet.userland.com/1998/07/14/xmlRpcForNewbies

• SoapRPC.com: Other resources, http://www.soaprpc.com/resources/

Page 98: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 98

References (2)

• Aloso,Casati, Kuno, Machiraju“Web Services - Concepts, Architectures and Applications”

• Liu“Distributed Computing - principles and applications”

Page 99: Internet Applications part 2

20 Maart 2006 ISS - Internet Applications Part 2 99

Self Study Exercises

• Chapter 11– Applet exercises: 4– General Servlet Exercises: 1 & 2– SOAP Exercises: 2 & 3