pdd_2003_1 Posted June 29, 2007 Share Posted June 29, 2007 I can create and view reports using iJasper. I have downloaded the documentation for Jasperreports and also iReports but am finding them tough going. How can I call reports from a JSP page? Can anyone provide some sample code so that I can get an overview of the process. Link to comment Share on other sites More sharing options...
aditya_gupta Posted June 29, 2007 Share Posted June 29, 2007 Hi,You can use JasperReports api. Here is the code which i have written in a servlet, you can convert it to jsp. /*-----------------------------------------import java.io.*;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;import java.util.HashMap;import javax.servlet.*;import javax.servlet.http.*;import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JRExporter;import net.sf.jasperreports.engine.JRExporterParameter;import net.sf.jasperreports.engine.JasperCompileManager;import net.sf.jasperreports.engine.JasperFillManager;import net.sf.jasperreports.engine.JasperPrint;import net.sf.jasperreports.engine.JasperReport;import net.sf.jasperreports.engine.export.JRPdfExporter; public class servlet_test extends HttpServlet {HttpServletRequest request, HttpServletResponse response){try{ HashMap hm = new HashMap(); Connection con = null; OutputStream ouputStream = null; JRExporter exporter = null; try{ Class.forName("net.sourceforge.jtds.jdbc.Driver"); con = DriverManager.getConnection("jdbc:jtds:sqlserver://serverip:portno/Database_name","user",""password"); } catch (SQLException ex) { ex.printStackTrace(); }catch(ClassNotFoundException cex){ cex.printStackTrace(); } hm.put("report_parameter_name",value); ouputStream = response.getOutputStream(); JasperPrint jasperPrint = JasperFillManager.fillReport(filename,hm,con); response.setContentType("application/pdf"); exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); exporter.exportReport(); } catch (JRException e) { throw new ServletException(e); } finally { if (ouputStream != null) { try { ouputStream.close(); } catch (IOException ex) { } } else{ response.flushBuffer(); } } } Aditya Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now