Detailed instructions for use are in the User's Guide.
[. . . ] MATLAB® Application Deployment Web Example Guide
How to Contact The MathWorks
Web Newsgroup www. mathworks. com/contact_TS. html Technical Support
www. mathworks. com comp. soft-sys. matlab suggest@mathworks. com bugs@mathworks. com doc@mathworks. com service@mathworks. com info@mathworks. com
Product enhancement suggestions Bug reports Documentation error reports Order status, license renewals, passcodes Sales, pricing, and general information
508-647-7000 (Phone) 508-647-7001 (Fax) The MathWorks, Inc. 3 Apple Hill Drive Natick, MA 01760-2098
For contact information about worldwide offices, see the MathWorks Web site. MATLAB® Application Deployment Web Example Guide © COPYRIGHT 20082010 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used or copied only under the terms of the license agreement. [. . . ] Note Some Web servers require you to register the application before it is accessible, usually by referencing the WAR from within the administrator's console.
Hosting a DAO Using a JavaTM Web Service
More and more companies are hosting services on the Web, often times with SOAP (Simple Object Access Protocol). This exposes business functions through simple services. Each of these services performs a specific task. Since SOAP is an established standard that is supported by many different languages and third-party applications, it is extremely versatile. You can use a SOAP Web service directly in Microsoft Excel with no prior knowledge of the service's implementation. Multiple language support makes SOAP suitable for use with primitive data types. Although these primitives can be wrapped in a number of complex object structures, the examples in this section will cover fundamental use cases that should be the same, regardless of data structure and business objects.
4-31
4
Business Service Developer Tasks
In this section, you will learn how to create basic Java objects that handle business logic, while Apache Axis2 performs the mechanics involved with turning the logic a Web service and exposing it. Alternatively, you can start by using WSDL (Web Service Definition Language -- the definition of your service) and generate Java from that. Afterward you can customize the Java with your business logic, or change the WSDL manually in a number of other ways to meet your needs.
Setting Up the Root Web Service Class
Since Axis2 supports POJOs (Plain Old Java Objects) you will create a shell class to contain all the service methods:
package examples; public class ExamplesWebService { //*************************** //**Place service methods here //**For our examples we will only //**be taking in and returning //**primitive values //*************************** }
Interacting with the DAO
Some examples of how to use the DAO with various data types follow: Numeric
public int getInt() { Examples examples = new Examples(); int integer = examples. getIntFromMWNumericArray(); examples. dispose(); return integer; }
4-32
Hosting a DAO on a Web Server
String
public String getString() { Examples examples = new Examples(); String string = examples. getStringFromMWCharArray(); examples. dispose(); return string; }
Numeric Array
public int[] getIntArray() { Examples examples = new Examples(); int[] intArray = examples. getIntArrayFromMWNumericArray(); examples. dispose(); return intArray; }
Character Array
public char[] getCharArray() { Examples examples = new Examples(); char[] charArray = examples. getCharArrayFromMWCharArray(); examples. dispose(); return charArray; }
Byte Array
public byte[] getByteArray() { Examples examples = new Examples(); byte[] byteArray = examples. getByteArrayFromMWNumeric(); examples. dispose(); return byteArray; }
4-33
4
Business Service Developer Tasks
Raw Image Bytes
Raw Image Bytes public byte[] getImageByteArray() { Examples examples = new Examples(); byte[] rawImageBytes = examples. getImageByteArrayFromMWNumericArray(); examples. dispose(); return rawImageBytes; }
Raw Image Bytes with Reorientation
public byte[] reorientAndGetImageByteArray( int height, int width, int elevation, int rotation, String imageFormat) { Examples examples = new Examples(); byte[] rawImageBytes = examples. getImageByteArrayFromMWNumericArrayWithOrientation( height, width, elevation, rotation, imageFormat); examples. dispose(); return rawImageBytes; }
Deploying the Web Service
1 Create a staging folder, if one does not exist, and copy the Examples DAO
class created in "Creating a DAO for Deployment" on page 4-6 and the Web service class created in "Setting Up the Root Web Service Class" on page 4-32into it.
4-34
Hosting a DAO on a Web Server
2 Create a lib folder and copy your deployed component into it. 3 Create a meta-inf folder and, inside it, create a services. xml file with
these contents:
<service> <parameter name="ServiceClass" locked="false">examples. ExamplesWebService</parameter> <operation name="getInt"> <messageReceiver class="org. apache. axis2. rpc. receivers. RPCMessageReceiver"/> </operation> <operation name="getString"> <messageReceiver class="org. apache. axis2. rpc. receivers. RPCMessageReceiver"/> </operation> <operation name="getIntArray"> <messageReceiver class="org. apache. axis2. rpc. receivers. RPCMessageReceiver"/> </operation> <operation name="getCharArray"> <messageReceiver class="org. apache. axis2. rpc. receivers. RPCMessageReceiver"/> </operation> <operation name="getByteArray"> <messageReceiver class="org. apache. axis2. rpc. receivers. RPCMessageReceiver"/> </operation> <operation name="getImageByteArray"> <messageReceiver class="org. apache. axis2. rpc. receivers. RPCMessageReceiver"/> </operation> </service>
The services. xml file tells Axis2 which methods to expose, and what mechanism to use to expose them.
4 Copy all of the files into a WAR (Web archive) file and place them in the
axis2 component folder (axis2/WEB-INF/services). Use the java -jar
4-35
4
Business Service Developer Tasks
command but give the output file an . aar extension rather than a . jar extension.
5 You should now see your service running in the Axis console. From the
console, note the URL for the WSDL file. You will use this URL in other applications to communicate with your Web service.
Hosting a . NET DAO with ASPX
Initializing the DAO
Before a DAO can be used, it must be initialized. The basic template to initialize a . NET DAO looks like this:
using using using using using using using using using System; System. Data; System. Configuration; System. Web; System. Web. Security; System. Web. UI; System. Web. UI. WebControls; System. Web. UI. WebControls. WebParts; System. Web. UI. HtmlControls;
public partial class _Default : System. Web. UI. Page { protected void Page_Load(object sender, EventArgs e) { Examples. Examples examples = new Examples. Examples(); //*************************************** //**This is where the examples would be pasted in //*************************************** //for Examples: int integer = examples. getIntFromMWNumericArray(); Response. Write("int: " + integer); examples. dispose(); } }
4-36
Hosting a DAO on a Web Server
Interacting with the DAO
Some examples of how to use the DAO with various data types follow: Numeric
int integer = examples. getIntFromMWNumericArray(); Response. Write("int: " + integer);
String
String stringResult = examples. getStringFromMWCharArray(); Response. Write("String: " + stringResult);
Double Array
double[] doubleArray = examples. getDoubleArrayFromMWNumericArray(); Response. Write("Double Array: "); for (int i = 0; i < doubleArray. Length; i++) { Response. Write("Array index(" + i + "): " + doubleArray[i]); }
Character Array
char[] charArray = examples. getCharArrayFromMWCharArray(); Response. Write("Char Array: "); for (int i = 0; i < charArray. Length; i++) { Response. Write("Array index("+ i +"): " + charArray[i]); }
Cell Array to Array
Object[] array = examples. getArrayFromCellArray(); for (int i = 0; i < array. Length; i++) { Response. Write("Array index("+ i+"): " + array[i]);
4-37
4
Business Service Developer Tasks
}
Cell Array to List
List<Object> list = examples. getListFromCellArray(); foreach (Object currentObj in list) { Response. Write("List Item: " + currentObj); }
Structure
Dictionary<Object, Object> dictionary = examples. getDictionaryFromStruct(); Response. Write("Structure Array: "); foreach (Object currentKey in dictionary. Keys) { Response. Write("Key: " + currentKey + " Value: " + dictionary[currentKey]); }
Byte Array
byte[] byteArray = examples. getByteArrayFromMWNumericArray(); Response. Write("Byte Array: "); for (int i = 0; i < byteArray. Length; i++) { Response. Write(byteArray[i]); } Response. BinaryWrite(byteArray);
Raw Image Bytes
byte[] rawImageBytes = examples. getImageByteArrayFromMWNumericArray(); Response. BinaryWrite(rawImageBytes);
4-38
Hosting a DAO on a Web Server
Raw Image Bytes with Reorientation Note This example allows you to perform similar functionality to what WebFigures performs, but in a manual implementation. It is one of many ways you can implement this functionality in a stateless manner.
int height = Convert. ToInt32(Request. Params. Get("height")); int width = Convert. ToInt32(Request. Params. Get("width")); int elevation = Convert. ToInt32(Request. Params. Get("elevation")); int rotation = Convert. ToInt32(Request. Params. Get("rotation")); String imageFormat = Request. Params. Get("imageFormat"); byte[] rawImageBytes = examples. getImageByteArrayFromMWNumericArrayWithOrientation( height, width, elevation, rotation, imageFormat); Response. BinaryWrite(rawImageBytes);
WebFigure
// In this example, we use a WebFigure Utility to get an HTML // String that // will display this figure, Notice // how we reference the name we used when attaching it to the // cache and we indicate // that the Attach type is session. String localEmbedString = WebFigureServiceUtility. GetHTMLEmbedString( "SessionStateWebFigure", WebFigureAttachType. session, 300, 300); Response. Write(localEmbedString);
4-39
4
Business Service Developer Tasks
Deploying the ASPX
You deploy an ASPX using the Publish functionality in Microsoft® Visual Studio. Visual Studio puts all of your code, along with any code your project depends upon, in a folder.
Hosting a DAO Using a . NET Web Service
Setting Up the Root Web Service Class
When creating Web services within . NET, simply create a new Web site (or use an existing site), and add an item of type Web Service to it. This will generate the root class in which you place your methods.
Interacting with the DAO
Each of these methods would be placed in the Web service class as methods. Numeric
[WebMethod] public int getInt() { Examples. Examples examples = new Examples. Examples(); int integer = examples. getIntFromMWNumericArray(); examples. dispose(); return integer; }
String
[WebMethod] public String getString() { Examples. Examples examples = new Examples. Examples(); String stringResult = examples. getStringFromMWCharArray(); examples. dispose(); return stringResult; }
4-40
Hosting a DAO on a Web Server
Double Array
[WebMethod] public double[] getDoubleArray() { Examples. Examples examples = new Examples. Examples(); double[] doubleArray = examples. getDoubleArrayFromMWNumericArray(); examples. dispose(); return doubleArray; }
Double Matrix Since . NET Web services can't support multidimensional arrays, convert what is returned from MATLAB Builder NE into a jagged array, as follows:
[WebMethod] public double[][] getDoubleMatrix(int argMagic) { Examples. ExamplesImpl examples = new Examples. ExamplesImpl(); double[, ] doubleMatrix = examples. getDoubleMatrixFromMWNumericArray(argMagic); int arraySize = (int)doubleMatrix. GetUpperBound(0) + 1; double[][] outputMatrix = new double[arraySize][]; int upperOuter = i < (int)doubleMatrix. GetUpperBound(0) + 1; for (int i = 0; upperOuter ; i++) { double[] subArray = new double[arraySize]; int upperInner = (int)doubleMatrix. GetUpperBound(1) + 1; for(int j = 0; j < upperInner; j++) { subArray[j] = doubleMatrix[i, j]; } outputMatrix[i] = subArray;
4-41
4
Business Service Developer Tasks
} examples. dispose(); return outputMatrix; }
Character Array
[WebMethod] public char[] getCharArray() { Examples. Examples examples = new Examples. Examples(); char[] charArray = examples. getCharArrayFromMWCharArray(); examples. dispose(); return charArray; }
Byte Array
[WebMethod] public byte[] getByteArray() { Examples. Examples examples = new Examples. Examples(); byte[] byteArray = examples. getByteArrayFromMWNumericArray(); examples. dispose(); return byteArray; }
Raw Image Bytes
[WebMethod] public byte[] getImageByteArray() { Examples. Examples examples = new Examples. Examples(); byte[] rawImageBytes = examples. getImageByteArrayFromMWNumericArray(); examples. dispose(); return rawImageBytes; }
4-42
Hosting a DAO on a Web Server
Raw Image Bytes with Reorientation
[WebMethod] public byte[] getImageByteArrayWithOrientation( int height, int width, int elevation, int rotation, String imageFormat) { Examples. Examples examples = new Examples. Examples(); byte[] rawImageBytes = examples. getImageByteArrayFromMWNumericArrayWithOrientation( height, width, elevation, rotation, imageFormat); examples. dispose(); return rawImageBytes; }
Deploying the Web Service
Visual Studio 2005 does all of the work involved with generating Web service artifacts. Once you've created the above methods, just run the service and you'll see a tester page that shows you the location of the WSDL, and then allows you to test each method.
4-43
4
Business Service Developer Tasks
4-44
5
Front-End Developer Tasks
· "Working with the Front-End Layer" on page 5-2 · "Creating a WebFigure on a JSP Page" on page 5-6 · "Creating a WebFigure on an ASPX Page" on page 5-10 · "Working with Static Images" on page 5-13 · "Displaying Complex Data Types Including Arrays and Matrices" on page 5-18 · "Using Web Services" on page 5-26
5
Front-End Developer Tasks
Working with the Front-End Layer
In this section. . . "About the Front-End Layer" on page 5-2 "About the Examples" on page 5-4
Service consumer responsible for presentation and usability Front-End Developer No MATLAB experience
Creates front end applications
About the Front-End Layer
Note For comprehensive end-to-end implementations of the concepts in this chapter, see Appendix A, "Sources for More Information". In well-designed multi-tier application architectures, the front-end layer presents data to the end user and validates the user's input. [. . . ] See the MATLAB Compiler User's Guide reference pages for details on the mcrversion command.
2 Ensure you have Tomcat 5 or later on your system (other J2EE Web
servers can work also, but the steps in this document have been tested with Tomcat).
3 Ensure the version of the MCR you have installed is the same version
as the MCR running with MATLAB when the application was built. If you are unsure, check with your MATLAB programmer or whoever initially deployed the component. For more information, see "Deploying a Component with the Magic Square Example" in the MATLAB Builder JA User's Guide.
4 Make note of the folder where the MCR is installed. It will be used later
when starting the applications.
5 Create the code for the JSP page:
Note This code uses an image resource and a cascading style sheet resource that is included if you download the code from MATLAB Central as in "Quick Start: Building Your Example Component" on page 8-5.
8-6
Creating an End-to-End Web Application
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Calculation Services</title> <% //This section of code determines if the user has entered // a number for the square size, if they have it overlays // the default size which is 5. [. . . ]