Mathematica 9 is now available
5.10 Returning General Content

Documentation5. Applications


5.10 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. The functionality is based on two commands: MSPReturn and MSPURLStore.

5.10.1 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.

MSPReturn[ "Notebook[Cell[\"Hello\", \"Title\"]]", "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.

MSPReturn[ "Notebook[Cell[\"Hello\", \"Title\"]]",
      "application/mathematica", "notebook.nb"]

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.

5.10.2 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.


m=DisplayString[Graphics[Line[ {{0, 0}, {1, 1}}]], "JPEG"];

MSPURLStore[m, "image/jpeg"]


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.

MSPURLStore[ "Notebook[Cell[\"Hello\", \"Title\"]]", "application/mathematica", "notebook.nb"]


The example script Examples/ContentStore.jsp has an example of MSPURLStore.

5.10.3 MSPPageOptions/ContentType

The ContentType option of MSPPageOptions offers a way to set the content type of a particular page. Generally this would be placed at the top of a page. The following example shows how a page can set its return type to be MathML.

   MSPPageOptions[ ContentType -> "text/mathml"]

   MSPFormat[ Integrate[ 1/(1-x^3), x], StandardForm, RawMathML]

This is, of course, quite similar to MSPReturn. It is different in that it returns the entire page, whereas MSPReturn returns only its first argument.

Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information