Hello All,
I am very new to portlet development. I have no experience with servlet
development either. I am trying to show a graph in a portlet. I am using
JFreeChart library for charting.
My portlet container is JetSpeed2 (2.2.1). As a test case, first I
tested my code with a line of text. It worked. Then I tried to move on
to graphics, but my graphics never showed up in the portlet. I decided
to test if my portlet could show a static image stored on the disk. I
did not have any luck even with that. I have followed methodology/code
provided at
http://blogs.sun.com/satya/entry/new_feature_resource_serving_in
Can someone please point out the problem here? If you need more
information, please let me know. I really appreciate your help.
Thanks.
Prajesh
Following is my portlet code. Please note that I have tested the line
ChartUtilities.writeChartAsJPEG(image_out, quality, demo.chart, 400,300);
inside a stand-alone app in which image_out was a FileOutputStream.
********************************************************
import java.io.IOException;
import java.io.OutputStream;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import javax.portlet.ResourceURL;
import java.io.PrintWriter;
import org.jfree.chart.ChartUtilities;
// import org.jfree.ui.RefineryUtilities;
// import org.jfree.util.Log;
// import org.jfree.util.PrintStreamLogTarget;
public class ScatterPlot extends GenericPortlet {
public void doView(RenderRequest request, RenderResponse response)
throws PortletException, IOException {
// Log.getInstance().addTarget(new PrintStreamLogTarget());
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
ResourceURL resURL = response.createResourceURL();
resURL.setResourceID("image");
writer.println("<IMG src=\"" + resURL + "\" >");
}
public void serveResource(ResourceRequest resRequest, ResourceResponse
resResp)
throws PortletException, IOException {
resResp.setContentType("image/jpeg");
OutputStream image_out = resResp.getPortletOutputStream();
TimeSeriesDemo demo = new TimeSeriesDemo("Time Series");
float quality = (float)(0.75);
ChartUtilities.writeChartAsJPEG(image_out, quality, demo.chart,
400,300);
image_out.flush();
image_out.close();
}
} // end of class
**********************************************************************
Following is my portlet.xml.
*********************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<portlet-app id="ScatterPlot" version="1.0">
<portlet id="ScatterPlot_ID">
<description>Plot as many variables on the Y axis against the ONLY
variable on the X axis</description>
<portlet-name>ScatterPlot_Name</portlet-name>
<display-name>Scatter Plot</display-name>
<portlet-class>ScatterPlot</portlet-class>
<expiration-cache>-1</expiration-cache>
<supports>
<mime-type>*/*</mime-type>
<portlet-mode>VIEW</portlet-mode>
<portlet-mode>HELP</portlet-mode>
<portlet-mode>EDIT</portlet-mode>
<!-- support custom about mode -->
<portlet-mode>about</portlet-mode>
<!-- support custom edit_defaults mode -->
<portlet-mode>edit_defaults</portlet-mode>
<!-- support custom preview mode -->
<portlet-mode>preview</portlet-mode>
<!-- support custom print mode -->
<portlet-mode>print</portlet-mode>
</supports>
<supported-locale>en</supported-locale>
<portlet-info>
<title>Scatter Plot</title>
</portlet-info>
</portlet>
</portlet-app>
*********************************************************************
Following is my web.xml.
*********************************************************************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>ScatterPlot</display-name>
<description>Scatter Plot Portlet</description>
</web-app>
***********************************************************************
--
/ *Prajesh Bhattacharya, PhD | Lawrence Berkeley National Laboratory |
One Cyclotron Road, MS 90R3111, Berkeley, CA 94720-8134 | 510-486-7857
(W) | 480-586-1387 (M) | 510-486-4089 (Fax)* /