11 Replies Latest reply on Sep 28, 2009 5:46 AM by Dimka Noneen

    Requesting doc through servlet on onComplete event problem.

    Dimka Noneen Newbie

      Helli.

      I have a form with button. There are some inputTexts on form for data.
      When I press a button it generates link and then servlet gets it and respones with a report document.

      But it work only on onclick event without setting data from fields to bean.
      Event onComplete on servlet responce complete IE windows closes itself without any result as report doc.

      Why it is so?

      Link to a WAR file:
      http://upload.com.ua/get/901049747/?mode=free
      (enter numerical CAPTCHA and press green button, wait for a 1 min)

      to run: http://127.0.0.1:8080/test/pages/reports.jsf

        • 1. Re: Requesting doc through servlet on onComplete event probl
          Nick Belaevski Master

          Hi,

          Please post pages/bean code.

          • 2. Re: Requesting doc through servlet on onComplete event probl
            Dimka Noneen Newbie

            Sorry, I fogot it..

            package servlets;
            
            import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;
            import net.sf.jasperreports.engine.JasperReport;
            import net.sf.jasperreports.engine.JasperCompileManager;
            import net.sf.jasperreports.engine.JRExporterParameter;
            import net.sf.jasperreports.engine.JasperFillManager;
            import net.sf.jasperreports.engine.export.JExcelApiExporter;
            import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
            
            import javax.servlet.http.HttpServlet;
            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;
            import javax.servlet.ServletException;
            import java.io.*;
            import java.util.HashMap;
            
            
            public class ReportServlet extends HttpServlet {
             @Override
             protected void service(HttpServletRequest req, HttpServletResponse resp)
             throws ServletException, IOException {
            
            
             JRBeanArrayDataSource jrBeanArrayDataSource = new JRBeanArrayDataSource(null);
            
             JasperReport jasper_report = null;
             try {
             try {
             InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("journal.jrxml");
             jasper_report = JasperCompileManager.compileReport(inputStream);
             } catch (Exception e) {
             e.printStackTrace();
             }
            
            
             ByteArrayOutputStream reportOutputStream = new ByteArrayOutputStream();
            
             JExcelApiExporter xlsExporter = new JExcelApiExporter();
             xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, JasperFillManager.fillReport(jasper_report, new HashMap(), jrBeanArrayDataSource));
             xlsExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, reportOutputStream);
             xlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
             xlsExporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
             xlsExporter.exportReport();
            
             byte[] bytes = reportOutputStream.toByteArray();
             resp.setContentType("application/vnd.ms-excel");
             resp.setHeader("Content-Disposition", "inline");
             resp.setHeader("Content-length", "" + bytes.length);
            
             BufferedInputStream in = new BufferedInputStream(new ByteArrayInputStream(bytes));
             BufferedOutputStream out = new BufferedOutputStream(resp.getOutputStream());
            
             try {
             byte[] buf = new byte[2048];
             while (true) {
             int actual = in.read(buf);
             if (actual < 0)
             break;
             out.write(buf, 0, actual);
             }
             }
             finally {
             if (null != in)
             in.close();
             if (null != out)
             out.close();
             }
            
             } catch (Exception e) {
             e.printStackTrace();
             }
            
             }
            
            }
            


            • 4. Re: Requesting doc through servlet on onComplete event probl
              Dimka Noneen Newbie

              these codes you can see in WAR ;)

              <?xml version="1.0" encoding="UTF-8"?>
              <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
               xmlns:ui="http://java.sun.com/jsf/facelets"
               xmlns:h="http://java.sun.com/jsf/html"
               xmlns:c="http://java.sun.com/jstl/core"
               xmlns:f="http://java.sun.com/jsf/core"
               xmlns:a4j="http://richfaces.org/a4j"
               xmlns:rich="http://richfaces.org/rich">
               <f:view>
               <a4j:form id="reports_form">
              
               <div>
               <rich:panel>
               <a4j:commandLink onclick="Richfaces.showModalPanel('print_report_panel');">
               <h:outputText value="REPORT"/>
               </a4j:commandLink>
               </rich:panel>
               </div>
               </a4j:form>
               <a4j:include viewId="/pages/report.jspx"/>
              
               </f:view>
              </jsp:root>
              
              
              ---------------------
              
              <ui:composition xmlns:jsp="http://java.sun.com/JSP/Page"
               version="2.1"
               xmlns:ui="http://java.sun.com/jsf/facelets"
               xmlns:f="http://java.sun.com/jsf/core"
               xmlns:h="http://java.sun.com/jsf/html"
               xmlns:c="http://java.sun.com/jstl/core"
               xmlns:a4j="http://richfaces.org/a4j"
               xmlns:rich="http://richfaces.org/rich">
              
               <rich:modalPanel id="print_report_panel"
               minHeight="200"
               minWidth="450"
               zindex="2000"
               autosized="true">
               <script><!--
               function report(){
               open('/test/Report' + new Date().getSeconds() + '.xls');
               return true;
               }
               --></script>
              
               <f:facet name="header">
               <h:outputText value="Reports"/>
               </f:facet>
               <f:facet name="controls">
               <a href="javascript:Richfaces.hideModalPanel('print_report_panel');">X</a>
               </f:facet>
               <a4j:form id="report_param">
               <rich:panel>
               <table width="100%">
               </table>
               <br/>
               <table width="100%">
               <tr>
               <td align="center">
               <h:panelGrid columns="3">
              
               <a4j:commandButton id="btn_click"
               value="onClick"
               onclick="report();"/>
               <a4j:commandButton id="btn_complete"
               value="onComplete"
               oncomplete="report();"/>
               </h:panelGrid>
               </td>
               </tr>
               </table>
               </rich:panel>
               </a4j:form>
               </rich:modalPanel>
              </ui:composition>


              • 6. Re: Requesting doc through servlet on onComplete event probl
                Dimka Noneen Newbie

                Hi,
                No... nothing.

                I replaced INLINE with:
                resp.setHeader("Content-Disposition", "attachment; filename=" + "Report" + System.currentTimeMillis() + ".xls");

                This variant(mime) I use with <h:coomandButton action..>.

                • 7. Re: Requesting doc through servlet on onComplete event probl
                  Nick Belaevski Master

                  Looks like some kind of security measure in IE. Try this:

                  function report(){
                   location.href = '/test/Report' + new Date().getSeconds() + '.xls';
                   return true;
                   }
                  


                  • 8. Re: Requesting doc through servlet on onComplete event probl
                    Dimka Noneen Newbie

                    nothing..
                    it's only drops error on main page:
                    description The requested resource (/test/pages/[object]) is not available.

                    new windows closes as always.

                    • 9. Re: Requesting doc through servlet on onComplete event probl
                      Dimka Noneen Newbie

                      in Opera works... some notice about popup appears, but on click on it gives me a report.

                      • 10. Re: Requesting doc through servlet on onComplete event probl
                        Dimka Noneen Newbie

                         

                        <a4j:commandButton value="test" oncomplete="$(find('btn')).click();"/>
                         <a4j:commandButton id="btn" style="display:none;" onclick = "report();"/>


                        Gives the same result.