Resource Serving Example

The JSR 286 portlet API introduced resource serving with method serveResource(…), which enables portlets to serve resources, for example, markup or binary data (PNG, PDF, etc), from within the portlet context using resource requests. A separate servlet is no longer required. This feature also encourages a better programming model.

This example uses resource serving to generate binary content (PNG image) from a portlet.

Resource Serving Portlet:

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 com.liferay.util.bridges.mvc.MVCPortlet;
 * Portlet implementation class QAMetrics
public class QAMetrics extends MVCPortlet {
    public void serveResource(ResourceRequest request, ResourceResponse response)
            throws PortletException, IOException {
        String resourceID = request.getResourceID();
        if (resourceID.equals("chart-image")) {
            PortletChartUtilities.writeChartImageData(request, response);
    public void doView(RenderRequest renderRequest,
        RenderResponse renderResponse) throws IOException, PortletException {
        // Pass the resource URL as a bean called resourceUrl to the view JSP
        ResourceURL chartUrl = renderResponse.createResourceURL();
        String resourceUrl = chartUrl.toString();
        renderRequest.setAttribute("resourceUrl", resourceUrl);
        include(viewJSP, renderRequest, renderResponse);

Data Transfer Object:

import javax.portlet.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
public class PortletChartUtilities {
    public static void writeChartImageData(ResourceRequest request, ResourceResponse response) throws IOException
        OutputStream out = response.getPortletOutputStream();
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(1.0, "Row 1", "Column 1");
        dataset.addValue(2.0, "Row 1", "Column 2");
        dataset.addValue(3.0, "Row 1", "Column 3");
        dataset.addValue(4.0, "Row 2", "Column 1");
        dataset.addValue(5.0, "Row 2", "Column 2");
        dataset.addValue(6.0, "Row 2", "Column 3");
        try {
            JFreeChart chart = ChartFactory.createBarChart("Bar Chart", "Category", "Value", dataset, PlotOrientation.VERTICAL, true, true, false);
            if (chart != null) {
                CategoryPlot plot = (CategoryPlot) chart.getPlot();
                BarRenderer renderer = (BarRenderer) plot.getRenderer();
                ChartUtilities.writeChartAsPNG(out, chart, 400, 300);
        } catch (Exception e) {
        } finally {

View JSP file view.jsp:

<%@ taglib uri="" prefix="portlet" %>
<%@ page import="javax.portlet.*"%>
<portlet:defineObjects />
<img src="${resourceUrl}"/>

Please remember to import the following libs that can be downloaded from


Screenshot of the portlet:

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License