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

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/

First, here is the JSP source.

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

<title>Live 3D Plotting</title>

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

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

Hitting refresh will cause the page to update.

Here is the source for the applet

import java.applet.Applet;
import java.awt.*;
public class TextApplet extends Applet
public void paint(Graphics 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((, 0, 1024)) != -1) {
out.write(b, 0, len);
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.