Mathematica 9 is now available
3.10 JavaScript: PlotScript.jsp

Documentation3. Basic Examples

 

3.10 JavaScript: PlotScript.jsp

This example demonstrates how to integrate a Mathematica-powered web site with JavaScript. It also demonstrates both client and server programming. The section involves some programming in JavaScript.

Note that JavaScript and Java are different languages. JavaScript is a scripting language that is useful for manipulating documents and other features of browsers. Java is a general purpose programming language that can be used in an HTML document via an applet. The two languages complement each other; JavaScript is useful for manipulating the browser and documents that are open in the browser, while Java has a more sophisticated collection of functions and can draw into the browser window. It is possible for JavaScript and Java to work together.

If you installed webMathematica as described above, you should be able to connect to this MSP via http://localhost:8080/webMathematica/Examples/PlotScript.jsp. (You may have some other URL for accessing your server.) The source for this page is in webMathematica/Examples/PlotScript.jsp and webMathematica/Examples/PlotScript1.jsp.

First, here is the source for PlotScript.jsp.

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

<html>
<head>
<title>Live 3D Plotting</title>
</head>

<body text="#171717" bgcolor = "#ffffff">
<html>
<head>
<SCRIPT>
<!--- Hide script from old browsers
function plot(f) {
    win = window.open( "PlotScript1?fun=" +
    escape(f.fun.value) + "&x1=" + escape(f.x1.value),
    "plot", "toolbar=none");
    win.resizeTo( 450, 350);
}
// end hiding from old browsers -->
</SCRIPT>
<title>Plot</title>
</head>
<body>
<h1>Plot</h1>
<form action="Plot" method="post">
<msp:allocateKernel>
Enter a function:
<input type="text" name="fun" size="24" value =
"<msp:evaluate> MSPValue[ $$fun, "Sin[x]^2"] </msp:evaluate>">
Enter a number:
<input type="text" name="x1" size="24" value =
"<msp:evaluate> MSPValue[ $$x1, "10"] </msp:evaluate>">
<hr>
<input type="button" value="Plot" onClick="plot(this.form)">
</msp:allocateKernel>
</form>
</body>
</html>

Second, here is the source for PlotScript1.jsp.

<html>
<head>
<title>Function Plot</title>
</head>
<body bgcolor="#ffffff">
<msp:allocateKernel>
<msp:evaluate>
   MSPBlock[ {$$fun, $$x1},
      MSPShow[ Plot[$$fun, {x, 0, $$x1}, ImageSize->400]]]
</msp:evaluate>
</msp:allocateKernel>
<form action="Plot" method="post">
<input type="button" value="Close" onClick="window.close()">
</form>
</body>
</html>

This is a simple example given to demonstrate how a JSP can work with JavaScript. The initial page, PlotScript.jsp, puts up a page with a form of two text input elements and one submit button. When the button is clicked, it opens a new window that contains the output of PlotScript1.jsp.



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.