我是JFreeChart
的新手并试图在JSP
中实施它。我通过JSP
页面上的表单接受用户输入,并在从数据库中获取数据后发送到我的图表的Servlet
页面。之后,我想在我的JSP
页面上显示图表,但遗憾的是,我的Servlet
页面上显示的是该图表,并且该页面未重定向回我的JSP
页面。我不明白为什么......
这是我的JSP页面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>
这是我的Servlet页面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);
}
}