webMathematica Tutorial | Functions |

There are a number of tags that are provided by web*Mathematica*. The key tags are those that allow web pages to interact with *Mathematica*. The way they operate is discussed in this section.

evaluate | evaluate input to Mathematica and insert the result in the output page |

evaluateQueued | queue input to Mathematica to be evaluated later |

set | set a Mathematica variable with the value of a page expression |

get | get the result of a Mathematica computation and use it to set a page expression |

<%@ page language="java" %>

<%@ taglib uri="http://www.wolfram.com/msp" prefix="msp" %>

<html>

<title>page</title>

<body>

<msp:evaluate>

eval1

</msp:evaluate>

<msp:evaluate>

eval2

</msp:evaluate>

</body>

</html>

web*Mathematica* contains many examples of these pages; several are described in detail in the Examples section.

The first task is to determine which pool is to be used for the request. This is based on the full name of the JSP using the URLPattern configuation setting.

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.

Note that each request may get a completely different kernel. You cannot rely on saving anything in your

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. web*Mathematica* 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 backquote (`), and any underscore (_) is replaced with a "U". This mapping of names is consistent with the way that *J/Link* maps names.

Each variable is then validated to ensure it only contains letters or digits as well as the dollar ($) and backquote (`) 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 doublequote (") characters are escaped with additional backslash (\) characters. If the value starts with an exclamation (!), a space is added. Finally, doublequotes (") are added around the result.

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

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

If the configuration parameter KernelAcquireCode has been set for the pool this will be executed at this time.

The evaluate tag exists to evaluate *Mathematica* commands inside of a JSP. The body of the tag is evaluated by *Mathematica.* You can use the full range of MSP functions inside an evaluate tag. Each tag uses the kernel that was allocated by the first evaluate tag.

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.

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

If you wish to calculate more than one result in an evaluate tag, the different steps must be separated with a semicolon ';'. 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.

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

When the request is finished, all the evaluate tags will have been processed. At this time the request will be terminated and the following steps carried out for postprocessing.

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.

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

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.

If the configuration parameter KernelReleaseCode has been set for the pool, this will be executed at this time. Note that if the kernel has been terminated due to a restart the release code will not be executed.

The set tag exists to use Java to set a *Mathematica* symbol. Each set tag uses the kernel that was allocated by request initialization to evaluate its contents.

It should be noted that until the request is finished the same kernel will be used for all the tags evaluate, set, and get, thus any definitions and commands made in one will be visible in others. These definitions are cleared out when the request is terminated.

The get tag exists to get a value from *Mathematica* into Java. Each get tag uses the kernel that was allocated by request initialization to evaluate its contents.

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

It should be noted that until the request is finished the same kernel will be used for all the tags evaluate, set, and get, thus any definitions and commands made in one will be visible in others. These definitions are cleared out when the request is terminated.

The evaluateQueued tag exists to carry out long running computations with web*Mathematica.* It is described in detail in the section on queuing of long calculations.

Attributes of the evaluateQueued tag.