Menu

Reports example with iReport and Thinwire

2007-02-21
2013-04-24
  • Carlos Julio Perez Quizhpe

    Greetings people

    Do you have a html invocation of a report example using the iReport and ThinWire frameworks?

    I need to launch a report and download the pdf version in my browser.

    Please, help me folks!

    Best regards

    From Manta, Ecuador
    Carlos Julio Pérez

     
    • Pieter van Onselen

      Integrating iReport into Thinwire should not be a problem.

      First generate the report on the server from any Thinwire class and save the pdf so it accessable from the browser ( This could be in your Thinwire app or in ROOT folder of tomcat)

      Open a window with the link with Hyperlink.openLocation

      Hope this helps

       
    • Carlos Julio Perez Quizhpe

      Somebody has an example?

      I can't open the pdf file in my browser (opera and/or firefox)

      Thanks for your help

      Best regards

       
      • Pieter van Onselen

        Alternatively use a report servlet that streams the PDF to the browser.

        I have an example but I used jasperreports on servlet doPost:

            lines = (Vector<DebtorStatementLineBean>) db.getDebtorTransactions(
                        "F101S", (Date) df.parseObject("2005/06/21"), (Date) df
                                .parseObject("2005/07/01"));

            JRBeanCollectionDataSource source = new JRBeanCollectionDataSource(
                    lines);

            String realPath = request.getSession().getServletContext().getRealPath(
                    "/");

            ServletOutputStream servletOutputStream = response.getOutputStream();

                    //GENERATE REPORT
            File reportFile = new File(getServletConfig().getServletContext()
                    .getRealPath("/reports/drStatement.jasper"));
            byte[] bytes = null;

            String report = null;

                // Save Report TO PDF Byte array
            bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),
                    new HashMap(), source);

                        // Setup response as pdf
            response.setContentType("application/pdf");
            response.setContentLength(bytes.length);
                        
                        // Bytes written to servlet output stream
            servletOutputStream.write(bytes, 0, bytes.length);
            servletOutputStream.flush();
            servletOutputStream.close();

         
    • Carlos Julio Perez Quizhpe

      Hi Pieter

      Thanks for your code example. But, i have a little question about it too.

      How do you integrate your report example code with thinwire?

      How do you invoke servlet's request() method or servlet' session object in thinwire?

      Do you can help me with more code please?

      PD: Sorry by my english, i speak spanish.

      Best regards from Ecuador

       
    • Nobody/Anonymous

      I did a similar thing for any file download. Create a delegate server, its in the forums somewhere. Listen for for a method get, with a parameter that you define. You will want a static Hashmap to map a resource name to a filename.

      public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

      String method = request.getMethod();

      if (method.equals("GET")) {

      String resource = request.getParameter("_filedownload_");

      if(resource != null){
      try {

                                 
      String mime = getServletContext().getMimeType(resourceName.toLowerCase());
                              

      response.setContentType(mime);
                      response.setHeader("Content-Disposition","filename="+StaticHashmap.get(resourcename));

      StaticHashmap.remove(resourcename);
      response.getOutputStream().write(fileByteArray);
      response.flushBuffer();
      return; //you don't want to delegate this response
      }
      }
      }
      thinwireServlet.service(request, response);
      }

      then open a hyperlink location when u wish the client to recieve a file
      StaticHashmap.put(resourcename,filename);
      Hyperlink.openLocation("?_filedownload_=" + resourcename);

       
      • al0

        al0 - 2007-03-01

        In this specific case all can be done much simplier -
        just define in your web.xml 2 servlets and, correspondingly, 2 mappings, e.g.

            <servlet>
                <description>
                    Servlet to sproduce reports
                </description>
                <display-name>Reports Servlet</display-name>
                <servlet-name>ReportsServlet</servlet-name>
                <servlet-class>some.pakage.ReportsServlet</servlet-class>       
            </servlet>
            <servlet>
                <description>
                    Web Servlet
                </description>
                <display-name>TW Application Servlet</display-name>       
                <servlet-name>WebServlet</servlet-name>
                <servlet-class>thinwire.render.web.WebServlet</servlet-class>
                ... Usual TninWire bla-bla-bla ...
            </servlet>
            <servlet-mapping>
                <servlet-name>ReportsServlet</servlet-name>
                <url-pattern>/Reports/*</url-pattern>
                <!-- Alternatively *.pdf may be used as pattern -->
            </servlet-mapping>
            <servlet-mapping>
                <servlet-name>WebServlet</servlet-name>
                <url-pattern>/</url-pattern>
            </servlet-mapping>

        No delegating is required

        Regards,
        Oleksandr

         
    • Nobody/Anonymous

      That URL mapping thing doesn't work for me in tomcat, it just redirects to the servlet.

       
      • al0

        al0 - 2007-03-02

        H-m-m,

        I use it in TomCat with ThinWire on regular basis to deliver Jasper reports, all works Ok, as expected (and how it is specified in Servlet Specification, v.2.4).

        May you say more exact what and how it is not working in your opinion?
        You say " it just redirects to the servlet", but it is exactly what it has to do - it maps URL to the servlet and then servlet decides how to process that URL.

        In my example any URL that starts with http://host:port/MyTWApplication/Reports/ will be processed by ReportsServlet (i.e. report will be generated and returned), and any other URL that starts with http://host:port/MyTWApplication/ will be processed by ThinWire.

        Regards,
        Oleksandr

         
      • Pieter van Onselen

        I found the same problem when the servlet mapping in web.xml is not in the correct order.

        Note Oleksandr's web.xml file:

        <servlet-mapping>
        <servlet-name>ReportsServlet</servlet-name> 
        <url-pattern>/Reports/*</url-pattern> 
        <!-- Alternatively *.pdf may be used as pattern -->
        </servlet-mapping>

        <servlet-mapping>
        <servlet-name>WebServlet</servlet-name>
        <url-pattern>/</url-pattern>
        </servlet-mapping>

        The ReportsServlet mapping must be before the WebServlet otherwise if you try to get to the Reports servlet it will go to the WebServlet instead.

        Pieter

         
        • al0

          al0 - 2007-03-02

          According to Servlet Specification 2.4 order of mappings should not matter - the most specific mapping shall be used. It, definitely, does not preclude specific servlet container to be buggy, but ... I just have tested - with Tomcat 5.5.20 it really does not matter, if I swap mappings, application behavior is not affected.

          Here is quotation from Servlet Specification 2.4 (chapter 11.1)
          -----------------------------------------------------------------
          1. The container will try to find an exact match of the path of the request to the
          path of the servlet. A successful match selects the servlet.
          2. The container will recursively try to match the longest path-prefix. This is done
          by stepping down the path tree a directory at a time, using the ’/’ character as
          a path separator. The longest match determines the servlet selected.
          1. Previous versions of this specification made use of these mapping techniques
          as a suggestion rather than a requirement, allowing servlet containers
          to each have their different schemes for mapping client requests
          to servlets.
          MAPPING REQUESTS TO SERVLETS
          Final Version
          86
          3. If the last segment in the URL path contains an extension (e.g. .jsp), the servlet
          container will try to match a servlet that handles requests for the extension.
          An extension is defined as the part of the last segment after the last ’.’ character.
          4. If neither of the previous three rules result in a servlet match, the container will
          attempt to serve content appropriate for the resource requested. If a "default"
          servlet is defined for the application, it will be used.
          -----------------------------------------------------------------

          Regards,
          Oleksandr

           
    • mervz22

      mervz22 - 2007-05-08

      try this site.there is a module that produce report,http://www.cs-solutions.info:8080/cmis/

       
    • Nobody/Anonymous

      When I access two servlet using different url respectively in the brower which is opened by myself, that's ok, that is to say, i can get what i want. But if i click a hyperlink in a window located by one url to open a new window which accesses another url, it will redirect to the former url. I don't konw why. Who can give me some explanations?

      Melvin

       

Log in to post a comment.

Auth0 Logo