95-733 Internet Technologies - PowerPoint PPT Presentation

1 / 72
About This Presentation
Title:

95-733 Internet Technologies

Description:

System.out.println('Book Titles'); for(int i = 0; i response.length; i ) ... Book Titles. To Kill A Mokingbird. Billy Budd. My Classpath for JAXM ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 73
Provided by: mm77
Category:

less

Transcript and Presenter's Notes

Title: 95-733 Internet Technologies


1
95-733 Internet Technologies
  • JAXM Java API for XML Messaging
  • (Processing SOAP)
  • Synchronous Messaging
  • Asynchronous Message

2
Java API for XML Messaging
Two main packages SOAP with
attachments API for Java (SAAJ)
javax.xml.soap Java API for
XML Messaging (JAXM) javax.xml.messaging
3
JAXM On the Client
// Code adapted from "Java Web Service" // by
Deitel StandAloneClient.java import
java.io. public class StandAloneClient
public static void main(String args) throws
IOException // Get a proxy that
handles communications BookTitlesProxy
service new BookTitlesProxy()
System.out.println("Proxy created")
4
// Ask the proxy for book titles
String response service.getBookTitles()
System.out.println("Book Titles")
for(int i 0 i lt response.length i)
System.out.println(responsei)

5
BookTitlesProxy.java
// Adapted from Java Web Services by Deitel
Deitel BookTitlesProxy.java //
BookTitlesProxy runs on the client and handles
communications // for wrapping a SOAP
document import javax.xml.soap. // for sending
the SOAP document import javax.xml.messaging.
6
// Standard Java imports import java.io. import
java.net.URL import java.util.Iterator public
class BookTitlesProxy // We will need a
connection, a message and an endpoint
private SOAPConnectionFactory soapConnectionFactor
y private URLEndpoint urlEndpoint
private MessageFactory messageFactory
7
public BookTitlesProxy() throws
java.io.IOException try
// get factories and endpoints
soapConnectionFactory
SOAPConnectionFactory.newInstance()
System.out.println("Got
SOAPconnection factory") //
get a message factory
messageFactory MessageFactory.newInstance()
System.out.println("Got Message
factory") // establish a url
endpoint urlEndpoint new
URLEndpoint(
"http//localhost8080/AnotherSOAPDemo/BookTitles"
) System.out.println("endpoint
built") catch
(SOAPException e) throw new
IOException(e.getMessage())

8
public String getBookTitles()
// invoke web service
try SOAPMessage
response sendSoapRequest()
return handleSoapResponse(response)
catch (SOAPException e)
System.out.println("Mike's
Exception " e) return
null
9
private SOAPMessage sendSoapRequest()
throws
SOAPException // get
a SOAPConnection from the factory
SOAPConnection soapConnection
soapConnectionFactory.cr
eateConnection() //
get a SOAPMessage from the factory
SOAPMessage soapRequest messageFactory.createMes
sage() // make a
synchronous call on the service //
in other words, call and wait for the response
SOAPMessage soapResponse
soapConnection.call(so
apRequest, urlEndpoint)
System.out.println("Got soap response from
server") soapConnection.close()
return soapResponse
10
/ The SOAP response has the following
structure ltsoap-envEnvelope
xmlnssoap-env
"http//schemas.xmlsoap.org/soap/envelope/"gt
ltsoap-envHeader/gt
ltsoap-envBodygt lttitles
bookCount"2"gt lttitlegtTo
Kill A MokingBirdlt/titlegt
lttitlegtBilly Buddlt/titlegt
lt/titlesgt lt/soap-envBodygt
lt/soap-envEnvelopegt /
11
private String handleSoapResponse(SOAPMessage
sr) throws
SOAPException // We need to take
the result from the SOAP message
SOAPPart responsePart sr.getSOAPPart()
SOAPEnvelope responseEnvelope
responsePart.getEnv
elope() SOAPBody responseBody
responseEnvelope.getBody()
Iterator responseBodyChildElements
responseBody.getChildElements()
SOAPBodyElement titlesElement
(SOAPBodyElement)
responseBodyChildElements.
next()
12
int bookCount Integer.parseInt(

titlesElement.getAttributeValue(
responseEnvelope.creat
eName(
"bookCount"))) String
bookTitles new StringbookCount
Iterator titleIterator titlesElement.getChild
Elements(
responseEnvelope.createName("title"))
int i 0 while(titleIterator.h
asNext()) SOAPElement
titleElement (SOAPElement)

titleIterator.next()
bookTitlesi titleElement.getValue()
return bookTitles

13
On the Server
//BookTitlesImpl.java // code adapted from "Java
Web Services" by Deitel import java.io.
import java.util. // when
using rdbms import java.sql. public class
BookTitlesImpl // this code would
normally access a database // private
Connection connection public
BookTitlesImpl() // establish a
connection to the database
14
public String getBookTitles()
// Use the rdbms connection to get a list of
// titles. Extract these titles from
the result // set and place in a
String array // here we will
skip the JDBC work String
bookTitles "To Kill A Mokingbird",

"Billy Budd" return
bookTitles
15
A New Kind of Servlet
// Code adapted from "Java Web Services" by
Deitel // BookTitlesServlet.java import
javax.servlet. import javax.xml.messaging. imp
ort javax.xml.soap. // This servlet is a
JAXMServlet. // It implements ReqRespListener and
so the client will wait for // the response. //
The alternative is to implement the
OneWayListener interface.
16
public class BookTitlesServlet extends
JAXMServlet implements

ReqRespListener // we need to create a
return message private MessageFactory
messageFactory // we need a class to handle
the reques private BookTitlesImpl service
public void init( ServletConfig config) throws
ServletException super.init(config)
try service new
BookTitlesImpl() messageFactory
MessageFactory.newInstance()
catch(SOAPException s)
s.printStackTrace()
17
/ We need to build a response with the following
structure ltsoap-envEnvelope
xmlnssoap-env
"http//schemas.xmlsoap.org/soap/envelope/"gt
ltsoap-envHeader/gt
ltsoap-envBodygt lttitles
bookCount"2"gt lttitlegtTo
Kill A MokingBirdlt/titlegt
lttitlegtBilly Buddlt/titlegt
lt/titlesgt lt/soap-envBodygt
lt/soap-envEnvelopegt /
18
// Like doPost or doGet but here we receive a
message from a // SOAP client.
public SOAPMessage onMessage( SOAPMessage
soapMessage ) try
String bookTitles null
if(service ! null)
bookTitles service.getBookTitles()
if(bookTitles ! null)
SOAPMessage message
messageFactory.createMessag
e() SOAPPart soapPart
message.getSOAPPart()
SOAPEnvelope soapEnvelope soapPart.getEnvelope()
SOAPBody soapBody
soapEnvelope.getBody()
SOAPBodyElement titlesElement
soapBody.addBodyElement
(
soapEnvelope.createName("titles"))
19
titlesElement.addAttribute(

soapEnvelope.createName("bookCount"),

Integer.toString(bookTitles.length))
for(int i 0 i lt bookTitles.length
i)
titlesElement.addChildElement(
soapEnvelope.createName("title")).

addTextNode(bookTitlesi)
return message
else
return null
catch(SOAPException s)
return null
20
Running the Client
D\McCarthy\www\95-733\examples\jaxm\clientcodegt j
ava StandAloneClient Got SOAPconnection
factory Got Message factory endpoint built Proxy
created Got soap response from server Book
Titles To Kill A Mokingbird Billy Budd
21
My Classpath for JAXM
d\jwsdp-1_0_01\common\endorsed\xercesImpl.jar.
D\xerces\xmlParserAPIs.jar d\jwsdp-1_0_01\commo
n\lib\servlet.jar d\jwsdp-1_0_01\common\lib\saaj
-api.jar d\jwsdp-1_0_01\common\lib\jaxm-api.jar
d\jwsdp-1_0_01\common\lib\saaj-ri.jar d\jwsdp-
1_0_01\common\lib\soap.jar d\jwsdp-1_0_01\common
\lib\jaxm-runtime.jar d\jwsdp-1_0_01\common\lib\
commons-logging.jar d\jwsdp-1_0_01\common\lib\ma
il.jar d\jwsdp-1_0_01\common\lib\activation.jar
22
d\jwsdp-1_0_01\common\lib\dom4j.jar d\jwsdp-1_0
_01\common\lib\jaxp-api.jar d\jwsdp-1_0_01\commo
n\endorsed\sax.jar d\jwsdp-1_0_01\common\endorse
d\xalan.jar d\jwsdp-1_0_01\common\endorsed\xerce
sImpl.jar d\jwsdp-1_0_01\common\endorsed\xsltc.j
ar
23
Asynchronous Messaging
24
JAXM With a Message Provider
An example series of calls
1. A JAXM Application sends messages to its own
provider rather than directly to a JAXM
Servlet. 2. The JAXM Application now goes on to
other things. 3. The message provider (on the
client) sends a SOAP request to another message
provider on the server. 4. In time, the message
provider on the server calls a JAXM Servlet (on
the server) and receives a response.
25
JAXM With a Message Provider
An example series of calls
5. The message provider (on the server)
communicates a SOAP document to the message
provider on the client. 6. The message provider
on the client calls a JAXM Servlet on the client.
26
(2)
JAXM Servlet
JAXM Application
(1)
(4)
(5)
Message Provider
Message Provider
(3)
(6)
In summary, the JAXM application places a SOAP
document in its out box. When the request
is processed, the response is passed back via
the clients inbox which makes a call on the
clients JAXM Servlet.
JAXM Servlet
27
XML Messaging JAXM
We enter two numbers to be added.
28
We hear back right away that the computation will
be performed.
29
Check another page to see if our computation has
been completed.
30
ResultHolder.java
JAXM Servlet ResultHandler
Browser
HTTPServlet GetResultsAsHTML
Two numbers
HTTP Servlet DoCalculationServlet
Browser
Feedback to browser right away
JAXM Message Provider
JAXM Servlet CalculationHandler
JAXM Message Provider
31
Configure the Provider
32
With a URI we name the provider
A URL Is the Providers location
33
Set the classpath
. d\jwsdp-1_0_01\common\lib\jaxm-api.jar d\jws
dp-1_0_01\common\lib\jaxm-runtime.jar d\jwsdp-1_
0_01\services\jaxm-provider d\jwsdp-1_0_01\servi
ces\jaxm-provideradmin d\jwsdp-1_0_01\common\lib
\saaj-api.jar d\jwsdp-1_0_01\common\lib\saaj-ri.
jar d\jwsdp-1_0_01\common\lib\dom4j.jar d\jwsd
p-1_0_01\common\lib\activation.jar d\jwsdp-1_0_0
1\common\lib\mail.jar d\jwsdp-1_0_01\common\lib\
commons-logging.jar d\jwsdp-1_0_01\common\lib\ja
xp-api.jar d\jwsdp-1_0_01\common\endorsed\dom.ja
r d\jwsdp-1_0_01\common\endorsed\sax.jar d\jws
dp-1_0_01\common\endorsed\xalan.jar d\jwsdp-1_0_
01\common\endorsed\xercesImpl.jar d\jwsdp-1_0_01
\common\endorsed\xsltc.jar d\xerces\xmlParserAPI
s.jar d\jwsdp-1_0_01\common\lib\servlet.jar d\
jwsdp-1_0_01\common\lib\soap.jar d\jwsdp-1_0_01\
common\lib\providerutil.jar
34
Provide two client.xml files
lt?xml version"1.0" encoding"ISO-8859-1"?gt lt!DOCT
YPE ClientConfig PUBLIC "-//Sun Microsystems,
Inc.//DTD JAXM Client//EN" "http//java.sun.co
m/xml/dtds/jaxm_client_1_0.dtd"gt ltClientConfiggt
ltEndpointgturnedu.cmu.andrew.mm6.serverProviderlt
/Endpointgt ltCallbackURLgt
http//127.0.0.18080/jaxmasync/servercode/Calcula
tionHandler lt/CallbackURLgt ltProvidergt
ltURIgthttp//java.sun.com/xml/jaxm/providerlt/URIgt
ltURLgthttp//127.0.0.18081/jaxm-provider/se
nderlt/URLgt lt/Providergt lt/ClientConfiggt
On the server side
35
lt?xml version"1.0" encoding"ISO-8859-1"?gt lt!DOCT
YPE ClientConfig PUBLIC "-//Sun Microsystems,
Inc.//DTD JAXM Client//EN" "http//java.sun.co
m/xml/dtds/jaxm_client_1_0.dtd"gt ltClientConfiggt
ltEndpointgturnedu.cmu.andrew.mm6.clientProvider
lt/Endpointgt ltCallbackURLgthttp//127.0.0.1
8080/jaxmasync/clientcode/ResultHandler
lt/CallbackURLgt ltProvidergt
ltURIgthttp//java.sun.com/xml/jaxm/providerlt/URIgt
ltURLgthttp//127.0.0.18081/jaxm-provider/send
erlt/URLgt lt/Providergt lt/ClientConfiggt
On the client side
36
Server Side Directories
jaxmasync/servercode ---build
---WEB-INF ---classes
---lib ---docs ---src -- CalculationHandler.jav
a ---web index.html ---WEB-INF web.xml
---classes client.xml
build.properties build.xml
37
Client Side Directories
D\McCarthy\www\95-733\examples\jaxmasync\clientco
degttree ---build ---WEB-INF
---classes ---lib ---docs ---src ---w
eb index.html ---WEB-INF web.xml
---classes client.xml build.properties
build.xml
DoCalculationServlet.java GetResultAsHTML.java R
esultHandler.java ResultHolder.java
38
Client Side Web.xml
lt?xml version"1.0" encoding"ISO-8859-1"?gt lt!DOCT
YPE web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.2//EN"
"http//java.sun.com/j2ee/dtds/web-app_2_2.dtd"gt lt
web-appgt ltservletgt ltservlet-namegtGetDa
taFromHTML lt/servlet-namegt
ltservlet-classgt DoCalculationServlet
lt/servlet-classgt lt/servletgt
39
ltservletgt ltservlet-namegtHandleResu
lt lt/servlet-namegt
ltservlet-classgt ResultHandler
lt/servlet-classgt lt/servletgt ltservletgt
ltservlet-namegtHTMLServletlt/servlet-namegt
ltservlet-classgtGetResultAsHTMLlt/servlet-classgt
lt/servletgt
40
ltservlet-mappinggt ltservlet-namegtHTMLSer
vletlt/servlet-namegt lturl-patterngt/GetResul
tAsHTML/lt/url-patterngt lt/servlet-mappinggt
ltservlet-mappinggt ltservlet-namegt
GetDataFromHTML lt/servlet-namegt
lturl-patterngt /DoCalculationServlet/
lt/url-patterngt lt/servlet-mappinggt

41
ltservlet-mappinggt ltservlet-namegt
HandleResult lt/servlet-namegt
lturl-patterngt /ResultHandler/
lt/url-patterngt lt/servlet-mappinggt lt/web-app
gt
42
Server Side web.xml
lt?xml version"1.0" encoding"ISO-8859-1"?gt lt!DOCT
YPE web-app PUBLIC "-//Sun Microsystems,
Inc.//DTD Web Application 2.2//EN"
"http//java.sun.com/j2ee/dtds/web-app_2_2.dtd"gt
ltweb-appgt ltservletgt
ltservlet-namegtHandleCalclt/servlet-namegt
ltservlet-classgtCalculationHandlerlt/servlet-classgt
ltload-on-startup/gt lt/servletgt
ltservlet-mappinggt ltservlet-namegtHandleCalc
lt/servlet-namegt lturl-patterngt/CalculationH
andler/lt/url-patterngt lt/servlet-mappinggt lt/we
b-appgt
43
ResultHolder.java
JAXM Servlet ResultHandler
Browser
HTTPServlet GetResultsAsHTML
Two numbers
HTTP Servlet DoCalculationServlet
Browser
Feedback to browser right away
JAXM Provider
JAXM Servlet CalculationHandler
JAXM Provider
44
Server Side CalculationHandler.java
// CalculationHandler.java -- Called by provider
// -- Get values from
message // -- Perform
addition and send a message to provider import
java.net. import java.io. import
java.util. import javax.servlet.http. import
javax.servlet. import javax.xml.messaging. imp
ort javax.xml.soap. import javax.activation. i
mport com.sun.xml.messaging.jaxm.ebxml. import
org.apache.commons.logging. import java.math.
45
public class CalculationHandler extends
JAXMServlet implements
OnewayListener private static final
boolean deBug true private
ProviderConnection serverProvider private
MessageFactory messageFactory private
String from private String to
46
public void init(ServletConfig config) throws
ServletException
super.init(config)
try // get a connection to
the client provider
ProviderConnectionFactory providerFactory

ProviderConnectionFactory.newInstance()
serverProvider
providerFactory.createConnection()
// Establish 'from'
and 'to' URN's to be placed within the
// outgoing message.
// The provider must know how these names map to
URL's // via the Provider
// Administration tool.

47
from "urnedu.cmu.andrew.mm6.serverProvi
der" ProviderMetaData metaData
serverProvider.getMetaData() String
profiles metaData.getSupportedProfiles()
boolean isProfileSupported
false for(int i 0 i lt profiles.length
i) if(profilesi.equals("ebxml"))
isProfileSupported true
if(isProfileSupported) //
Build an EBXML style message factory
messageFactory serverProvider.createMessageFacto
ry("ebxml") if(deBug)
System.out.println("Server side init complete
with no problem")
48
else throw new ServletException("Ca
lculationHandler
Profile problem" new Date())
catch(Exception e) throw new
ServletException( "CalculationHandler
Problems in init()" e new Date())

49
public void onMessage(SOAPMessage
reqMessage) if(deBug)
System.out.println("Hit onMessage() big time "
new Date())
// Build SOAP document to return to the sender
try if(deBug)
System.out.println("The following is the message
received
by CalculationHandler
onMessage") reqMessage.writeTo(System.
out)
50
EbXMLMessageImpl resMessage new

EbXMLMessageImpl(reqMessage) to
"urnedu.cmu.andrew.mm6.clientProvider"
resMessage.setSender(new Party(from))
resMessage.setReceiver(new Party(to)) Iterator
i resMessage.getAttachments() AttachmentPart
operand1 (AttachmentPart) i.next()
AttachmentPart operand2 (AttachmentPart)
i.next() String op1 (String)(operand1.getCont
ent()) String op2 (String)(operand2.getContent
()) BigInteger o1 new BigInteger(op1)
BigInteger o2 new BigInteger(op2) BigInteger
result o1.add(o2)
51
String answer result.toString() AttachmentPart
answerPart resMessage.createAttachmentPart() an
swerPart.setContent(answer, "text/plain")
resMessage.addAttachmentPart(a
nswerPart) serverProvider.send(resMess
age) if(deBug) System.out.println(
"CalculationHandler Message sent back
to
clientProvider")
catch(IOException ioe)
System.out.println("JAXM exception thrown "
ioe) catch(JAXMException je)
System.out.println("JAXM exception thrown "
je) catch(SOAPException se)
System.out.println("Soap exception thrown "
se)
52
ResultHolder.java
JAXM Servlet ResultHandler
Browser
HTTPServlet GetResultsAsHTML
Two numbers
HTTP Servlet DoCalculationServlet
Browser
Feedback to browser right away
JAXM Provider
JAXM Servlet CalculationHandler
JAXM Provider
53
HTTP ServletDoCalculationServlet
// DoCalculation.java -- Get values from client
browser // -- Send SOAP
request to ClientProvider Asynchronously import
java.net. import java.io. import
java.util. import javax.servlet.http. import
javax.servlet. import javax.xml.messaging. imp
ort javax.xml.soap. import javax.activation. i
mport com.sun.xml.messaging.jaxm.ebxml. import
org.apache.commons.logging.
54
// Normal servlet. Collects data from the
browser, sends a SOAP message // to a JAXM
provider and does not wait for a response. Sends
an HTML // note of confirmation back to the
browser. public class DoCalculationServlet
extends HttpServlet private
ProviderConnection clientProvider private
MessageFactory messageFactory private
String from private String to
private static final boolean deBug true
public void init(ServletConfig config) throws
ServletException
super.init(config) if(deBug)
System.out.println( "Running at
the top of client side html form init" new
Date())
55
try // get a connection to the
client provider ProviderConnectionFa
ctory providerFactory
ProviderConnectionFactory.newInstance()
clientProvider providerFactory.creat
eConnection()
// Establish 'from' and 'to' URN's to be placed
within the // outgoing message.
// The provider must know how these
names map to // URL's via the
Provider // Administration
tool. from
"urnedu.cmu.andrew.mm6.clientProvider"
to "urnedu.cmu.andrew.mm6.serverProvider
"
56
ProviderMetaData metaData
clientProvider.getMetaData()
String profiles metaData.getSupportedProfiles(
) boolean isProfileSupported
false for(int i 0 i lt
profiles.length i)
if(profilesi.equals("ebxml"))
isProfileSupported true
if(isProfileSupported)
// Build an EBXML style
message factory messageFactory
clientProvider.createMessageFactory(

"ebxml")
else throw new
ServletException("Runtime Profile problem"
new Date())
catch(Exception e)
throw new ServletException("Run
Time Problem in
DoCalculationServlet init()" e new
Date())
57
public void doPost(HttpServletRequest req,
HttpServletResponse
response) throws
ServletException,
IOException response.setContentTy
pe("text/html") PrintWriter out
response.getWriter() String
finalString "" String op1
req.getParameter("op1") String op2
req.getParameter("op2") //
Build SOAP document to send to the provider
try EbXMLMessageImpl
message (EbXMLMessageImpl)

messageFactory.createMessage()
58
message.setSender(new Party(from)) message.setRec
eiver(new Party(to)) AttachmentPart operand1
message.createAttachmentPart() operand1.setConten
t(op1, "text/plain") AttachmentPart operand2
message.createAttachmentPart() operand2.setConten
t(op2, "text/plain") message.addAttachm
entPart(operand1) message.addAttachmentPart(opera
nd2) clientProvider.send(message)
catch(JAXMException je)
System.out.println(
"JAXM exception thrown " je)
catch(SOAPException se) System.out.println(

"Soap exception thrown " se)

59
String docType "lt!DOCTYPE HTML PUBLIC
\"//W3C//DTD"
" HTML 4.0 " docType "Transitional//EN\"gt\
n" out.println(docType
"ltHTMLgt\n" "ltHEADgtltTITLEgtRequest
Response" "lt/TITLEgt"
"lt/HEADgt\n" "ltBODYgt\n"
"ltH2gt Sent Calculation Request to Local
Provider lt/H2gt\n" "ltH2gt" op1
"" op2 " will be computedlt/H2gt\n"
"lt/BODYgtlt/HTMLgt")
60
ResultHolder.java
JAXM Servlet ResultHandler
Browser
HTTPServlet GetResultsAsHTML
Two numbers
HTTP Servlet DoCalculationServlet
Browser
Feedback to browser right away
JAXM Provider
JAXM Servlet CalculationHandler
JAXM Provider
61
JAXM Servlet ResultHandler
// ResultHandler.java -- Called by provider
// -- Get result from
message // -- Write
result to a shared object or RDBMS import
java.net. import java.io. import
java.util. import javax.servlet.http. import
javax.servlet. import javax.xml.messaging. imp
ort javax.xml.soap. import javax.activation. i
mport com.sun.xml.messaging.jaxm.ebxml. import
org.apache.commons.logging. import java.math.
62
public class ResultHandler extends JAXMServlet
implements OnewayListener
private static final boolean deBug
true private ProviderConnection
clientProvider private MessageFactory
messageFactory String from, to
public void init(ServletConfig config) throws
ServletException
super.init(config)
63
try // get a connection to the
client provider ProviderConnectionFacto
ry providerFactory
ProviderConnectionFactory.newInstanc
e() clientProvider
providerFactory.createConnection()
// Establish 'from' and 'to'
URN's to be placed within // the
outgoing message. // The provider must
know how these names map to URL's //
via the Provider // Administration
tool. from
"urnedu.cmu.andrew.mm6.clientProvider"
ProviderMetaData metaData clientProvider.getMe
taData() String profiles
metaData.getSupportedProfiles()
64
boolean isProfileSupported false
for(int i 0 i lt profiles.length i)
if(profilesi.equals("ebxml"))
isProfileSupported true
if(isProfileSupported) // Build an
EBXML style message factory
messageFactory clientProvider.createMessageFacto
ry("ebxml") else throw
new ServletException("ResultHandler
OnewayListener
Profile problem" new
Date()) catch(Exception e)
throw new ServletException("ResultHandler
OnewayListener
Problems in init()" e
new Date())
65
public void onMessage(SOAPMessage
inMessage) try if(deBug)
System.out.println("Message received from
server appears
now")
inMessage.writeTo(System.out) // Read the
data from the SOAP document EbXMLMessageImpl
resultMessage new
EbXMLMessageImpl(inMessage) Iterator i
resultMessage.getAttachments()
AttachmentPart operand1 (AttachmentPart)
i.next() AttachmentPart operand2
(AttachmentPart) i.next()
AttachmentPart result (AttachmentPart)
i.next()
66
String op1 (String)(operand1.getContent()) Stri
ng op2 (String)(operand2.getContent()) String
res (String)(result.getContent()) //
Place the result in a shared object. ResultHolder
singleTon ResultHolder.getInstance() singleTon.
setLastResult(op1 " " op2 " "
res) catch(Exception e)
System.out.println("onMessage in ResultHandler
exception " e)
67
ResultHolder.java
JAXM Servlet ResultHandler
Browser
HTTPServlet GetResultsAsHTML
Two numbers
HTTP Servlet DoCalculationServlet
Browser
Feedback to browser right away
JAXM Provider
JAXM Servlet CalculationHandler
JAXM Provider
68
ResultHolder.java
public class ResultHolder private String
lastResult private static ResultHolder
instance new ResultHolder() private
ResultHolder() lastResult ""
public static ResultHolder getInstance()
return instance synchronized public String
getLastResult() return lastResult
synchronized public void setLastResult(String
result) lastResult

result synchronized public
String toString() return lastResult
69
ResultHolder.java
JAXM Servlet ResultHandler
Browser
HTTPServlet GetResultsAsHTML
Two numbers
HTTP Servlet DoCalculationServlet
Browser
Feedback to browser right away
JAXM Provider
JAXM Servlet CalculationHandler
JAXM Provider
70
GetResultsAsHTML
// GetResultAsHTML.java -- Get result from
ResultHolder object // --
Send it back to client as HTML import
java.net. import java.io. import
java.util. import javax.servlet.http. import
javax.servlet. public class GetResultAsHTML
extends HttpServlet private static final
boolean deBug true public void
init(ServletConfig config) throws
ServletException
super.init(config)
71
public void doGet (HttpServletRequest req,
HttpServletResponse response)
throws ServletException,
IOException
response.setContentType("text/html")
PrintWriter out response.getWriter()
String finalString ResultHolder.getInstance().to
String() String docType
"lt!DOCTYPE HTML PUBLIC \"//W3C//DTD"
" HTML 4.0 "
docType "Transitional//EN\"gt\n"
72
out.println(docType
"ltHTMLgt\n"
"ltHEADgtltTITLEgt
GetResultAsHTML Response" "lt/TITLEgt"
"lt/HEADgt\n"
"ltBODYgt\n" "ltH2gt Most
Recent Calculation lt/H2gt\n"
"ltH2gt" finalString "lt/H2gt\n"
"lt/BODYgtlt/HTMLgt")
Write a Comment
User Comments (0)
About PowerShow.com