a cup of java

Here’s your cup of hot java & web 2.0 cookies. Enjoy it.

Archive for the ‘JSP’ Category

Displaying dynamic charts in a web page (using JFreeChart & JSP)

with 4 comments

This code snippet will tell you how to create a chart (using JFreeChart) on the fly and displaying it in a webpage.

getChart.jsp

< %@ page import="java.io.*" %>
< %@ page import="org.jfree.chart.JFreeChart" %>
< %@ page import="org.jfree.chart.ChartUtilities" %>

< %
 try
 {
 File image = File.createTempFile("image", "tmp");

 // Assume that we have the chart
 ChartUtilities.saveChartAsPNG(image, chart, 500, 300);

 FileInputStream fileInStream = new FileInputStream(image);
 OutputStream outStream = response.getOutputStream();   

 long fileLength;
 byte[] byteStream;

 fileLength = image.length();
 byteStream = new byte[(int)fileLength];
 fileInStream.read(byteStream, 0, (int)fileLength);

 response.setContentType("image/png");
 response.setContentLength((int)fileLength);
 response.setHeader("Cache-Control",
     "no-store,no-cache, must-revalidate, post-check=0, pre-check=0");
 response.setHeader("Pragma", "no-cache");

 fileInStream.close();
 outStream.write(byteStream);
 outStream.flush();
 outStream.close();

 }
 catch (IOException e)
 {
 System.err.println("Problem occurred creating chart.");
 }

%>

How to use it in a webpage:

<img src="getChart.jsp" alt="Progress chart" />

We can make it more dynamic using various parameters. See the sample below.

<img src="getChart.jsp?type=bar&action=showProfits" alt="Progress chart" />

Useful links: creating charts using JFreeChart
Creating BarChart with custom colors using JFreeChart
Creating a 3D BarChart using JFreeChart
Creating Time Series charts using JFreeChart
Creating Pie Chart using JFreeChart (Using plot)
Creating Pie Chart using JFreeChart

Written by Subinkrishna G

July 10, 2007 at 7:16 am

Posted in Code Snippets, Graphics, JSP, Tips, Web

Tagged with

Creating custom downloads using JSP

without comments

This code snippet will help you to create custom/runtime downloads upon user request. In this code snippet, the code will create a download for an image in the server.

String fileName = config.getServletContext().getRealPath("") + "image.jpg";
String displayFileName = "myFile.jpg";
long fileLength;
byte[] byteStream;
File fileToDownload = new File(fileName);
FileInputStream fileInStream = new FileInputStream(fileToDownload);
OutputStream outStream = response.getOutputStream();
fileLength = fileToDownload.length();
byteStream = new byte[(int)fileLength];
fileInStream.read(byteStream, 0, (int)fileLength);
response.setContentType("image/jpg");
response.setContentLength((int)fileLength);
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "no-cache");
response.setHeader("Content-Disposition", "attachment; filename=" + displayFileName);
fileInStream.close();
outStream.write(byteStream);
outStream.flush();

Here, evenif the browser can display the JPEG image coming from the the server as the response, will ask the user to either “Open” or “Save” it. B’ coz we are setting the  “Content-Disposition” header to “attachment”.

Written by Subinkrishna G

June 27, 2007 at 10:35 am

Posted in Code Snippets, JSP, Web