Mathematica 9 is now available
3.3 MSP Functions: Expand.jsp

Documentation3. Basic Examples

 

3.3 MSP Functions: Expand.jsp

If you installed webMathematica as described above, you should be able to connect to this JSP via http://localhost:8080/webMathematica/Examples/Expand.jsp. (You may have some other URL for accessing your server.)

When the submit button is pressed, the polynomial is raised to the power and expanded, and an HTML page that contains the result is returned. The source for this page is in webMathematica/Examples/Expand.jsp. A section that shows the form tag is shown below.

<%@ page language="java" %>
<%@ taglib uri="/webMathematica-taglib" prefix="msp" %>

<html>
<head>
<title>Expanding Polynomials</title>
</head>
<body>
<h1>Expanding Polynomials</h1>
<form action="Expand.jsp" method="post">
Enter a polynomial (e.g. x+y):
<input type="text" name="expr" size="10">
Enter a positive integer (e.g. 4):
<input type="text" name="num" size="3">
<br>
<msp:allocateKernel>
<msp:evaluate>
   MSPBlock[{$$expr, $$num},
      Expand[$$expr^$$num]]      secure computation with input variables
</msp:evaluate>
</msp:allocateKernel>
<input type="submit" name="submitButton" value="Evaluate">
</form>
</body>
</html>

This page contains form and input tags as described in the previous example. Additionally, the msp:evaluate tag refers to the MSP function MSPBlock.

When the form is submitted, the server connects to a Mathematica kernel, in which two symbols, $$expr and $$num, are assigned to the text from the two input elements. If no text is entered the symbols will not have any definition.

Mathematica now evaluates the contents of the msp:evaluate tag. The MSPBlock command is a programming construct, which here inspects two input variables, $$expr and $$num. If either of these has no value, MSPBlock returns a null string, which is why the first time you access the page, you do not see a result. The values of both variables are then interpreted by Mathematica. If successful, the results of interpretation are substituted into the second argument or body of MSPBlock. In this example all instances of $$expr are substituted with the parsed value of $$expr, and the same is done for $$num. The result is then evaluated, formatted, and placed in the HTML page, which is returned to the client.

Interpretation of the variables by Mathematica can fail in two ways: the input might not be valid Mathematica input (for example, f[}), or it might be dangerous input (such as ReadList["/etc/passwd"]). In both cases, the inputs are rejected and an error message generated. This demonstrates some of the security features of the system, which the Security section documents in detail. The use of variables is discussed further in Tips and Tricks: Variables.

The formatting of the result of an msp:evaluate tag is discussed in more details in Advanced Topics: Evaluation Formatting.



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.