I am new to JFreeChart
and trying to implement it in JSP
. I am accepting user input through a form on JSP
page and and sending to Servlet
page where my chart is formed after fetching data from database. After that I want to display the chart back on my JSP
page but unfortunately the chart is being displayed on my Servlet
page and the page is not redirected back to my JSP
page. I am not getting the reason why...
Here is my JSP page up.jsp
<form action="FormChart" class="values" method="post">
<table cellspacing="4" cellpadding="4">
<tr>
<td>Select Industry</td>
<td><select name="sta" class="dropdown">
<option>Industry</option>
<option>Cotton</option>
<option>Sugar</option>
</select></td>
</tr>
<tr>
<td>Select Category </td>
<td><select name="ind" class="dropdown">
<option>Category</option>
<option>Area</option>
<option>Production</option>
<option>Yield</option>
</select></td>
</tr>
<tr>
<td>Input Year Range</td>
</tr>
<tr>
<td>Year 1</td>
<td><input type="text" name="yr1" class="yearbox"/></td>
<td style="padding-left: 20px;">Year 2</td>
<td><input type="text" name="yr2" class="yearbox"/>
</tr>
<tr>
<td><input type="submit" value="Submit" style="margin-top: 10px;" name="bt" class="btn btn-primary btn1"/></td>
</tr>
</table>
</form>
<div style="border:1px solid; float:left;width:55%;margin-top:10px;margin-left:20px;border-radius:5px;height:400px;">
<img src="/FormChart" WIDTH="600" HEIGHT="400" BORDER="0" usemap="#chart"/>
<%--<jsp:include page="/FormChart" />--%>
</div>
Here is my Servlet Page FormChart.java
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/png");
OutputStream ot = response.getOutputStream();
String qry;
String y1, y2;
String indus;
float a;
int b;
DefaultCategoryDataset categoryDataset = new DefaultCategoryDataset();
String st;
String img;
categoryDataset = new DefaultCategoryDataset();
try
{
y1 = request.getParameter("yr1");
y2 = request.getParameter("yr2");
st = request.getParameter("sta").toLowerCase();
indus = request.getParameter("ind").toLowerCase();
qry = "select year," + indus + " from " + st + " where year between '" + y1 + "' and '" + y2 + "' and state='Uttar Pradesh'";
utility ut = new utility();
ResultSet rs = ut.DQL(qry);
while (rs.next()) {
a = Float.parseFloat(rs.getString("" + indus));
b = Math.round(a);
categoryDataset.setValue(b, "" + st, "" + rs.getString("year"));
}
JFreeChart chart = ChartFactory.createBarChart3D("" + st, // Title
"Year", // X-Axis label
"" + indus,// Y-Axis label
categoryDataset, // Dataset
PlotOrientation.VERTICAL,
true, // Show legend
true,
false
);
final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
final File file1 = new File("F:/JavaAptech/Netbeans 74 applications/MinorProj/web/" + "up" + st + indus + y1 + y2 + ".png");
img = "up" + st + indus + y1 + y2 + ".png";
ChartUtilities.writeChartAsPNG(ot, chart, 600, 400);
ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
chart.setBorderVisible(true);
request.getRequestDispatcher("/up.jsp").forward(request,response);
}
catch (Exception e)
{
// out.println(e);
}
}