Skip to Main Content Skip to Search
Product Documentation

Web Application Example

Example Overview

This example demonstrates how to display a plot created by a Java servlet calling a component created with the MATLAB Builder JA product over a Web interface. This example uses MATLAB varargin and varargout for optional input and output to the varargexample.m function. For more information about varargin and varargout, see Specifying Optional Arguments.

Before You Work this Example

This section describes what you need to know and do before you create the Web deployment example.

Ensure You Have the Required Products

The following products must be installed at their recommended release levels.

MATLAB, MATLAB Compiler, MATLAB Builder JA.  This example was tested with R2007b.

Java Development Kit (JDK).  Ensure you have a JDK installed on your system. You can download it from Sun Microsystems, Inc.

Ensure Your Web Server Is Java Compliant

In order to run this example, your Web server must be capable of running accepted Java frameworks like J2EE. Running the WebFigures demo (Deploying a Java Component Over the Web) also requires the ability to run servlets in WARs (Web Archives).

Install the javabuilder.jar Library

Ensure that the javabuilder.jar library (matlabroot/toolbox/javabuilder/jar/javabuilder.jar) has been installed into your Web server's common library folder.

Downloading the Demo Files

Download the demo files from the File Exchange at MATLAB Central. With File Exchange selected in the Search drop-down box, enter the keyword java_web_vararg_demo and click Go.

Contents of the Demo Files

The demo files contain the following three folders:

Build Your Java Component

Build your Java component by compiling your code into a deployable Java component .jar file.

  1. Start deploytool at the MATLAB command line.

  2. You create a Java application by using the Deployment Tool GUI to build a Java class that wraps around your MATLAB code.

    To compile or build the Java application using the Deployment Tool, use the following information as you work through this example in Building the Java Component:

    Project Namevararg_java
    Class Namevararg_javaclass
    File to compilevarargexample.m

Compiling Your Java Code

Use javac to compile the Java source file VarArgServletClass.java from demo folder JavaCode\src\VarArg.

javac.exe should be located in the bin folder of your JDK installation.

Ensure your classpath is set to include:

For more details about using javac, see the Sun Microsystems, Inc. Web site.

Generating the Web Archive (WAR) File

Web archive or WAR files are a type of Java Archive used to deploy J2EE and JSP servlets. To run this example you will need to use the jar command to generate the final WAR file that runs the application. To do this, follow these steps:

  1. Add javabuilder.jar to the WEB-INF\lib directory. For more information, see Helper Library Locations in the MATLAB Application Deployment Web Example Guide.

  2. Copy the JAR file created using the MATLAB Builder JA product into the JavaCode\build\WEB-INF\classes\VarArg demo folder.

  3. Copy the compiled Java class to the JavaCode\build\WEB-INF\classes\VarArg demo folder.

  4. From the folder JavaCode, use the jar command to generate the final WAR as follows:

    jar cf VarArgServlet.war -C build .

      Caution   Don't omit the . parameter above, which denotes the current working folder.

      Caution   Placing javabuilder.jar in the WEB-INF/Lib folder for a single Web application generally works. However, if another application also places javabuilder.jar in its WEB-INF/Lib locations, problems may occur. The native resources associated with javabuilder.jar can be loaded only once in an application. Therefore, javabuilder.jar must only be visible to a single class loader.

    For more information about the jar command, refer to the Sun Microsystems, Inc. Web site.

Running the Web Deployment Demo

When you're ready to run the application, do the following:

  1. Install the VarArgServlet.war file into your Web server's webapps folder.

  2. Run the application by entering http://localhost:port_number/VarArgServlet in the address field of your Web browser, where port_number is the port that your Web server is configured to use (usually 8080).

Using the Web Application

To use the application, do the following on the http://localhost/VarArgServlet Web page:

  1. Enter any amount of numbers to plot in the Data to Plot field.

  2. Select Line Color and Border Color using the Optional Input drop-down lists. Note that these optional inputs are passed as varargin to the compiled MATLAB code.

  3. Select additional information you want to output, such as mean and standard deviation, by clicking an option in the Optional Output area. Note that these optional outputs are set as varargout from the compiled MATLAB code.

  4. Click Display Plot. Example output is shown below using the Mean optional output.

  


Recommended Products

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

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