Mathematica 9 is now available
3.9 Applets: TextApplet.jsp

Documentation3. Basic Examples

 

3.9 Applets: TextApplet.jsp

This example demonstrates how to call on the services of a Mathematica-powered web site from an applet. This shows a combination of client and server programming. The section involves some programming in Java.

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

First, here is the JSP source.

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

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

<body text="#171717" bgcolor = "#ffffff">
<html>
<title>Applet Test</title>
<body bgcolor="#ffffff">
<p>
Here is an applet that gets a result from Mathematica:
<br>

<msp:allocateKernel>
<msp:evaluate>
   If[ MSPValueQ[ $$Compute],
      MSPReturn[ "Date[] returns " <> ToString[ Date[]], "text/plain"]]
</msp:evaluate>
<applet
   code="TextApplet"
   archive =
      "<msp:evaluate> $WebApplication <> "/Resources/applets/MSPExamples.jar"</msp:evaluate>"
   width="400"
   height="30" >
   <param name="ArgumentURL" value="TextApplet.jsp?Compute=True">
</applet>
</msp:allocateKernel>

<p>
Hitting refresh will cause the page to update.
</body>
</html>

Here is the source for the applet TextApplet.java.

import java.applet.Applet;
import java.awt.*;
import java.net.*;
import java.io.*;
public class TextApplet extends Applet
{
public void paint(Graphics g)
{
super.paint(g);
try {
URL url=new URL(getDocumentBase(), getParameter("ArgumentURL"));
InputStream in=url.openStream();
ByteArrayOutputStream out=new ByteArrayOutputStream();
byte[] b=new byte[1024]; int len;
while((len=in.read(b, 0, 1024)) != -1) {
out.write(b, 0, len);
}
b=out.toByteArray();
g.drawBytes(b, 0, b.length-1, 20, 20);
}
catch (Exception e) {
System.out.println("Error "+e);
}
}
}

This is a very simple applet; the paint method opens a connection to a URL, the name of which is formed from the document that loaded the applet, and the value of the parameter ArgumentURL, which is passed in from a param tag. This causes the TextApplet JSP to be called and return a computation of the date.



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.