giving all the concerned files so that it will be helpful for u to understand.
this is the bean file:votepie.java. it is used to create a chart. the class file is under WEB-INF/classes/beans folder.
<strong>package</strong> classes.beans;
<strong>import</strong> java.sql.Connection;
<strong>import</strong> java.sql.DriverManager;
<strong>import</strong> java.sql.*;
<strong>import</strong> org.jfree.data.jdbc.*;
<strong>import</strong> org.jfree.data.general.*;
<strong>import</strong> java.io.*;
<strong>import</strong> java.awt.*;
<strong>import</strong> java.util.*;
<strong>import</strong> java.awt.image.*;
<strong>import</strong> org.jfree.data.*;
<strong>import</strong> org.jfree.chart.*;
<strong>import</strong> org.jfree.chart.plot.*;
<strong>import</strong> org.jfree.chart.urls.*;
<strong>import</strong> org.jfree.chart.entity.*;
<strong>import</strong> javax.servlet.*;
<strong>import</strong> javax.servlet.http.*;
<strong>import</strong> org.jfree.data.general.DefaultPieDataset;
<strong>import</strong> org.jfree.chart.ChartFactory;
<strong>import</strong> org.jfree.chart.ChartUtilities.*;
<strong>import</strong> org.jfree.chart.imagemap.*;
<strong>import</strong> java.io.PrintWriter.*;
<strong>import</strong> java.lang.String.*;
<strong>import</strong> org.jfree.chart.ChartRenderingInfo.*;
<strong>public</strong> <strong>class</strong> votepie {
PreparedStatement st;
<strong>public</strong> PieDataset readData() {
JDBCPieDataset data = <strong>null</strong>;
String url = "jdbc:mysql://localhost/vote";
Connection con;
<strong>try</strong> {
Class.forName("com.mysql.jdbc.Driver");
}
<strong>catch</strong> (ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
<strong>try</strong> {
ResultSet rs = st.executeQuery();
<strong>int</strong> id = rs.getInt("poll_id");
con = DriverManager.getConnection(url, "vote", "vote001");
data = <strong>new</strong> JDBCPieDataset(con);
st = con.prepareStatement ("SELECT option_text, counter FROM VOTE_VOTES where poll_id=?");
st.setInt(1, id);
rs = st.executeQuery();
//data.executeQuery(sql);
con.close();
}
<strong>catch</strong> (SQLException e) {
System.err.print("SQLException: ");
System.err.println(e.getMessage());
}
<strong>catch</strong> (Exception e) {
System.err.print("Exception: ");
System.err.println(e.getMessage());
}
<strong>return</strong> data;
}
<strong>public</strong> String getChartViewer(HttpServletRequest request, HttpServletResponse response) {
votepie pd = <strong>new</strong> votepie();
// create the chart...
JFreeChart chart = ChartFactory.createPieChart(
"Vote Result Pie Chart", // chart title
pd.readData(), // data
true, // include legend
true,
<strong>false</strong>
);
// set the background color for the chart...
chart.setBackgroundPaint(Color.cyan);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setNoDataMessage("No data available");
// set drilldown capability...
//plot.setURLGenerator(new StandardPieURLGenerator("Bar3DDemo.jsp","section"));
//plot.setLabelGenerator(null);
// OPTIONAL CUSTOMISATION COMPLETED.
ChartRenderingInfo info = <strong>null</strong>;
HttpSession session = request.getSession();
<strong>try</strong> {
//Create RenderingInfo object
response.setContentType("text/html");
info = <strong>new</strong> ChartRenderingInfo(<strong>new</strong> StandardEntityCollection());
BufferedImage chartImage = chart.createBufferedImage(640, 400, info);
// putting chart as BufferedImage in session,
// thus making it available for the image reading action Action.
session.setAttribute("chartImage", chartImage);
PrintWriter writer = <strong>new</strong> PrintWriter(response.getWriter());
ChartUtilities.writeImageMap(writer, "imageMap", info, <strong>true</strong>);
writer.flush();
}
<strong>catch</strong> (Exception e) {
// handle your exception here
}
String pathInfo = "http://";
pathInfo += request.getServerName();
<strong>int</strong> port = request.getServerPort();
pathInfo += ":"+String.valueOf(port);
pathInfo += request.getContextPath();
String chartViewer = pathInfo + "/servlet/ChartViewer";
<strong>return</strong> chartViewer;
}
}
this is theservlet file: voteserve.java..the class file is under WEB-INF/classes/servlets folder.
<strong>package</strong> classes.servlets;
<strong>import</strong> java.io.*;
<strong>import</strong> java.awt.image.*;
<strong>import</strong> javax.servlet.*;
<strong>import</strong> javax.servlet.http.*;
<strong>import</strong> com.keypoint.PngEncoder;
<strong>public</strong> <strong>class</strong> voteserve <strong>extends</strong> HttpServlet {
<strong>public</strong> <strong>void</strong> init() <strong>throws</strong> ServletException {
}
//Process the HTTP Get request
<strong>public</strong> <strong>void</strong> doGet(HttpServletRequest request, HttpServletResponse response)
<strong>throws</strong> ServletException, IOException {
// get the chart from session
HttpSession session = request.getSession();
BufferedImage chartImage = (BufferedImage) session.getAttribute("chartImage");
// set the content type so the browser can see this as a picture
response.setContentType("image/png");
// send the picture
PngEncoder encoder = <strong>new</strong> PngEncoder(chartImage, false, 0, 9);
response.getOutputStream().write(encoder.pngEncode());
}
//Process the HTTP Post request
<strong>public</strong> <strong>void</strong> doPost(HttpServletRequest request, HttpServletResponse response)
<strong>throws</strong> ServletException, IOException {
doGet(request, response);
}
//Process the HTTP Put request
<strong>public</strong> <strong>void</strong> doPut(HttpServletRequest request, HttpServletResponse response)
<strong>throws</strong> ServletException, IOException {
}
//Clean up resources
<strong>public</strong> <strong>void</strong> destroy() {
}
}
guys please plz help me out....thanks