 |
 |
 |
| |
|
| 
|
|
In this article you will learn how to consume
a web service using Macromedia JRun 4 on the
back end, while using Macromedia Flash MX with
Flash Remoting on the front end.
To build Macromedia Flash movies that interact
with web services using JRun, you must have
the Macromedia Flash
MX authoring environment with the Flash
Remoting Components and JRun
4 installed. The Flash Remoting service
is a feature of JRun 4 and does not require
an additional installation. |
|
|
| |
|
Web services are remote applications that expose their functions
and associated parameters using the Web Services Description
Language (WSDL). WSDL files describe the functionality of
a web service, including available functions, parameters,
and results. You use a Simple Object Access Protocol (SOAP)
proxy to parse the WSDL and make the remote service functions
available in your application.
Before we begin, if you want to learn more about working
with JRun and web services, see Michael Peterson's article,
Using
web services in Macromedia JRun 4. Also check out the
Flash Remoting Support
Center to find additional documentation. |
| |
Identifying
the web service
The web service you will be consuming is called
IEmailService
by Shiv Kumar. This
web service is a wrapper around Simple Mail Transfer Protocol
(SMTP). Using this web service, you can send an e-mail without
attachments to anyone without the need for configuration.
Its parameters are: |
| |
| · |
(string)
toAddress |
| · |
(string)
fromAddress |
| · |
(string)
ASubject |
|
· |
(string)
MsgBody |
|
| |
| It
returns the following: |
| |
|
· |
(integer)
Return, which is "0" if successful. Any other
integer indicates an error. |
|
| |
| IEmailService
is found on www.xmethods.com
a site that lists publicly available web services. After you
learn how to consume (or make remote procedure calls) on IEmailService,
you will be able to consume any web service on xmethods.com
or anywhere you find a web service with a proper WSDL. |
| |
| Building
a front end for the web service in Macromedia Flash MX is
just as easy as building a front end for it in JSP. The difference
lies in control—when you use Macromedia Flash, the possibilities
and design options for your web application are endless. |
| |
Consuming
the web service
JRun 4 uses the Apache
Axis SOAP implementation for dealing with web services.
Axis comes with a variety of tools for working with web services.
One of these is called wsdl2java, a program that reads WSDL
and generates all the Java code needed to consume that service.
|
| |
| From
the command line, you run: |
| |
/jrunhome/bin/wsdl2java
–o<output_directory> <uri_of_wsdl> |
| |
| For
this example, if you are going to deploy under the default
server in JRun, run the following: |
| |
/jrunhome/bin/wsdl2java
–o /jrunhome/servers/default/SERVER-INF/classes/ http://webservices.matlus.com/scripts/emailwebservice.dll/wsdl/IEmailService |
| |
| This
creates four files (IEmailService.java, IEmailServicebindingStub.java,
IEmailServiceservice.java, and IEmailServiceserviceLocator.java)
in com/Borland/www off the SERVER-INF/classes directory of
the default server. Flash Remoting uses classes in SERVER-INF/classes
rather than default-war/WEB-INF/classes, as JSPs do. |
| |
| IEmailServicebindingStub.java
contains all the code that connects to and consumes the web
service. If you are interested in learning more, be sure to
look through it. Since IEmailServicebindingStub.java has no
functional empty constructor, you will have to write a wrapper
bean around it so it can be loaded through Flash Remoting.
I have written the following IEmailServiceBean.java file to
do just that. Take note of all the exceptions that are being
thrown. If you like, you can write proper try/catch's around
the method to enable more elaborate error checking: |
| |
//
//same package as the wsdl2java generated classes
package com.borland.www;
public class IEmailServiceBean
{
//create a handle to the generated stub
private com.borland.www.IEmailServicebindingStub soap;
//fully functional empty constuctor
public IEmailServiceBean() throws java.net.MalformedURLException,
org.apache.axis.AxisFault
{
final java.net.URL endPoint
= new java.net.URL
("http://webservices.matlus.com/scripts/emailwebservice.dll
/soap/IEmailservice");
soap = new
com.borland.www.IEmailServicebindingStub(endPoint, new
org.apache.axis.client.Service());
}
//public method to call the web services method
public int sendMail(String toAddress, String fromAddress,
String
aSubject, String msgBody) throws java.rmi.RemoteException
{
return
soap.sendMail(toAddress, fromAddress, aSubject,
msgBody);
}
}
|
| |
| You
have to compile all the Java files in the SERVER-INF/classes
directory because JRun does not do this automatically. Conversely,
JRun usually does compile class files that are being called
from the WEB-INF/classes directory. Make sure your CLASSPATH
is set for compiling. Everything you need for the Apache Axis
classes can be found in /javahome/lib/webservices.jar
. Once the Java classes have been compiled in SERVER-INF/classes,
restart the default JRun server. |
| |
Creating
the front end with Macromedia Flash MX and Flash Remoting
Launch the Macromedia Flash MX authoring environment so you
can build an interface for the IEmailService web service.
The service takes in four parameters, the "to" and
"from" e-mail addresses, a subject, and a message
body. |
| |
| Using
the Text tool, place four text areas in your Macromedia Flash
movie, make them of type "input text" and name them
in the Property inspector using the instance names: toAddress,
fromAddress, aSubject, and msgBody. Also drag a Flash Component
"Push Button" onto the stage. Label it "Send
Email" and set the Click Handler to "sendNow",
as shown in Figure 1. |
| |
 |
| Figure
1: An example of how you might layout your Macromedia Flash
movie. |
| |
| After
you've built the interface, it is time to add the ActionScript
to glue the Java bean and the Macromedia Flash movie together: |
| |
//First
include the necessary stuff
#include "NetServices.as"
//Start up the Movie with this
if (inited == null)
{
// do this code only once
inited = true;
// set the default gateway URL
(for use in authoring)
NetServices.setDefaultGatewayUrl
("http://localhost:8100/flashservices/gateway");
// connect to the gateway
gatewayConnnection =
NetServices.createGatewayConnection();
// get a reference to the JavaBean
in my HTTP session
(creating a new one the first time)
mailService =
gatewayConnnection.getService("com.borland.www.IEmailServiceBean"
, this);
}
//Create the click handler function "sendNow" that
sends the message when the push button is clicked.
function sendNow()
{
mailService.sendMail( toAddress.text,
fromAddress.text,
aSubject.text, msgBody.text );
}
//Finally, create a handler for the incoming data and
reuse the msgBody textarea to write the results to.
function sendMail_Result( result )
{
msgBody.text = "Message Result (0=Sent):" + result;
}
function sendMail_Status( result )
{
msgBody.text = "Error:" + result.details;
} |
| |
| Publish
the movie and you'll have a Macromedia Flash client consuming
a web service! |
| |
| |
|
| About the author |
| Mick
Robinson has been working with Java and Java-based technologies
for the last five years. His joys in life are his daughter,
Keona,
and innovative web-based applications written in J2EE. He
is currently a developer for MapCloud Services Inc.—a
Vancouver Canada-based company that specializes in cutting-edge
Internet mapping applications using Macromedia technologies.
Please visit their website at http://MapCloud.com. |
| |