webMathematica

Tom Wickham-Jones
Wolfram Research Inc.

June 2001

Introduction

webMathematica is a new technology that allows the generation of dynamic web content with Mathematica. It integrates Mathematica with a web server.

Why Mathematica on the Web?

Probably the major reason for using Mathematica over the web is for computation. The system contains a large collection of functions for calculating in many areas and all of this functionality becomes available over the web. This is particularly important since many web technologies, so powerful in many areas, are not very suited to scientific computation, it is simply not their main focus. Mathematica is very suitable for scientific computation, and this is what it can provide for web computations.

Mathematica contains a high level, interactive, functional programming language. It lends itself to rapid prototyping but can scale up to large intensive computations. These benefits carry over to web content generation where they can significantly reduce the time spent developing the site.

Mathematica has long held a powerful, though complex, connection technology in MathLink. Now connecting to Mathematica has been dramatically simplified with the use of J/Link. This technology makes Mathematica more suitable for integrating into web computation.

The Mathematica front end has long provided the premium mechanism for working with the Mathematica kernel. One might expect that the front end has no place in a web infrastructure. In fact the front end and a browser give different and complimentary ways to use Mathematica. The latter gives a simpler more interactive interface;  people already know how to use browsers so the start-up time is reduced. The front end ultimately gives a more powerful way to work with Mathematica. It is possible to combine the two ways of interacting with Mathematica so that you can start in a browser and move to working in the front end.

Mathematica is known to be a premium system for interactive mathematical typesetting. What is not so well known is that it is a powerful system for working with MathML. MathML is designed to allow the use and re-use of mathematical and scientific content on the web,and by other applications. As MathML becomes increasingly important it is very advantageous that Mathematica works well with MathML.

webMathematica allows the many features and benefits of Mathematica to be used over the web.

Why a Web Interface?

A web interface brings many of its typical benefits to working with Mathematica.

A web browser is the most common type of software that people learn so people need less training to use a web interface.

There are many people who are expert in developing servers and dynamic web sites. There are many technologies and tools for working with the web. These people will be able to use their favourite tools to develop Mathematica related web sites. This makes the web applications that people build with Mathematica easier to build and more powerful.

A major benefit of the web is the ease of delivery, there is no need for installation of special software and access can be controlled and measured with standard technologies.

webMathematica allows the many features and benefits of web technology to be used with Mathematica.

Potential Areas of Use

Individual scientists and researchers often develop packages for special calculations, this might be done with Mathematica or with other computational tools. If they want to give these tools to their colleagues they often have a problem, their colleagues may not have access to or experience with the necessary components, and so it can be hard to share this computational work. webMathematica solves this problem. People who develop calculations in Mathematica can build sites to deliver interactive calculations over the company web intranet. The people who connect to and use these sites don't have to know anything about Mathematica, in fact they don't need to know they are even using Mathematica. In addition, since they are using web access there are no special requirements for installing and administering software on individual machines. Finally, companies can develop sites that provide computational services to a broader audience than just their own intranet, for example, they can provide custom calculators for special customers.

Schools, universities, and other educational establishments can use webMathematica to deliver sophisticated courses over  the web, including highly interactive courseware.

Authors and publishers can publish live interactive technical books, supplements and journals over the web with webMathematica.

webMathematica provides a way for researchers, scientists, and artists to showcase their Mathematica-based work using interactive web calculators.

Design Goals

webMathematica was developed according the following design goals:

Technology Standards:   using standard technology will make webMathematica faster to develop and easier to use.

Consistency with Mathematica:  webMathematica should work with existing Mathematica applications and technology, it should also be available on different platforms and operating systems.

Scalability:  webMathematica should work for both small and large sites.

Compatibility:  webMathematica should be compatible with many different client and server technologies.

Customizable:  users should be able to use parts of webMathematica technology in their own web solutions.

Dynamic Web Technology

The web is formed from a large collection of servers, programs that wait for requests for material. Requests are sent from clients which are often operated by users who want to collect information from a server. Typically, a server provides an application that supports the Hyper Text Transfer Protocol (HTTP). Common servers include the Apache Web Server and Microsoft Internet Information Server. Another class of server are application servers; these are large programs that support HTTP as well as connections to data bases and other tools. The client software will very often  be a web browser such as Internet Explorer, Netscape Navigator, or Mozilla. In order for a web site to support interactive computation it needs to provide some dynamic technology, this can be supported either by the client or the server.

There are several technologies that provide dynamic content from a server; examples are CGI scripting, Active Server Pages, and Server Plug-ins. In addition there are some Java based technologies such as Java Servlets and Java Server Pages. The latter are both standard technologies for dynamic web content, available on all platforms, which can be integrated with most web technologies. webMathematica is based on Java Servlets.

There is a number of client technologies that support dynamic behaviour. All of these technologies can be integrated to work with each other. This includes HTML forms, JavaScript, applets, plug-ins and Active-X controls. webMathematica can work with all of these client technologies.

Mathematica Server Pages

Mathematica Server Pages (MSP) technology is the foundation of webMathematica. It is based on a standard Java technology called servlets. Servlets are special Java programs that run on a web server machine. Typically support is provided by a separate program called a servlet container (or sometimes a "servlet engine") which connects to the web server. Essentially all modern web servers support servlets natively or through a plug-in servlet container. This includes Apache, Microsoft's IIS and PWS, Netscape Enterprise Server, iPlanet, and application servers  (such as IBM WebSphere).

MSP technology allows a site to contain  HTML pages which are enhanced by the addition of Mathematica commands. When a request is made for one of these pages, which are called MSP scripts, any Mathematica commands are evaluated and the computed result is placed in the page.

Requirements

The aim of MSP technology is to reduce the amount of extra knowledge required for developing a site to a minimum; in practice this means knowing about HTML and Mathematica. It also aims to automate the management of the site to make running, maintenance and configuration as convenient as possible.

Knowledge

In order to install and maintain an MSP site it is necessary that you have experience of installing and maintaining a web site. You do not need any special knowledge of Java other than installing a Java runtime.

In order to write MSP scripts it is necessary to have experience of HTML. You will also need some experience with Mathematica. You do not need any knowledge of Java and you do not need to know about JavaScript.

Equipment

In order to run a webMathematica site the minimum technical components are:

○ A Servlet Container supporting the 2.0 API (or higher)

○ A Java Runtime Environment (JRE) 1.1 (or higher)

There are many different combinations of hardware and operating systems that support these components, it is probably fair to say that most systems that run Mathematica will support an MSP site. At present we are  focusing on support for Intel/Windows, Intel/Linux and Sun/Solaris. It is possible to run on other platforms, but we have not provided the same level of validation.

MSP Scripts

MSP scripts contain HTML that is marked up with Mathematica commands. They are easy to write and fit well with HTML development mechanisms such as HTML editors. Here is a sample MSP script.

<html>
  <head>
    <title>Expanding Polynomials</title>
  </head>
  <body>
    <h1>Expanding Polynomials</h1>
    <form action="Expand" method="post">
      Enter a polynomial (eg x+y): 
      <input type="text" name="expr" size="10">
      Enter a positive integer (eg 4):
      <input type="text" name="num" size="3">
      <br>
      
      <%Mathlet
             MSPBlock[{$$expr,$$num},
                     Expand[$$expr^$$num]] %>
                     
      <br>
      <input type="submit" name="button" value="Evaluate">
    </form>
  </body>
</html>

This is very much standard HTML except for the Mathlet tag. This contains the Mathematica commands that are to be evaluated when the page is requested from the server. Here the input polynomial, referenced by the variable $$expr is raised to the power given by $$num, and expanded. The web page that is sent back to the client contains the computed result. MSPBlock is a command provided by webMathematica for working with input sent with an HTTP request. There is a large collection of Mathematica commands for working with web requests and responses that take care of features such as plotting, formatting and typesetting, embedding applets, and returning general content.

Setting Up

Setting up a webMathematica site involves first installing and configuring a server that supports Java servlets. When this is running the webMathematica tools are then installed on the server. These contain all the material that is necessary to support the site, including:

○ A Mathematica application. Support for the Mathematica commands.

○ A Mathematica session manager. Software for launching and shutting down Mathematica sessions, as well as support for configuring and monitoring.

○ A Web Application. An application for loading into the servlet container.

○ Example MPS scripts.

○ Sample configuration files.

○ Documentation.

○ Applets.

○ Images.

Technology Overview

The core of the webMathematica site is the MSP servlet. This uses the Mathematica session manager, a  useful application which maintains a pool of Mathematica sessions which are waiting to be used. When a request is received, the MSP servlet obtains a Mathematica kernel and uses it to process the MSP script. When this is finished the kernel is returned to the pool and the result returned to the browser. A given Mathematica session is reused many times and this leads to an improvement in performance. This arrangement allows one server to work with more than one Mathematica kernel, allowing concurrent computations.

The result of a request to a webMathematica site can be anything that can be generated by Mathematica: this includes HTML, MathML, XML, images and Mathematica notebooks. The results can interoperate with applets, javascript, plug-ins and Active-X controls.

The Mathematica session manager takes care of launching and initializing Mathematica kernels. It also takes care of shutting down sessions that exceed some preset limit for computations. The whole system is fully configurable to provide detailed control of a particular site.

An overview of the steps in processing a page is shown below, for the sake of illustration this has a Mathematica session pool with 3 sessions.

The Number of Processes

webMathematica provides automatic Mathematica pooling. Requests will take Mathematica sessions until none are available; further requests wait until a Mathematica session is available. Since several computations are being done at the same time, this is a form of threading or parallel computation.

webMathematica can work with multiple Mathematica processes, but the optimal number of processes suitable for an individual site is a very difficult to determine in general. There are many different parameters and no simple formula.

Typically a server benefits from threads because one request may need to wait for something, for example reading from or writing to a file, loading a DLL, or launching an external process. All of these are opportunities for the server to run some other thread. With webMathematica these do not apply so readily, Mathematica is often computing, the system sits in some tight loop crunching away until it gets your result.

Some Points

On a single CPU if you run one Mathematica calculation that takes 20 seconds, the same calculation run simultaneously on 10 Mathematica processes will take 200 seconds.

If you have a multi-processor machine you can benefit from as many Mathematica sessions as you have processors.

If your system is running at a low load or a high load it does not really matter how many Mathematica processes you have. You can gain some advantage in some middle range, this is probably more useful if you have a wide variation in the time required for computations.

A major factor is the time that the computation takes in Mathematica, you can measure this with the Mathematica Timing command. webMathematica gives very efficient access to the Mathematica, there is some small fixed cost for each request, but the time is dominated by the computation.

webMathematica scales linearly with the time for computations and the number of computations. Scaling linearly means that there are no hidden bottle necks and that you can solve performance problems by improving hardware.

webMathematica Licensing

There are two licenses for webMathematica. Note that the number of users of your web site does not influence the license.

webMathematica Amateur

This is a low-cost version intended for non-commercial use. Web sites must display a webMathematica banner and must be freely accessible.

The Amateur license is provided as a benefit of Mathematica subscription, premier service. Certain special purchase programs provide a number of webMathematica Amateur licenses.

webMathematica Professional

This is for restricted web sites, commercial or in some type of intranet. If the site is part of the materials for an on-line course restricted to particular students, this would need the Professional version.

The professional license also has some restrictions: sites cannot allow general Mathematica access and cannot replicate Mathematica 'look and feel'.

The cost of a professional license is a premium over regular Mathematica, but remember that volume discounts apply. If you are part of a special purchase program with discounts, which can be significant, these will apply to webMathematica.

Summary

webMathematica allows a web server to generate dynamic content with Mathematica. It is driven by a technology called Mathematica Server Pages. MSP technology works by placing MSP scripts, HTML pages that contain Mathematica commands, on your server. When a request for a page is received these commands are evaluated by Mathematica and the result is returned. MSP technology is based on Java servlets, a standard and fully supported Web technology. They are fully integrated with many client and server web technologies.

WRI provides the necessary tools for a Java servlet enabled web server. It is necessary for someone wanting to host a webMathematica site to provide the server.

More information on webMathematica can be found at http://www.wolfram.com/products/webmathematica, and examples of webMathematica can be found at http://library.wolfram.com/explorations.