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" %>
<body bgcolor="#ffffff" >
This jsp uses the MSP Taglib to demonstrate passing
Java values to Mathematica.
int num = 10;
Object obj = new Object();
<msp:set name="var1" intValue="<%= num %>" />
<msp:set name="var2" objectValue="String from java" />
<msp:set name="var3" objectValue="<%= obj %>" />
Nest[ f, x, var1]
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.