webMathematica >
webMathematica Tutorial

Returning General Content

The typical result of a webMathematica request is an HTML page, which might include references to images. The commands available for webMathematica are designed to make this type of request very convenient. However, it is also very useful to be able to return other formats such as Mathematica notebooks or TeX documents. Mathematica commands for generating these other formats are Export and ExportString. When these other formats are returned to a browser, it can often launch a helper application that provides special functionality for that format. This section discusses how to use webMathematica to return general content of different formats.

Direct Return

The simplest way to return content type other than HTML is to write a page that only returns your data. If your data is static, you should insert it into the page. Alternatively, if it has to be generated each time by Mathematica, you should just have an evaluate tag that returns your data and not use any HTML markup. It is a good idea to set the content type at the top of the page. This technique can be useful for AJAX and web service interactions. An example follows.
An alternative to using a page directive is to use the ContentType option of MSPPageOptions. The following example demonstrates how this can be done.
It is probably more convenient just to use the page directory.

MSPReturn

When an MSP script evaluates MSPReturn, the processing of the script is terminated and the first argument is immediately returned. The second argument specifies the content type. In this example a notebook object is returned, and the result is set to be application/mathematica.
Certain HTTP clients can use the content type to launch a helper application. However, some clients need a filename to be associated with the request. For this purpose, MSPReturn takes a third argument that sets the filename in an HTTP header. An example follows.
However, for some HTTP clients (for example Internet Explorer) this has the undesirable effect of causing the client to display two Open or Save dialog boxes. Most clients work much better if the request for the script that contains the MSPReturn uses the filename with an appropriate extension. Since the extension for webMathematica requests has to end in .jsp, this is not possible. An alternative is to generate a URL that has the correct extension; this functionality is provided by MSPURLStore.
MSPReturn is useful when the commands are embedded inside an existing page and you just want to terminate processing the page and return the result. It is simpler to use the direct return technique, so this would be preferred if it is possible.

MSPURLStore

MSPURLStore uses the mechanism that webMathematica provides for storing images generated by commands such as MSPShow. It actually stores its argument on the server and returns a URL that references the argument.
In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
In[3]:=
Click for copyable input
Out[3]=
The URL is relative to the request that contained the MSPURLStore. It contains a unique identifier and a description of the content type. Since the server steadily deletes stored information on the server, the information will not remain on the server indefinitely. This mechanism is particularly useful for preparing input for plug-ins and applets.
MSPURLStore can also take a third argument to set the filename, which is put into the URL that is returned. For example, the filename of notebook.nb is set in this example.
In[4]:=
Click for copyable input
Out[4]=
Setting the filename can be helpful for the browser to choose the correct helper application. The example script Examples/ContentStore.jsp has an example of MSPURLStore.
MSPURLStore is useful as a way of returning content if you need a URL such as in a src attribute of an img tag. This can be useful when working with dynamic client technologies such as AJAX or Flash.