Mathematica 9 is now available
3.8 Returning General Content: Content.jsp

Documentation3. Basic Examples


3.8 Returning General Content: Content.jsp

If you installed webMathematica as described above, you should be able to connect to this JSP via http://localhost:8080/webMathematica/Examples/Content.jsp. (You may have some other URL for accessing your server.)

All of the examples up to this point return HTML to the browser, but the web can work with general content involving many different formats. MSPReturn is provided to allow an MSP to return arbitrary content. Here is an example that demonstrates how different formats can be returned. The source is in webMathematica/Examples/Content.jsp and webMathematica/WEB-INF/Applications/ExampleUtilities/Content.m.

First, here is the source.

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

<title>Live 3D Plotting</title>

<body text="#171717" bgcolor = "#ffffff">
<title>General Content</title>
<body bgcolor="#ffffff">
<h1>General Content</h1>

<form action="Content.jsp" method="post">
This example takes a format type and converts a notebook into
this format type. It returns the converted notebook. </p>

   If[ MSPValueQ[ $$button],
      Get[ "ExampleUtilities`Content`"];
      MSPReturn @@ GeneralContent[ $$button]]


Please select a format:
<input type="submit" name="button" value="Notebook">
<input type="submit" name="button" value="PostScript">
<input type="submit" name="button" value="GIF">

Here is the Mathematica source.

MakeNotebook[] :=
Module[ {nb, nbobj},
nb = NotebookCreate[] ;
NotebookWrite[ nb, Cell[ "A Dynamically Created Notebook", "Title"]] ;
NotebookWrite[ nb,
Cell[ "Converted to " <> $$button, "Subtitle"]] ;
NotebookWrite[ nb, Cell[ "The date is " <> ToString[ Date[]], "Text"]] ;
nbobj = NotebookGet[ nb] ;
NotebookClose[ nb] ;

GeneralContent[ fmt_] :=
Module[ {nbobj},
nbobj = MakeNotebook[] ;
{ToString[ nbobj, InputForm],
{DisplayString[ nbobj, "EPS"],
{DisplayString[ nbobj, "GIF"],
_, "Unknown format"]

In this example, one evaluation tests the variable $$button. If it has a value from activating one of the buttons in the form, this is used to specify a return format type and passed to a function, GeneralContent. The Mathematica code for this function is placed into a separate package to be loaded when the variable is set. GeneralContent calls a function that creates a very simple notebook, MakeNotebook. MakeNotebook generates a notebook using the Mathematica Notebook API and the J/Link function UseFrontEnd. In a real life situation a more interesting notebook would probably be generated. MSPReturn returns the representation of the notebook to the server with the content type. This is then returned to the browser, which, if suitably configured, will deploy the necessary helper application.

In a more advanced example, the dynamically generated notebook would probably use information sent with the request from the client.

If you wish to return special content and also set a filename to be used with this content, then you may wish to use the three-argument form of MSPReturn. This is described in the MSP Function Reference section.

Another way to set the content returned from an MSP script is to use MSPPageOptions. The topic of returning general content is discussed later.

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