ChartDirector 7.0 (Java Edition)

BaseChart.makeSession


Usage

public String makeSession(HttpServletRequest request, string name [, int imageFormat [, String filename [, boolean asAttachment ]]])

Description

Generates the chart as an image, saves it to a session variable and returns query parameters for loading the image.

Note: This method is designed to address the limitation of older HTML standard that only allows the <IMG> tag to contain the URL of an image, but the image itself. The browser, after receiving the HTML, will need to connect to the URL with a separate HTTP connection to get the image.

The HTML was designed that way because when the web was invented, people were still using 2400 bps modem. Loading an image could be very slow, and the network connection was charged on a per minute basis. It was better to see the text first, and load the image later via separate connections. Earlier browsers even allowed users to disable loading images entirely.

Nowadays all browsers support embedding the images in HTML. Network speed is much faster that viewing video is common. Newer ChartDirector sample code no longer use this API.

The makeSession method is designed to support web applications in which the chart is generated in the same script that generates the HTML.

Older HTML standard that only allows the the <IMG> tag to contain the URL of an image, but the image itself. The browser, after receiving the HTML, will need to use a separate HTTP connection to get the image. That means the script that generates the HTML with the chart image cannot put the chart image in the web page. One way is to save the chart image as a file using a unique filename, and then include the url of that image file in the <IMG> tag. This would create many files on the server and another mechanism is needed to clean up old files.

The makeSession method saves the image in a session variable, avoiding creating image files on the hard disk. Almost all web servers today support session variables. The server will destroy the session variable when the user session ends, usually after a period of no activity.

The makeSession method returns query parameters to allow the image to be loaded with the ChartDirector utility script "getchart.jsp".

In a typical application, the chart is created like:

String chart1URL = c.makeSession(request, "chart1");

The web page then includes an <IMG> tag as follows:

<img src='<%=response.encodeURL("getchart.jsp?"+chart1URL)%>'>

Note that the above assumes "getchart.jsp" is in the same directory as your script. If it is in a different directory, you would need to modify the path in the <IMG> tag. Also, the directory that contains "getchart.jsp" must be in the same web application (servlet context) as your script, otherwise "getchart.jsp" will be unable to access the session variables created by your script.

As an alternative to "getchart.jsp", ChartDirector also has a GetSessionImage servlet that performs the same function. This is useful for servlet applications that do not use JSP.

For example, if you have mapped "*.chart" to GetSessionImage, you may use "getchart.chart" as the URL instead of "getchart.jsp". Please refer to the Installation section for details.

Note: The getimage.jsp may not work properly in some very old web servers. In this case, the getimage.jsp will automatically redirect itself to the GetSessionImage servlet. Therefore, it may be useful to install the GetSessionImage servlet even if you may not need it.

On the browser side, if the user attempts to save the image, such as by right clicking on the image and select "Save Image As" or something similar, the browser by default should derived a filename from the image URL. When makeSession is used, the URL is that of the script file "getchart.jsp". Using this as the filename of the image may not be desirable. In this case, the filename argument can be used to suggest an alternative filename to the browser.

When the image is sent to the browser, HTTP allows it to be marked as an attachment. In this case, if the receiving HTML element is an <IFRAME>, <FRAME> or the browser window itself, the browser should save it as a file instead of displaying it. Some browsers may prompt the user for the directory to use, while others may save it to a special "download directory" without prompting the user. The asAttachment argument can be used to mark the image as an attachment.

Arguments

ArgumentDefaultDescription
request(Mandatory)The HttpServletRequest object representing the current request.
name(Mandatory)The name of the session variable used to store the chart image. If there are multiple chart images in the same web page, each image must use a different name.
imageFormatPNGA constant representing the format of the image. Must be one of the predefined constants PNG, JPG, GIF, BMP, WMP, SVG, SVGZ or PDF.
filename""The suggested filename to use when user saves the chart image as a file on the browser side.
asAttachmentfalseSpecifies whether the image is intended as an attachment.

Return Value

A string representing the query parameters for retrieving the image using "getchart.jsp" or the GetSessionImage servlet.