This is documentation for an earlier version of webMathematica.
View current User Guide (Version 3.3)

Documentation3. Basic Examples


3.12 Getting Variables: GetBasic.jsp

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

This example passes values computed in a JSP into Mathematica where they are used for computation by Mathematica. The source for this page is in webMathematica/Examples/GetBasic.jsp, and a selection is shown below.


   num = Random[Integer, {1, 10}];
   list = Table[ Random[], {num}] ;
   sum = Apply[ Plus, list]/num;

<msp:get name="dArray" type="double[]" value="list" />
<msp:get name="dValue" type="Double" value="sum" />

The average is <%= dValue %>.

<table border>
<% for ( int i = 0; i < dArray.length; i++) { %>
<tr><td><%= dArray[i] %></td></tr>
<% } %>


In this example Mathematica generates a list of random numbers, and computes the mean. The JSP obtains these values using the msp:get tag. This has a name attribute, which governs the name of the variable in Java, a type attribute, which governs the type that will be used in Java, and a value attribute, which gives the Mathematica expression that must be evaluated. Only reference types such as Object, String, or arrays can be sent with this tag. In addition the type given in Java must be compatible with the type that Mathematica has sent; if this is not the case, then an exception will be thrown. The rules for transmission from Mathematica are that the normal J/Link type conversions will be applied, but if none of these applies, then the object will be converted into an object of type com.wolfram.jlink.Expr, a class that is provided by J/Link to represent general Mathematica expressions.

This particular example requires programming in both Java and Mathematica. It shows how easy it would be to incorporate webMathematica into an existing JSP framework.