ChartDirector 7.0 (Java Edition)

WebChartViewer.streamChart


Usage

public static boolean streamChart(HttpServletResponse response, byte[] image[, String filename[, boolean asAttachment])

Description

Sends the image out as a MIME stream.

The optional filename argument is the suggested filename in case it is needed on the browser side. For example, if the user saves the image to the hard disk, the browser will usually derive a filename based from the URL. For dynamically generated images, the URL is a script, and using it as the image filename may not be meaningful. In this case, the filename can be used to specify the filename to use.

The optional asAttachment argument specifies if the MIME stream is an attachment. When the browser receives an attachment, instead of displaying it, it should save it as a file. 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.

Arguments

ArgumentDefaultDescription
response(Mandatory)An HttpServletResponse object used to encode the output.
image(Mandatory)The in-memory image.
filename""The suggested filename to use when user saves the chart image as a file on the browser side.
asAttachmentfalseSpecifies whether the stream is an attachment.

Return Value

This method always return true.

Why this method always returns true

In JSP, anything that is outside the code block <% ... %> or <%! ... %> will be sent to the browser as text. Usually, these text are HTML. JSP will translate the text into Java statements that send the text to the browser.

The purpose of streamChart is to send the binary image back to the browser. Any text will corrupt the image. So there should be nothing outside the code blocks. In practice, it is common to find invisible empty space or empty lines after the code block. To avoid these invisible text to corrupt the image, after calling streamChart, the code can immediately issue a "return;" statement.

However, this does not work for JSP. JSP will still generate the code to sends the invisible text to the browser. These code will never be executed because of the "return;" statement. It is a compiler error in Java to have "unreachable code" - code that can never be executed from the compiler's point of view.

To work around this issue, the following code can be used:

if (myWebChartViewer.streeamChart(response, myImage)) return;
From the compiler's point of view, the "return;" is conditional, so it is still possible for code after the return statement to execute. In practice, the return statement is always executed because streeamChart always returns true.

In summary, the purpose of the "true" return value is to provide a "true" value that is unpredictable to the compiler. This can work around unreachable code automatically generated by JSP.