我把.jrxml和.jasper文件放到myeclipse项目中,导入相关的包了,但通过jsp按扭去调用生成报表文件方法,怎么找不到路径?
我用main方法去测试是可以生成。这是怎么回事?
其代码如下:
index.jsp页面:
servlet代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charrset=GBK");
ResultSet resultSet = null;
Connection con = JDBC.getConnection();
PreparedStatement statement;
try {
statement = con.prepareStatement(
"select * from fruitSell "
);
resultSet = statement.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JRResultSetDataSource result =
new JRResultSetDataSource( resultSet );
JasperReport jasperReport;
try {
jasperReport = JasperCompileManager.compileReport(getServletContext().getRealPath("reports/fruitSell.jrxml"));
JasperPrint jasperPrint =
JasperFillManager.fillReport(
jasperReport, new HashMap(), result
);
JasperExportManager.exportReportToPdfFile(
jasperPrint, "reports/fruitSell1.pdf"
);
} catch (JRException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
getServletContext().getRequestDispatcher("/doIndex.jsp").forward(request, response);
}