Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB Compiler   

Hosting a DAO on a Web Server

After you construct your DAO, you need to expose the wrapped service(s) via the Web.

There are many things to consider with regards to exposing the service. For example, a JSP is not suited for binary streaming since the J2EE infrastructure already wraps the output stream. In each of the following sections, some basic concepts that can be used in a realistic system are demonstrated. Typically, the response is not simply dumped to the output stream, but instead wrapped in a more complex XML document or Web service. Using these templates as a guide, you can extend these examples using similar patterns. For each of these examples, refer to the DAO class defined in Creating a DAO for Deployment. This DAO takes care of MATLAB specific data conversion and data clean-up tasks.

Hosting the DAO with a Servlet

Note that the DAO is initialized in the init method of the servlet. When you create and access a component created with the builders, an instance of the MCR is created that the component communicates with in order to handle MATLAB tasks. This can incur much overhead if performed every time a user accesses the servlet. Alternately, by performing initialization in the init method, it is performed once for all sessions using the servlet. If you want to rebuild each time, place the call within a doget method.

It is also possible that neither of the above approaches will meet your needs since they initialize once per servlet, rather then once per server. If this is an issue, use a singleton object that is instantiated in a Context Listener class (a class that extends ServletContextListener). This class has a contextInitialized method and a contextDestroyed method which get called automatically when the server starts or is stopped. This allows all of your applications to access the singleton and access component objects as needed.

  1. Create a staging folder, if one does not exist, under the folder where your component resides on your Web server. The DAO must reside in this folder, in a Java archive file (JAR), on the class path so it can be imported.

  2. Initialize the DAO using the following examples as templates:

     Initializing the DAO for a Servlet

     Numeric

     String

     Numeric Array

     Character Array

     Cell Array To Array

     Cell Array To List

     Structure To Map

     Byte Array

     Images (WebFigures)

     WebFigure to Bytes

     Raw Image Bytes

     Raw Image Bytes with Reorientation

  3. Inside the staging folder you created at the start of this procedure, create a WEB-INF folder.

  4. Inside the WEB-INF folder, create two additional folders:

    • classes

    • lib

  5. Place all of the class files (including the DAO created in Creating a DAO for Deployment) into the class folder within the appropriate package folders that exist.

  6. Copy the component JAR file into the lib folder.

  7. Create a web.xml file in the WEB-INF folder.

    This file provides the Web server with a valid path into your code and defines the entry point into that code. Use this template as an example:

     Example of a web.xml File Used in a Java™ Servlet Component

    The following URL accesses this servlet with the configuration described above:

    http://localhost:8080/Examples/ExamplesServlet

    Note the Examples string in the URL, since the JAR is named Examples.jar. Using this string sets up the correct server context and is a customizable attribute within the console of many Web servers.

  8. Using the java -jar command, bundle the folders you created into a WAR (Web archive) and place it in your Web server's component folder.

      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 Java™ 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.

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

 String

 Numeric Array

 Character Array

 Byte Array

 Raw Image Bytes

 Raw Image Bytes with Reorientation

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 and the Web service class created in Setting Up the Root Web Service Classinto it.

  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 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 System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using 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();
		}
}

Interacting with the DAO

Some examples of how to use the DAO with various data types follow:

 Numeric

 String

 Double Array

 Character Array

 Cell Array to Array

 Cell Array to List

 Structure

 Byte Array

 Raw Image Bytes

 Raw Image Bytes with Reorientation

 WebFigure

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

 String

 Double Array

 Double Matrix

 Character Array

 Byte Array

 Raw Image Bytes

 Raw Image Bytes with Reorientation

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.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS