Saturday 29 April 2017

Send jFreeChart to jsp page

In my previous post, I explained how to send jfreeChart as servlet response. In this post, I am going to explain how to send jFreeChart as response to jsp page.


Step 1: Create new dynamic web project ‘jFreeChartDemo’
Give the project name as ‘jFreeChartDemo’.

Press Next.

Press Next.

Select the check box ‘Generate web.xml deployment descriptor’.

Press Finish.

Step 2: Mavenize the project. Right click on the project ‘jFreeChartDemo’ -> Configure -> Convert To Maven project

Use the defaults and press Finish.

Step 2: Create new package ‘com.sample. Define Hello.java like below.

Hello.java
package com.sample;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

public class Hello extends HttpServlet {

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  /*
          * Get the output stream from the response
          * object
          */
  try {
   DefaultCategoryDataset dataset = new DefaultCategoryDataset();
   dataset.addValue(500, "Company A Revenue", "2009");
   dataset.addValue(550, "Company A Revenue", "2010");
   dataset.addValue(450, "Company A Revenue", "2011");
   dataset.addValue(650, "Company A Revenue", "2012");
   dataset.addValue(550, "Company A Revenue", "2013");
   dataset.addValue(700, "Company A Revenue", "2014");

   dataset.addValue(200, "Company B Revenue", "2009");
   dataset.addValue(450, "Company B Revenue", "2010");
   dataset.addValue(398, "Company B Revenue", "2011");
   dataset.addValue(425, "Company B Revenue", "2012");
   dataset.addValue(770, "Company B Revenue", "2013");
   dataset.addValue(810, "Company B Revenue", "2014");

   /* create chart */
   JFreeChart chart = ChartFactory.createLineChart("", "Year", "Revenue in M$", dataset,
     PlotOrientation.VERTICAL, false, true, false);
   
   BufferedImage bufferedImage = chart.createBufferedImage(500, 500);
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   ImageIO.write(bufferedImage, "png", baos);
   baos.flush();
   byte[] imageInByte = baos.toByteArray();
   baos.close();
   
   request.setAttribute("image", imageInByte);
   
    request.getRequestDispatcher("/freeChart.jsp").forward(request, response);
  } catch (Exception e) {
   System.err
     .println(e.toString()); /* Throw exceptions to log files */
  } 
 }
}

Step 2: Define freeChart.jsp like below.


freeChart.jsp
<%@page import="java.io.*, javax.imageio.*, java.awt.image.*, javax.servlet.*, javax.servlet.http.*,org.jfree.chart.*, org.jfree.chart.plot.*, org.jfree.data.category.*"%>

<!DOCTYPE html>
<html>
<head>

<title>Insert title here</title>
</head>
<body>
 <%
   /* Set the HTTP Response Type */
   response.setContentType("image/png");
   
   byte[] imageInBytes = (byte[]) request.getAttribute("image");
   OutputStream outStream = response.getOutputStream();
   outStream.write(imageInBytes);
 %>

</body>
</html>

Update web.xml like below.


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>jFreeChartDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
   <servlet>
        <servlet-name>Hello</servlet-name>
        <servlet-class>com.sample.Hello</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Hello</servlet-name>
        <url-pattern>/HelloWorld</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

Run the application on server and hit the url ‘http://localhost:8080/jFreeChartDemo/HelloWorld’.


You can able to see following output.









Previous                                                 Next                                                 Home

No comments:

Post a Comment