Mathematica 9 is now available
3.11 Setting Variables: SetBasic.jsp

Documentation3. Basic Examples

 

3.11 Setting Variables: SetBasic.jsp

If you installed webMathematica as described above, you should be able to connect to this JSP via http://localhost:8080/webMathematica/Examples/SetBasic.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. This example uses the Java programming language which makes it different from most webMathematica examples which do not require any Java programming. The source for this page is in webMathematica/Examples/SetBasic.jsp and is shown below.

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

<html>
<head>
<title>Set Example</title>
</head>
<body bgcolor="#ffffff" >

<h1>Set Example</h1>

<p>
This jsp uses the MSP Taglib to demonstrate passing
Java values to Mathematica.
</p>

<%
int num = 10;
Object obj = new Object();
%>

<msp:allocateKernel>

<msp:set name="var1" intValue="<%= num %>" />
<msp:set name="var2" objectValue="String from java" />
<msp:set name="var3" objectValue="<%= obj %>" />

<msp:evaluate>
Nest[ f, x, var1]   
</msp:evaluate>

<msp:evaluate>
Characters[ var2]
</msp:evaluate>

<msp:evaluate>
var3@hashCode[]
</msp:evaluate>

</msp:allocateKernel>

</body>
</html>

In this example, a variable num, which is an int, str, which is a String, and obj, which is an Object, are created in the JSP. These are then passed to Mathematica using the msp:set tag. This tag takes two attributes, the name attribute gives the name that the variable will be given in Mathematica, while the value attribute refers to the value. If the variable is of a primitive type, such as int, char, or double, then it needs to use the appropriate value attribute, such as intValue, charValue, or doubleValue. Notice how msp:set sends a Java int as a Mathematica integer and a Java String as a Mathematica string. The Java Object is sent as a Mathematica object reference. The rules that govern how types are sent from Java to Mathematica are exactly those that J/Link uses.



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.