WEBMATHEMATICA TUTORIAL

Developing Your Own Pages

Once you have installed and configured a webMathematica server so that the examples run correctly and have studied the basics of writing material for webMathematica, as described previously, you are ready to start developing your own material.

One way to start is to make your own area in the webMathematica web application. You could make a directory here (for example NewScripts) and copy one of the samples (for example Plot.jsp) from the Examples directory. You could then access this script with the URL http://localhost:8080/webMathematica/NewScripts/Plot.jsp.

This might be a good time to revisit the webMathematica index page found at http://localhost:8080/webMathematica/index.html, which provides a number of links that demonstrate features of webMathematica. When you actually want to write your own material, you may look at the Tips and Tricks described in this chapter. The chapter continues to describe other page development utilities that are part of webMathematica.

Wolfram Workbench

Wolfram Workbench provides tools for developing solutions and applications based on Wolfram products such as Mathematica and gridMathematica, http://www.wolfram.com/products/workbench. It also includes support for webMathematica.

More information can be found in the Workbench documentation, but the following gives a summary of some of the main features.

webMathematica Projects

Wolfram Workbench is a project-based system, and to work with webMathematica it supports a type of webMathematica project. This allows you to develop your web pages and Mathematica code, perhaps archiving it in a code repository. You can test the individual components, and then deploy them to the server to actually use them.

Syntax Support for JSP Pages

Wolfram Workbench provides a variety of syntax support for JSP pages. This includes showing both XML, HTML, and JSP syntax errors. Of particular value are reports on Mathematica syntax errors inside of evaluate tags.

Authoring Pages

Wolfram Workbench provides a number of tools that help writing JSP and HTML pages. These include command completion and preview features. In addition, a palette is provided that allows templates for entire page structures to be added.

Wizards

Wolfram Workbench offers a number of wizards for creating webMathematica material such as an entire project or a new web page.

Server Interaction Tool

Wolfram Workbench contains a tool that works with the actual server. From this you can carry out tasks such as starting or stopping the server, deploying your project, and connecting with a debug session.

Full webMathematica Documentation

Wolfram Workbench contains the webMathematica user guide, along with other material specific to the Workbench tools. This is probably the most convenient way to read the documentation.

Debugging for Mathematica Code

Wolfram Workbench provides a debugger for Mathematica code. You can use this debugger to connect to the server and follow your code as it executes, setting breakpoints to halt in particular locations.

More information is available in the debugging section.

Tips and Tricks

This section provides a summary of a few issues that will help you to get started writing your own pages. These are all described in more detail in later sections of the documentation, but are collected together here in a brief description. Getting a good grasp of these points will help you to make progress in developing your site.

Variables

There are two types of variables that are important for you to understand when you are getting started with webMathematica: input variables and page variables.

Input variables come with the HTTP web request, for example from an input field in an HTML form. You can identify input variables in Mathematica code because they are labeled with a '$$' prefix. In the example below, the setting variable may be sent from an input field. In Mathematica code it is called $$setting.

<input type="text" name="setting" />

<msp:evaluate>
If[ MSPValueQ[ $$setting],
....
]
</msp:evaluate>

You should be aware that input variables are a potential security risk to your server. Therefore, you should always use the special functions, MSPBlock and MSPToExpression, for converting into Mathematica input. In particular, you should never use ToExpression on an input variable. An example of using MSPBlock is shown below.

<input type="text" name="fun" />

<msp:evaluate>
MSPBlock[ {$$fun},
Integrate[ $$fun, x]
]
</msp:evaluate>

Page variables are Mathematica variables that you use to hold intermediate values. They are called page variables since they are cleared when the page is finished. In the example below, the page variable tmp is used to hold the expression that was entered into the text input field (which is held in an input variable called $$expr). Note the use of the secure function, MSPToExpression, to convert the Mathematica expression from the input.

<input type="text" name="expr" />

<msp:evaluate>
tmp = Null;
tmp = MSPToExpression[ $$expr] ;
</msp:evaluate>

<p>
<msp:evaluate>
If[ tmp =!= Null,
....
]
</msp:evaluate>
</p>

If you want your variable to persist from one page to another, you can declare it as a session variable. This and further details of variables are discussed in detail in Advanced Topics: Variables.

Coding in Pages

The purpose of webMathematica is to use Mathematica for web computation; a key part of this is placing Mathematica code in your web pages. This is done with evaluate tags, as follows.

<msp:evaluate>
Integrate[ 1/(1-x^3),x]
</msp:evaluate>

Note that the Mathematica code will evaluate in the typical way for Mathematica and the result of the computation will appear in the web page. You can use MSPFormat to change the way that the result is formatted; more information on formatting in webMathematica is found in Advanced Topics: Evaluation Formatting. An example of MSPFormat is shown below; this formats the integral into TraditionalForm using a GIF image to display the result.

<msp:evaluate>
MSPFormat[ Integrate[ 1/(1-x^3),x], TraditionalForm]
</msp:evaluate>

If you do not wish to see the result in the web output, you can suppress it by using a semicolon ';'. In the following example an assignment is made to the variable x, but no output appears.

<msp:evaluate>
x = 109;
</msp:evaluate>

A final tip for working with code in webMathematica pages is the separation of multiple computations in a single evaluate tag by using a semicolon ';'. This is shown below.

<msp:evaluate>
x = 109;
y = 44.5;
{x+y}
</msp:evaluate>

More information on coding in webMathematica pages is found in Appendix: MSP Taglib, which gives a detailed reference on the webMathematica tags.

Templates

webMathematica provides a number of templates and other utilities that can be used to incorporate more design into your webMathematica material.

Browse Examples

The webMathematica examples can be reached from the webMathematica home page, which you should be able to reach via http://localhost:8080/webMathematica. (You may have some other URL for accessing your server.) The home page shows examples wrapped up in a template that adds more design around the pages to give them a better visual appearance. This template makes use of HTML frames and so it would be relatively easy to modify your own work to make use of it.

Design Examples

As you develop your own material, you may wish to look at the design examples. These are a collection of samples that make use of colors, fonts, and images for a more professional appearance. You can access the design examples from the main index page, or with a URL such as http://localhost:8080/webMathematica/DesignTemplates/DesignTemplate1.jsp.

Banners and Buttons

A collection of banners and buttons are available for use in your pages, which you can find with the link http://localhost:8080/webMathematica/BannersImages/. To use one of these images, such as the banner webm-white.gif, you can use an img tag such as the following.

<img src="/webMathematica/Resources/Images/webm-white.gif" />

The section on including static files has more information on how to include images.

Certain license options for webMathematica require that you use an approved banner for your site. You may use one of these banner images in order to comply with this requirement.

Minimal Installation

When you have confirmed that your webMathematica site is running correctly and you start to develop your own material, you may wish to strip out all of the documentation and examples to get a minimal installation. The minimum set of files for webMathematica is that everything in the WEB-INF directory must be kept.

Minimal File Layout

webMathematica
WEB-INF everything in here

In addition you can remove all the J/Link native libraries from webMathematica/WEB-INF/lib/SystemFiles/Libraries except for the library required for your system, which is located in a directory named by $SystemID.