i want to show a image using servlet,which is being called by a java file (beans).
in that java how will i get the parameter eg: poll-id=1,2 or 3...n from that jsp page?....here what i've done(.java file) but the its not showing the image(error: data not available):
package myapp.webwork.beans;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;
import org.jfree.data.jdbc.*;
import org.jfree.data.general.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.image.*;
import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.urls.*;
import org.jfree.chart.entity.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities.*;
import org.jfree.chart.imagemap.*;
import java.io.PrintWriter.*;
import java.lang.String.*;
import java.lang.Integer.*;
import org.jfree.chart.ChartRenderingInfo.*;
public class votepie {
Connection con;
PreparedStatement st;
ResultSet rs;
//int id;
/**
* @param args
*/
public PieDataset readData(int id) {
JDBCPieDataset data = null;
String url = "jdbc:mysql://localhost/vote";
//Connection con;
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
//id = rs.getString("poll_id");
con = DriverManager.getConnection(url, "vote", "vote001");
data = new JDBCPieDataset(con);
st = con.prepareStatement("SELECT option_text, counter FROM VOTE_VOTES where poll_id=?");
//PreparedStatement st= con.prepareStatement(sql);
st.setInt(1, id);
rs = st.executeQuery();
//data.executeQuery(sql);
con.close();
st.close();
rs.close();
}
catch (SQLException e) {
System.err.print("SQLException: ");
System.err.println(e.getMessage());
}
catch (Exception e) {
System.err.print("Exception: ");
System.err.println(e.getMessage());
}
return data;
}
public String getvoteserve(HttpServletRequest request, HttpServletResponse response) {
votepie pd = new votepie();
int id = new Integer (request.getParameter("poll_id"));
// create the chart...
JFreeChart chart = ChartFactory.createPieChart(
"Vote Result Pie Chart", // chart title
pd.readData(id), // data
true, // include legend
true,
false
);
// set the background color for the chart...
chart.setBackgroundPaint(Color.cyan);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setNoDataMessage("No data available");
// OPTIONAL CUSTOMISATION COMPLETED.
ChartRenderingInfo info = null;
HttpSession session = request.getSession();
try {
//Create RenderingInfo object
response.setContentType("text/html");
info = new ChartRenderingInfo(new 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 = new PrintWriter(response.getWriter());
ChartUtilities.writeImageMap(writer, "imageMap", info, true);
writer.flush();
}
catch (Exception e) {
// handle your exception here
}
String pathInfo = "http://";
pathInfo += request.getServerName();
int port = request.getServerPort();
pathInfo += ":"+String.valueOf(port);
pathInfo += request.getContextPath();
String chartViewer = pathInfo + "/servlet/voteserve";
return chartViewer;
}
}
another thinh when I put thvalue of the "poll_id" manually into the SQL query the image is being shown. so may be the prob lies here. or is it so?....plz kindly suggest what to do....