Mathematica 9 is now available
9.2 MSP Taglib Reference

Documentation9. Appendix

 

9.2 MSP Taglib Reference

The six tags that are provided by the MSP taglib are discussed in this section. There are many examples of their usage, in particular those described in the section Basic Examples.

The most basic usage of the MSP taglib is for a JSP to wrap an msp:allocateKernel tag around one or more msp:evaluate tags.

...
<msp:allocateKernel>

<msp:evaluate>
      ...
</msp:evaluate>

<msp:evaluate>
...
</msp:evaluate>

</msp:allocateKernel>
...

The following is a description of the operation of these tags. It provides a summary of the steps in processing a JSP that uses the MSP taglib.

9.2.1 msp:allocateKernel (tag open)

The msp:allocateKernel tag is used to obtain a Mathematica kernel for computation. The open tag takes the following steps for preprocessing the page.

Determine the Pool

msp:allocateKernel first determines what pool to use for the request based on the name of the JSP. If no pool was named, the General pool is used.

Allocate the Kernel

A Mathematica kernel is requested from the kernel pool. The pool maintains a collection of Mathematica kernels waiting for computations. If no kernel is available, the system waits until one is ready. Using a pool allows the system to share Mathematica kernels across multiple requests, which leads to a faster response time for the system.
Note that each request may get a completely different kernel. You cannot rely on saving anything in your Mathematica kernel and restoring it the next time.

Assign Input Variables

Any input variables that were sent with the request are then passed to the Mathematica kernel with their values. For a variable sym and value fun, a Mathematica assignment $$sym = "fun" is made. This ensures that the value is a Mathematica string, an inert object that will not evaluate without some special action. Note that input elements are not the only sources of variables. For example, an image map may cause transmission of variables. webMathematica renames these input variables, and this helps to ensure that they do not interfere with your Mathematica code.

Each variable is scanned to verify that it is a valid Mathematica symbol. Any "." character is replaced by a back-quote (`), and any underscore (_) is replaced with a "U". This mapping of names is consistent with the way that J/Link maps names.

Here are some samples of renamed variables.

Each variable is then validated to ensure it only contains letters or digits as well as the dollar ($) and back-quote (`) characters. This prevents an attack that sends a variable starting with an exclamation (!) character. This would be potentially dangerous because it might cause Mathematica to launch an operating system shell.

Each value is turned into a Mathematica string. For this, any backslash (\) and double-quote (") characters are escaped with additional backslash (\) characters. If the value starts with an exclamation (!), a space is added. Finally, double-quotes (") are added around the result.

Assign Parameters

Assignments to $ServletRequest, $ServletResponse, $ServletConfig, $ScriptName, $PathInfo, and $QueryString appropriate for this request are made in the kernel.

Initialization

The settings of $Context and $ContextPath are saved, and the lists used to store messages and print output are initialized.

9.2.2 msp:evaluate

The msp:evaluate tag exists to evaluate Mathematica commands inside of a JSP. You can use the full range of MSP functions inside an msp:evaluate tag. Each tag uses the kernel that was allocated by the msp:allocateKernel tag to evaluate its contents. If no kernel has been allocated, the request is forwarded to an error page.

If any MSPException is thrown, it will be caught by the processing code, and some suitable error message will be inserted.

If any MSPReturn command is evaluated, processing of the current evaluation and all other evaluations is terminated immediately, and its argument is returned directly from the JSP. If no MSPReturn command is encountered, the result of the evaluation is inserted into the output stream.

The processing of each evaluation is wrapped in TimeConstrained, the time parameter being computed from the KernelTimeLimit configuration parameter.

If Mathematica generates any messages or print output, these are stored so they can be retrieved with MSPGetMessages and MSPGetPrintOutput respectively.

The result of the msp:evaluate tag will be formatted and returned in the result. In the example below, the current date will appear in the output page.

<msp:evaluate>
   Date[]
</msp:evaluate>

If you wish to calculate more than one result in an msp:evaluate tag the different steps must be separated with a semi-colon ';'. The result of the last computation will be formatted and appear in the output page. In the example below, the numerical result of x+y will appear.

<msp:evaluate>
   x = Sin[5.6];
   y = Sqrt[x];
   x+y
</msp:evaluate>

More information on formatting of the result of msp:evaluate appears in the section on Evaluation Formatting.

It should be noted that within the scope of an msp:allocateKernel the same Kernel will be used for all the tags msp:evaluate, msp:set, and msp:get, thus any definitions and commands made in one will be visible in others. These definitions are cleared out by /msp:allocateKernel.

9.2.3 msp:allocateKernel (tag close)

When the msp:allocateKernel tag is closed, the following steps are taken for postprocessing.

Java Exceptions

If any Java exceptions were thrown while processing the JSP, these are caught and the kernel is shut down and restarted. The exception is then rethrown and it may be returned with the HTTP request.

MSPReturn

If a MSPReturn was encountered during an evaluation, its argument is returned instead of the normal output of the JSP.

Set ContentType

The content type is set. It is specified by a setting of the ContentType option of MSPPageOptions or by MSPReturn. The default is text/html.

Clean the Kernel

The kernel is cleaned so that it can be used again. This involves clearing the values of parameters that were sent with the request and removing all symbols in the default context. In addition $Context and $ContextPath are restored to their initial values, any Java object references are removed, and any open streams are closed.

Release the Kernel

The kernel is released to the pool so that it can be used again.

9.2.4 msp:set

The msp:set tag exists to use Java to set a Mathematica symbol. Each msp:set tag uses the kernel that was allocated by the msp:allocateKernel tag to evaluate its contents. If no kernel has been allocated, the request is forwarded to an error page.

The tag takes the following required attribute.

In addition, one of the following attributes must be set.

In the following example, the Mathematica variable var is set by the Java int num.

<msp:allocateKernel>

   <msp:set name="var" intValue="<%= num %>" />

</msp:allocateKernel>

An example of msp:set is given above.

It should be noted that within the scope of an msp:allocateKernel the same Kernel will be used for all the tags msp:evaluate, msp:set, and msp:get, thus any definitions and commands made in one will be visible in others. These definitions are cleared out by /msp:allocateKernel.

9.2.5 msp:get

The msp:get tag exists to get a value from Mathematica into Java. Each msp:get tag uses the kernel that was allocated by the msp:allocateKernel tag to evaluate its contents. If no kernel has been allocated, the request is forwarded to an error page.

The tag takes three required attributes, which are described below.

In the following example, the Java variable dValue with type Double is set to the result of the Mathematica function Random[].

<msp:allocateKernel>

   <msp:get name="dValue" type="Double" value="Random[]" />

</msp:allocateKernel>

An example of msp:get is given above.

It should be noted that within the scope of an msp:allocateKernel the same Kernel will be used for all the tags msp:evaluate, msp:set, and msp:get, thus any definitions and commands made in one will be visible in others. These definitions are cleared out by /msp:allocateKernel.

9.2.6 msp:includeClassicMSP

The msp:includeClassicMSP tag exists to include an MSP script into a JSP. MSP scripts are an older way of working with webMathematica described in the section Classic web Mathematica Technology.

The tag takes two attributes, which are described below.

The following example includes the result of the MSP script Examples/PlotSingle.msp. The arguments fun=Sin[x]&x1=10 are passed to the MSP script.

<msp:includeClassicMSP name="Examples/PlotSingle" arguments="fun=Sin[x]&x1=10" />

An example of msp:includeClassicMSP is given above.

9.2.7 msp:forwardClassicMSP

The msp:forwardClassicMSP tag exists to forward to an MSP script from a JSP. MSP scripts are an older way of working with webMathematica, described in the section Classic web Mathematica Technology.

The tag takes two attributes, which are described below.

The following example will forward to the MSP script Examples/PlotSingle.msp. The arguments fun=Sin[x]&x1=10 are passed to the MSP script.

<msp:forwardClassicMSP name="Examples/PlotSingle" arguments="fun=Sin[x]&x1=10" />

9.2.8 Overview

An overview of the steps in processing a page can be found in the introductory section on webMathematica Technology.



Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT. CURRENT WEBMATHEMATICA DOCUMENTATION IS NOW AVAILABLE.