Servlet and JasperReports


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Servlet and JasperReports

  1. #1
    Join Date
    Feb 2005
    Posts
    31

    Servlet and JasperReports

    Hi there,

    Please tell me there's someone here who knows about JasperReports! I've been spinning my wheels on this one for days. I'm trying to use a servlet to export a report in HTML format. I used a sample from the source projects file as a guide, set up my directory structure identicle to the example, and the only difference is that I am actually passing a Connection object as a parameter to the JasperFillManager.fillReport(...) method. My understanding is that this method is supposed to use the connection object to query the database. But that doesn't seem to be what's happening.

    My servlet code is able to execute without any exceptions but my report doesn't appear in the browser at all! I had a look at the source response from the servlet and it looks like this:

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style type="text/css">
    a {text-decoration: none}
    </style>
    </head>
    <body text="#000000" link="#000000" alink="#000000" vlink="#000000">
    <table width="100%" cellpadding="0" cellspacing="0" border="0">
    <tr><td width="50%"> </td><td align="center">

    </td><td width="50%"> </td></tr>
    </table>
    </body>
    </html>

    Just an empty table!
    So, the servlet is returning something, just not my report! Any idea what might be causing the problem? It shouldn't matter that my Connection object comes from a connection pool should it?

    Alan

    Code:
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
        {
           Connection connection = null;
           System.out.println("entering doPost");
           res.setContentType("text/html");
           PrintWriter out = res.getWriter();
    
           String CompanyID = req.getParameter("CompanyID");
           String CourseID = req.getParameter("CourseID");
           String SubmissionDate = req.getParameter("SubmissionDate");
           String DaysMissedAverage = "1-3";
           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
           java.util.Date today = new java.util.Date(); 
           String ReportDate = sdf.format(today);
           String ReportingServletURL = res.encodeRedirectURL("/scholastic/CourseEvalReportProcessor");
            
           //create a map of parameters to pass to the report.
           Map<String, Object> parameters = new HashMap<String,Object>();
           parameters.put("CompanyID", CompanyID);
           parameters.put("CourseID", CourseID);
           parameters.put("SubmissionDate", SubmissionDate);
           parameters.put("DaysMissedAverage", DaysMissedAverage);
           parameters.put("ReportDate", ReportDate);
           parameters.put("ReportingServletURL", ReportingServletURL);
    
           try
           {
             Context ctx = new InitialContext();
                if(ctx == null ) 
                   throw new Exception("No Context available...");
    
             DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/Scholastic");
        
             if (ds != null)
             {
               connection = ds.getConnection();
               if(connection != null)
               {  System.out.println("got connection");
                  //get the pre-compiled report
                  ServletContext context = this.getServletConfig().getServletContext();
                  File reportFile = new File(context.getRealPath("/reports/CourseEvalReport.jasper")); 
                  parameters.put("BaseDir", reportFile.getParentFile());
                  
                  if(reportFile.exists() == false)
                  {
                    out.println("<html>");
    	        out.println("<head>");
    	        out.println("<title>CourseEvaluationReport</title>");
                    out.println("<link href=\"site.css\" rel=\"stylesheet\" type=\"text/css\">");
    	        out.println("</head>");
    	        out.println("<body>");
                    out.println("Unable to load report file CourseEvalReport.jasper");	        
                    out.println("</body>");
    	        out.println("</html>");
                    return;
                  }
                  else
                  {
                    System.out.println("creating report...");
                    JasperPrint jasperPrint = null;
                    System.out.println("reportFile.getPath() = " + reportFile.getPath());                
                    JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
                    System.out.println("got jasper report...");
                    //create JasperPrint using fillReport() method
                    jasperPrint = JasperFillManager.fillReport(jasperReport, 
    					parameters, 
    					connection); 
                                  					
    		System.out.println("got jasper print");				
    		JRHtmlExporter exporter = new JRHtmlExporter();
                    
                    //Send the report back out using PrintWriter object
                    Map imagesMap = new HashMap();
    		req.getSession().setAttribute("IMAGES_MAP", imagesMap);
    			
    		exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
    		exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
    		exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
    		exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image?image=");
    		exporter.exportReport();
                    System.out.println("the end");
                    
                  }              
               }           
             }         
           }
           catch (JRException e)
           {
    	 out.println("<html>");
    	 out.println("<head>");
    	 out.println("<title>CourseEvaluationReport</title>");
             out.println("<link href=\"site.css\" rel=\"stylesheet\" type=\"text/css\">");
    	 out.println("</head>");
    	 out.println("<body>");
             out.println("<span class=\"bnew\">JasperReports encountered this error :</span>");
    	 out.println("<pre>");
             e.printStackTrace(out);
             out.println("</pre>");
             out.println("</body>");
    	 out.println("</html>");
           }
           catch(SQLException sqle)
           {
             System.err.println("SQL Problem: " + sqle.getMessage());
             System.err.println("SQL State: " + sqle.getSQLState());
             System.err.println("Vendor Error: " + sqle.getErrorCode());
             sqle.printStackTrace();      
           }
           catch(Exception e){System.out.println("Normal Exception:\n");e.printStackTrace();}
           finally
           {
             if (connection != null)
             {
               try { connection.close(); } catch (SQLException e) { ; }
               connection = null;
             }
           }
        }

  2. #2
    Join Date
    Jul 2005
    Posts
    1
    Hi Alan,

    Did u get the solution for this .... I am having a similar one .....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center