| Contents | Index |
| On this page… |
|---|
Generating the Web Archive (WAR) File |
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.
This section describes what you need to know and do before you create the Web deployment example.
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.
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 |
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).
Ensure that the javabuilder.jar library (matlabroot/toolbox/javabuilder/jar/javabuilder.jar) has been installed into your Web server's common library folder.
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.
The demo 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 demo.
Note As an alternative to compiling the demo 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 Demo. |
Build your Java component by compiling your code into a deployable Java component .jar file.
Note For a more detailed explanation of building a Java component, including further details on setting up your Java environment, the src and distrib folders, and other information, see Getting Started |
Start deploytool at the MATLAB command line.
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 Name | vararg_java |
| Class Name | vararg_javaclass |
| File to compile | varargexample.m |
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:
javabuilder.jar (shipped with the MATLAB Builder JA product)
vararg_java.jar (shipped with the MATLAB Builder JA product)
servlet-api.jar (in the demo folder JavaCode\lib)
For more details about using javac, see the Sun Microsystems, Inc. Web site.
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:
Add javabuilder.jar to the WEB-INF\lib directory. For more information, see Helper Library Locations in the MATLAB Application Deployment Web Example Guide.
Copy the JAR file created using the MATLAB Builder JA product into the JavaCode\build\WEB-INF\classes\VarArg demo folder.
Copy the compiled Java class to the JavaCode\build\WEB-INF\classes\VarArg demo folder.
From the folder JavaCode, use the jar command to generate the final WAR as follows:
jar cf VarArgServlet.war -C build .
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.
When you're ready to run the application, do the following:
Install the VarArgServlet.war file into your Web server's webapps folder.
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).
To use the application, do the following on the http://localhost/VarArgServlet Web page:

Enter any amount of numbers to plot in the Data to Plot field.
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.
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.
Click Display Plot. Example output is shown below using the Mean optional output.

![]() | Optimization Example | Deploying a Java Component Over the Web | ![]() |

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 |