Display a MATLAB Plot on the Web using a Java Servlet

Overview

This example demonstrates how to display a plot created by a Java® servlet calling a class created with the MATLAB® Compiler SDK™ 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 Specify Optional Arguments.

Prerequisites

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 Compiler SDK

  • Java Development Kit (JDK™)

    Ensure you have a JDK installed on your system. You can download it from Oracle, Inc.

    Note

    You should be using the same version of Java that ships with MATLAB. To find out what version of Java MATLAB is running, enter the following MATLAB command:

    version -java

    Caution:MathWorks® only supports the Oracle® JDK and JRE™. A certain measure of cross-version compatibility resides in the Oracle software and it may be possible to run applications with compiled MATLAB functions with non-Oracle JDKs under some circumstances—however, compatibility is not guaranteed.

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 example (Implement a WebFigure) 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.

Locating and Copying the Example Files

The example files are located in the matlabroot\toolbox\javabuilder\Examples\java_web_vararg_demo folder.

Contents of the Example Files

The example files contain the following three folders:

  • mcode — Contains all of the MATLAB source code.

  • JavaCode — Contains the required Java files and libraries.

  • compile — Contains some helpful MATLAB functions to compile and clean up the example.

    Note

    As an alternative to compiling the example code manually and creating the application WAR (Web Archive) manually, you can run compileVarArgServletDemo.m in the compile folder. If you choose this option and want to change the locations of the output files, edit the values in getVarArgServletDemoSettings.m.

    If you choose to run compileVarArgServletDemo.m, consult the readme file in the download for additional information and then skip to Running the Web Deployment Example.

Copy the Example Files

Copy the folder java_web_vararg_demo containing example files to a local folder. Failure to do so may lead to errors.

Build Your Java Package

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

  1. Open the Library Compiler app.

  2. Create the Java package using the Library Compiler app to build a Java class that wraps around your MATLAB code.

    To compile the Java package using the Library Compiler app, use the following information as you work through this example in Compile Java Packages with Library Compiler App:

    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 example folder JavaCode\src\VarArg.

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

Ensure your classpath is set to include:

  • javabuilder.jar — included with MATLAB Compiler SDK

  • vararg_java.jar — the JAR file you just built

  • servlet-api.jar — included as part of the servlet container

For more details about using javac, see the Oracle website.

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. Copy the JAR file created using the MATLAB Compiler SDK product into the JavaCode\build\WEB-INF\classes\VarArg example folder.

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

  3. 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.

    For more information about the jar command, refer to the Oracle Web site.

Running the Web Deployment Example

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.

Was this topic helpful?