You can use both servlets and JSPs to return a chart back to the browser. The chart can either be shown in an applet (i.e., it can be interactive) or be shown as a static image (i.e., a jpeg, a png etc.). Net traffic and bandwidth availability should be the key factors in deciding whether to use applets or static images.
The charts returned to the browsers can be done in two ways: they can be saved to a file or sent directly to the browser. In the first scenario, the chart is saved on the server side and the file location is sent to the client browser which then shows the chart. In the second scenario, the chart itself is sent, either as a string or as an output stream, to the client browser.
Servlets and JSPs can save charts to files if the charts shown need to be re-used or a copy is needed. Here, charts are saved to a file on the server side (whether the chart is shown in a JNLP (More about Applets in JNLP: Section 2.6 - Run Applets in WebStart with JNLP file) or as a static image is immaterial). However, these types of servlets and JSPs need to be constructed carefully. If multiple clients hit the page, it is possible for one client to see another client's chart. The browsers might also sometimes cache a previous chart (in the case of a static image) and they will not show the new chart unless refreshed/reloaded. Since the files are saved on the server side, one must be careful not to override the files before the client browser has seen the chart. They must be purged periodically in order to avoid taking up server space.
In both cases, a chart is exported to the desired format.
QbChart chart = new QbChart (...........); .... .... .... int format = QbChart.CHT; // here set to an interactive chart. For a static chart change to QbChart.JPEG chart.export(format, "chart", 500, 500);
An HTML file can then be returned to the browser, which redirects the page to a JSP file, which contains the chart location within the JSP file so that the browser can view it.
<?xml version="1.0" encoding="utf-8"?> <jnlp spec="1.0+" href="EspressViewer.jnlp"> <information> <title>Espress Viewer</title> <vendor>Quadbase Systems Inc.</vendor> <offline-allowed/> </information> <resources> <j2se version="1.8+" max-heap-size="1024m"/> <jar href="lib/EspressViewer.jar"/> <jar href="lib/jsqlparser.jar"/> </resources> <security> <all-permissions/> </security> <applet-desc name="Chart Viewer" main-class="quadbase.chartviewer.Viewer" width="640" height="480"> <param name="filename" value="help/examples/ChartAPI/data/test.tpl"/> <param name="preventSelfDestruct" value="true"/> </applet-desc> <update check="always" policy="always"/> </jnlp>
Chart Viewer has been used here. Please note that in the above case, EspressManager MUST be running.
In the case of a static image, a simple <img src>
tag giving the location of the static image is sufficient to display the chart.
Using this method, you can separate the pages returning the chart from your servlet/JSP and design them well in advance and outside of your servlet/JSP. You can use a database or a hashtable to keep track of the charts generated and show them again as needed, as well as have the charts re-generated again periodically.
For examples, please refer to the files in the help/examples/servlets/DatabaseJPEG
directory and help/examples/jsp/Chart
directories.
Servlets and JSPs can send charts directly to the browser so that they need not be saved as files on the server side. Thus, disk space on the server is not being filled up and maintenance of files need not be done. However, a permanent copy of the chart cannot be made (unless it is printed from the browser). Therefore, the chart has to be re-generated if it is needed again.
Sending charts to the browser differs from saving them to files, as here, charts are sent either as an outputstream (in the case of static images) or as string (in the case of an interactive chart).
For a static image, on the servlet side, an outputstream is created and the chart is exported to the outputstream.
public class OutputStreamServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // first, set the "content type" header of the response response.setContentType("image/html"); // where response is the response to the servlet OutputStream toClient = res.getOutputStream(); QbChart chart = new QbChart (...........); .... .... .... chart.export(QbChart.JPEG, toClient, 500, 500); } }
On the HTML part, an <img src>
tag with the servlet enclosed is sufficient to view the chart image.
<HTML><BODY> <img src="http://machine_name:8080/servlet/OutputStreamServlet> </BODY></HTML>
You can also output the map file as an outputstream and embed it within your generated HTML file. Generating the map file as an outputstream can be done using the following method in the QbChart
class:
export(int format, OutputStream image, OutputStream mapFile, String fileName, int w, int h, boolean generateMapFile, int option);
For an interactive chart, the chart is exported as a string.
QbChart chart = new QbChart (...........);
String buffer = chart.exportChartToString();
....
....
....
On the HTML side, you can use Chart Viewer to view the interactive chart. Here, the HTML is embedded in the servlet code itself for applet(More in: Section 2.6 - Run Applets in WebStart with JNLP file) as shown :-
toClient.println("<applet-desc main-class=\"quadbase.chartviewer.Viewer\" width=600 height=600 >”); toClient.println("<PARAM name=\"ChartData\" value=\"" + chart.exportChartToString() + "\">"); toClient.println("</applet-desc>"); toClient.println("<PARAM name=\"ChartData\" value=\"" + chart.exportChartToString() + "\">"); toClient.println("</applet>");
For examples, please refer to the files in the help/examples/servlets/StreamingJPEG
and help/examples/servlets/StreamingChart
directories.
On a JSP, you have to create a Java Bean object (myStreamChart
), which creates a chart and returns it as a String object. The JSP page has to be modified as shown :-
<jsp:useBean id="myStreamChart" scope="page" class="streamingChart.CreateChart" /> <jsp:setProperty name="myStreamChart" property="*"/> <applet-desc main-class="quadbase.chartviewer.Viewer" width=600 height=600 > <PARAM name="ChartData" value="<%= myStreamChart.export()%>"> </applet-desc>
where myStreamChart
java bean will contain the following code :-
public string export() throws Exception { QbChart chart = new QbChart(....); return chart.exportChartToString(); }
For an example, please refer to the files in help/examples/jsp/streamingChart
.
In order to send a static image directly to the browser, a combination of JSP and servlet is used.