Writing Your First JSP: Hands-On

Chris Williamson and Brendan Elli

Saturday October 8, 005

Focus

Focuses on developing a JSP using webMathematica.

Does not focus on installing webMathematica or supporting it on a particular servlet engine.

Tags

<msp:allocateKernel>

The msp:allocateKernel tag is used to obtain a Mathematica kernel for computation.

<msp:evaluate>

The msp:evaluate tag exists to evaluate Mathematica commands inside of a JSP. You can use the full range of MSP functions inside an msp:evaluate tag. Each tag uses the kernel that was allocated by the msp:allocateKernel tag to evaluate its contents.

Example:
<msp:allocateKernel>
<msp:evaluate>
      Date[]
</msp:evaluate>
</msp:allocateKernel>

See: HelloWorld

Mathematica variables

Any input variables that were sent with the request are passed to the Mathematica kernel with their values and assigned a symbol.  The request input sym=fun is set to $$sym with a value of "fun" in Mathematica.

Example:
<msp:allocateKernel>
<msp:evaluate>
      $$sym
</msp:evaluate>
</msp:allocateKernel>

See: Variables

Expression Security

A major danger to any Mathematica site is that someone will try to send commands to Mathematica that may breach the security of the server. These commands can be sent as the value of input variables passed in from the server.

Example:
ReadList["/etc/passwd"]

MSPBlock, MSPToExpression, MSPValue

MSPBlock

This is one of the key ways to work with variables from the HTTP request. MSPBlock takes each of the variables , interprets them, and then replaces any occurrences in body with the interpreted value. If any variables do not have values, an empty string is returned. If a security or parse error is encountered when interpreting the value, an MSPException is thrown.

See: Expand

MSPToExpression

MSPToExpression is the webMathematica version of ToExpression; it provides the same functionality for turning strings into Mathematica input. However, it provides some additional features. The most important feature is that it carries out a security check on its input.

See: Integrate

MSPValue

This is a utility function that is useful for extracting the value of variables with a default.  If the value is not a string, this would indicate a programmatic error by the page author.  If the variable is not a Mathematica symbol, this would indicate a programmatic error by the page author.

See: Plot

Type Setting

MSPFormat

This is one of the important ways to format results from Mathematica.  The formatted result can appear in the different format types that Mathematica provides for output. These include OutputForm, InputForm, StandardForm, TraditionalForm, and MathMLForm. In addition the result can be returned as HTML, an image format, or as MathML.  The second argument of MSPFormat is a symbol that selects the Mathematica format type, and the third argument is a string that sets the actual content type of the result.

See: Integrate

Plotting

MSPShow

MSPShow is the main way to include graphical results from Mathematica for inclusion in an HTML page.

See: Plot

MSPExportImage

MSPExportImage is used to save an image using the Mathematica command Export. If the first argument is a graphics object or a Cell or BoxData object, it is displayed directly. If it is a general expression, it is formatted with TraditionalForm.

Return Content Other Than  HTML

MSPReturn

MSPReturn allows a page to return something that is not an HTML result. For example, a page can return a Mathematica notebook directly to the client.

See: Content

HTML Integration

HTMLTableForm

The function HTMLTableForm takes an input and formats it into an HTML table.

See: Regression Table

HTMLFormat

The function HTMLFormat provides some useful formatting functions into HTML. It is suitable for formatting small expressions such as numbers as shown below.  It is less suitable for formatting large expressions, since everything will come out in InputForm.

See: FindRoot

HTMLSelect

The function HTMLSelect provides a useful way to generate select tags with webMathematica. It takes a list of the different options and the name to be used when the selection is submitted. Its operation is shown below.

See: HTML Select

HTMLCheckBox

The function HTMLCheckBox provides a useful way to generate an input checkbox tag with webMathematica. It takes the name to use when the checkbox is submitted as an argument . Its operation is shown below.

Sessions

MSPSessionVariable

This is a scoping construct for declaring a variable to be a session variable. The values of a session variable will be stored in a session managed by the servlet container. These sessions are a standard feature of modern web servers/browsers and are used to hold state on a server. This means that a value will live from one call of the server to another.

See: DICOM

JLink and webMathematica

$ServletConfig

$ServletConfig holds a reference to the ServletConfig object of the servlet transaction. It is possible to make J/Link calls on this object as documented in the section Server APIs.

$ServletRequest

$ServletRequest holds a reference to the HTTPServletRequest object of the servlet transaction. It is possible to make J/Link calls on this object as documented in the section Server APIs.

$ServletResponse

$ServletResponse holds a reference to the HTTPServletResponse object of the servlet transaction. It is possible to make J/Link calls on this object as documented in the section Server APIs.

See: Request Information

Java Integration

Embedding Java code is also supported within JSPs.

<msp:set>

The msp:set tag exists to use Java to set a Mathematica symbol. Each msp:set tag uses the kernel that was allocated by the msp:allocateKernel tag to evaluate its contents.

See: msp:set

<msp:get>

The msp:get tag exists to get a value from Mathematica into Java. Each msp:get tag uses the kernel that was allocated by the msp:allocateKernel tag to evaluate its contents.

See: msp:get

JSP Expression Language

The JSP expression language is a great way to embed Java variables into HTML.  It is often useful to separate out the functionality of a web page from the design of the web page.  This really helps designers concentrate on the design without having to worry about breaking the functionality.  For webMathematica users, Mathematica code can do something specific and then place its results into a Java object.  This object could then be used inside of HTML that a designer has generated using the expression language.  It is a nice tool for separating code within a JSP from the HTML, so it is friendlier to developers who design the look of a web page.

Example:
${param.foo}

This feature is only supported by servlet engines that implement JSP 2.0 (including Tomcat 5.0 and Tomcat 5.5).  Often this feature needs to be turned on to use.  This can be done in Tomcat using something like...
<%@page language="java" isELIgnored ="false"%>

See: Implicit Objects

Configuring webMathematica

WEB-INF

This directory contains all the files used to support the webapp such as configuration files, Java libraries, and Mathematica packages.

WEB-INF/Applications

This directory is used to store Mathematica packages and files.  This directory is added to the $Path of the kernel used in webMathematica, \(so\ packages\ may\ be\ easily\ \(\(loaded\)\(.\)\)\)

WEB-INF/lib and WEB-INF/classes

These directories are used to store Java libraries and classes respectively.  These files may be used inside Java code launched by JSPs or servlets and Mathematica code using JLink.

WEB-INF/web.xml

This file is used to configure the webapp.  This file is used to launch webMathematica as well as any other servlets, filters, etc that the webapp may wish to launch.

WEB-INF/conf/MSP.conf

This file is used to configure the webMathematica kernel pool.  The user may set the path to launch Mathematica here as well as some other things such as kernel timeout, verbose logging and more.

Tips

Write Mathematica code in packages that may reused and debugged in Mathematica.

Put Java code in servlets, filters, or classes.  Don't use JSPs.

Look for Java libraries and Tag libraries to help something you want to do.  Struts is a very useful package for creating larger sites.  It has an API for templating and implements an MVC pattern that allows for a very organized site.  JSTL is another very useful taglib that may help with the second tip as well as give access to database tags among other things.

Use Eclipse for very nice Java and Mathematica development tools.  Use it to launch webMathematica in Tomcat and debug it directly in Eclipse.