0 Replies Latest reply on Sep 10, 2012 3:19 AM by raghu88

    Cannot forward after response has been committed...

    raghu88

      Hi  to all,


      i want to export some data to an excel file while downloading the file i got the following exception .......but after downloading the excel file i am trying to open it is saying like "the file you are trying to open .xls is in a different format than specified by the file extension"please any one help me how to solve this one.....



      this is my error log...........


      Servlet.service() for servlet action threw exception: java.lang.IllegalStateException: Cannot forward after response has been committed

      at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:417) [:6.1.0.Final]

      at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:407) [:6.1.0.Final]

      at org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1063) [:1.2.7]

      at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:386) [:1.2.7]

      at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:229) [:1.2.7]

      at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194) [:1.2.7]

      at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432) [:1.2.7]

      at javax.servlet.http.HttpServlet.service(HttpServlet.java:754) [:1.0.0.Final]

      at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [:1.0.0.Final]

      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324) [:6.1.0.Final]

      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.1.0.Final]

      at com.zyom.security.LoginMonitorFilter.doFilter(LoginMonitorFilter.java:67) [:]

      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274) [:6.1.0.Final]

      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242) [:6.1.0.Final]

      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [:6.1.0.Final]

      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [:6.1.0.Final]

      at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181) [:6.1.0.Final]

      at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285) [:1.1.0.Final]

      at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261) [:1.1.0.Final]

      at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88) [:6.1.0.Final]

      at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100) [:6.1.0.Final]

      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159) [:6.1.0.Final]

      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [:6.1.0.Final]

      at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) [:6.1.0.Final]

      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [:6.1.0.Final]

      at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53) [:6.1.0.Final]

      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [:6.1.0.Final]

      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [:6.1.0.Final]

      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654) [:6.1.0.Final]

      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951) [:6.1.0.Final]

      at java.lang.Thread.run(Thread.java:619) [:1.6.0_13]




       

      1. package com.zyom.struts.ats.action; 
      2.  
      3. import java.io.File; 
      4. import java.text.NumberFormat; 
      5. import java.text.SimpleDateFormat; 
      6. import java.util.ArrayList; 
      7. import java.util.Calendar; 
      8. import java.util.Date; 
      9. import java.util.HashMap; 
      10. import java.util.Iterator; 
      11. import java.util.List; 
      12.  
      13. import javax.servlet.ServletContext; 
      14. import javax.servlet.http.HttpServletRequest; 
      15. import javax.servlet.http.HttpServletResponse; 
      16. import javax.servlet.http.HttpSession; 
      17.  
      18. import jxl.Workbook; 
      19. import jxl.write.Colour; 
      20. import jxl.write.Label; 
      21. import jxl.write.WritableCellFormat; 
      22. import jxl.write.WritableFont; 
      23. import jxl.write.WritableSheet; 
      24. import jxl.write.WritableWorkbook; 
      25.  
      26.  
      27. import org.apache.struts.action.ActionForm; 
      28. import org.apache.struts.action.ActionMapping; 
      29. import org.apache.struts.actions.DownloadAction; 
      30. import org.apache.struts.actions.DownloadAction.ResourceStreamInfo; 
      31. import org.apache.struts.actions.DownloadAction.StreamInfo; 
      32.  
      33. import com.zyom.reuse.util.DateHandler; 
      34. import com.zyom.struts.form.CmForm; 
      35. import com.zyom.struts.form.DemandFGForm; 
      36. import com.zyom.webclient.util.DBHelper; 
      37. import com.zyom.webclient.util.AtsHelper; 
      38. import com.zyom.webclient.util.DemandSupplyItem; 
      39. import com.zyom.webclient.util.MozartCostUtils; 
      40. import com.zyom.struts.form.MapCustomerForm; 
      41.  
      42. public class RegionCustUserMapXlAction extends DownloadAction { 
      43.  
      44.     protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form, 
      45.             HttpServletRequest request, HttpServletResponse response) 
      46.     throws Exception { 
      47.         Date currentDatetime = new Date(System.currentTimeMillis()); 
      48.         SimpleDateFormat formatter = new SimpleDateFormat("MM-dd-yyyy-HH-mm"); 
      49.  
      50.         String myDate = formatter.format(currentDatetime); 
      51.  
      52.         String myDate1 = formatter.format(currentDatetime); 
      53.         String fileName = "RegCustUsrMap_"+myDate+".xls"
      54.         String contentType = "application/vnd.ms-excel"
      55.         ServletContext servletCtx = this.servlet.getServletContext(); 
      56.          
      57.         try
      58.  
      59.             String path = servletCtx.getRealPath(fileName); 
      60.             WritableWorkbook workbook = Workbook.createWorkbook(new File(path));         
      61.             HttpSession session = request.getSession(true); 
      62.             List cust_list=new ArrayList(); 
      63.             cust_list=(ArrayList)session.getAttribute("cust_list"); 
      64.              
      65.             jxl.write.Number num; 
      66.  
      67.             Calendar stDate = Calendar.getInstance(); 
      68.             Calendar endDate = Calendar.getInstance(); 
      69.              
      70.             String temp = ""
      71.  
      72.             SimpleDateFormat formatter1 = new SimpleDateFormat("MMM-dd,yyyy");    
      73.  
      74.             WritableSheet sheet = workbook.createSheet("RegCustUsrMap_"+myDate1, 1);  
      75.             WritableFont arial10font = new WritableFont(WritableFont.ARIAL, 10); 
      76.             WritableCellFormat arial10format = new WritableCellFormat (arial10font); 
      77.             // sheet.mergeCells(0,0,2,0); 
      78.             WritableFont times16font = new WritableFont(WritableFont.TIMES, 10, WritableFont.BOLD, true); 
      79.             WritableCellFormat times16format = new WritableCellFormat (times16font); 
      80.             times16format.setAlignment(jxl.format.Alignment.CENTRE); 
      81.             // times16format.setBackground(Colour.BLUE); 
      82.             times16format.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.MEDIUM,Colour.BLACK); 
      83.  
      84.  
      85.  
      86.             WritableFont datafont = new WritableFont(WritableFont.ARIAL , 10); 
      87.             WritableFont datafont1 = new WritableFont(WritableFont.ARIAL , 10); 
      88.             WritableFont datafont2 = new WritableFont(WritableFont.ARIAL , 10); 
      89.  
      90.             datafont1.setColour(Colour.BLUE); 
      91.             datafont2.setColour(Colour.RED); 
      92.             datafont.setColour(Colour.BLACK); 
      93.             WritableCellFormat dataformat = new WritableCellFormat (datafont2); 
      94.             WritableCellFormat dataformatApproved = new WritableCellFormat (datafont); 
      95.             WritableCellFormat dataformatnotApproved = new WritableCellFormat (datafont1); 
      96.             WritableCellFormat dataformatApproved1 = new WritableCellFormat (datafont); 
      97.             dataformat.setAlignment(jxl.format.Alignment.LEFT); 
      98.             dataformatApproved.setAlignment(jxl.format.Alignment.LEFT); 
      99.             dataformatnotApproved.setAlignment(jxl.format.Alignment.LEFT); 
      100.             dataformatApproved.setBackground(jxl.format.Colour.YELLOW); 
      101.             dataformatApproved1.setAlignment(jxl.format.Alignment.LEFT); 
      102.             dataformatApproved1.setBackground(jxl.format.Colour.BRIGHT_GREEN); 
      103.             // times16format.setBackground(Colour.BLUE); 
      104.  
      105.             dataformatApproved.setBorder(jxl.format.Border.ALL , jxl.format.BorderLineStyle.MEDIUM_DASHED ,Colour.BLACK); 
      106.             dataformatApproved1.setBorder(jxl.format.Border.ALL , jxl.format.BorderLineStyle.MEDIUM_DASHED ,Colour.BLACK); 
      107.              
      108.             Label label = new Label(0, 0, "Region Customer User Map",arial10format); 
      109.             sheet.addCell(label); 
      110.             label=new Label(0,2,"As of: ",arial10format); 
      111.             sheet.addCell(label); 
      112.             label=new Label(1,2,DateHandler.getTodaysDate(),arial10format); 
      113.             sheet.addCell(label); 
      114.                          
      115.             int i=0
      116.             int j=3
      117.              
      118.             j++; 
      119.             label = new Label(i, j, "Sales Region" ,times16format); 
      120.             sheet.addCell(label);    
      121.             i++; 
      122.             label = new Label(i, j, "ROLES MAPPED AT REGION LEVEL (ALL EXCEPT SALES ID)" ,times16format); 
      123.             sheet.addCell(label);    
      124.             i++; 
      125.             label = new Label(i, j, "Customer" ,times16format); 
      126.             sheet.addCell(label);    
      127.             i++; 
      128.             label = new Label(i, j, "Country" ,times16format); 
      129.             sheet.addCell(label); 
      130.             i++; 
      131.             label = new Label(i, j, "Sales Id" ,times16format); 
      132.             sheet.addCell(label);    
      133.             i++; 
      134.                          
      135.  
      136.             while(endDate.after(stDate)){ 
      137.                   
      138.                 label = new Label(i, j, formatter1.format(new java.util.Date(stDate.getTimeInMillis())) ,times16format); 
      139.                 sheet.addCell(label); 
      140.                  i++; 
      141.                 stDate.add(Calendar.DATE, 7); 
      142.                  }     
      143.              
      144.         for (int count = 0; count < cust_list.size(); count++) { 
      145.             MapCustomerForm mapCustomerForm=new MapCustomerForm(); 
      146.             mapCustomerForm = (MapCustomerForm) cust_list.get(count); 
      147.                  
      148.                   j++; 
      149.                    
      150.                     i=0
      151.                     label = new Label(i, j, mapCustomerForm.getRegion(),arial10format); 
      152.                     sheet.addCell(label);    
      153.                     i++; 
      154.                     label = new Label(i, j, mapCustomerForm.getUser_names(),arial10format); 
      155.                     sheet.addCell(label);    
      156.                     i++; 
      157.                     label = new Label(i, j,mapCustomerForm.getCustomer(),arial10format); 
      158.                     sheet.addCell(label);    
      159.                     i++; 
      160.                     label = new Label(i, j, mapCustomerForm.getLocation(),arial10format); 
      161.                     sheet.addCell(label);    
      162.                     i++; 
      163.                     label = new Label(i, j,mapCustomerForm.getSalesid(),arial10format); 
      164.                     sheet.addCell(label); 
      165.                     i++; 
      166.                      
      167.             } 
      168.                          
      169.         j=j+2
      170.          
      171.          
      172.                 workbook.write(); 
      173.                 workbook.close();  
      174.  
      175.         }catch(Exception e){ 
      176.  
      177.             System.out.print(e); 
      178.         }    
      179.           
      180.  
      181.          
      182.  
      183.         /*response.setHeader("Content-disposition", "attachment;filename="+ fileName );
      184.         final java.io.OutputStream os = response.getOutputStream();
      185.         os.flush();
      186.         os.close();*/ 
      187.         response.setContentType("application/vnd.ms-excel"); 
      188.         response.setHeader("Content-disposition","inline;fileName=" + fileName); 
      189.         final java.io.OutputStream os = response.getOutputStream(); 
      190.          
      191.         os.flush(); 
      192.          
      193.         return new ResourceStreamInfo(contentType, servletCtx, fileName); 
      194.     }