Mathematica 9 is now available
4.1 Tips and Tricks

Documentation4. Developing Your Own Pages

 

4.1 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 User Guide, 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.

4.1.1 Variables

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

Input variables come with the HTTP request, for example from an input field in an HTML form. You can identify input variables in Mathematica code because they are labelled with a '$$' prefix. In the example below the setting variable may be set by 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 and 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.

4.1.2 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 <msp: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 semi-colon ';'. 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 <msp:evaluate> tag by using a semi-colon ';' 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.



Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT. CURRENT WEBMATHEMATICA DOCUMENTATION IS NOW AVAILABLE.