#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    GetOutputStream() has already been called for this response


    Hi I have a program and wants to do an export to excell with a jsp file. I use for this the jxl-library.

    But I got the following error:
    Code:
    org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response
    	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
    	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:410)
    	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:366)
    	org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
    	org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    	org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:97)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    	org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:100)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    	org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:78)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    	org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    	org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:35)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    	org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:177)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    	org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:187)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    	org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    	org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
    	org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
    	org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:167)
    	org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    	org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    	org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
    	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    	net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:151)
    	net.bull.javamelody.MonitoringFilter.doFilter(MonitoringFilter.java:129)
    	org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
    	org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
    I don't find the problem. This is my code:
    Code:
    <%@ page import = "java.util.*, java.text.*, java.sql.*, java.net.*,java.io.*" %>  
    <%@ page import = "jxl.*" %> 
    <%@ page import = "java.sql.*" %>
    <%@ include file="config.jsp" %>
    <%@ include file="AdHocQueries_Pers_queryConstructor.jsp" %>
    <%@ include file="AdHocQueries_OT_queryConstructor.jsp" %>
    <%@ include file="getRole.jsp" %> 
    <%@ include file="updateUserActivity.jsp" %>
    <% 
    
    
    	ArrayList<String> autorized_users = new ArrayList<String>();
    	autorized_users.add("HRG Full Access");
        if(isAuthorized(autorized_users,db_ipp_addr,db_name,db_username, db_password, request.getUserPrincipal().getName()) == false){
    		response.sendRedirect("notAuthorized.jsp"); 
    	}
    
    	boolean noLimit = false;
    	String clause = "";
    	String count = "";
    	if (request.getParameter("clause") != null){
    	clause = request.getParameter("clause");
    	}else{
    		clause = "";    
    	} 
    	
    	String OT_filter = "";
    	String Pers_filter = "";
    	String OT_name = "";
    	String Pers_name = "";
    	String OT_nr  = request.getParameter("adhocMerge_OT"); 
    	String Pers_nr = request.getParameter("adhocMerge_Pers"); 
    	String initialPersSql = "";
    	String postPersSql = "";
    	String initialOTSql = "";
    	String whereclauseOTSql = "";
    	String MergeSelectedField = "";
    	ArrayList<String> MergeSelectedFieldList = new ArrayList<String>();
    	
    	String count_row_pers = "0";
    	
        Connection connection = null; 
        Statement statement = null; 
        ResultSet rs = null; 
        String connectionURL = "jdbc:mysql://" + db_ipp_addr + ":3306/" + db_name;
        Class.forName("com.mysql.jdbc.Driver").newInstance(); 
        connection = DriverManager.getConnection(connectionURL, db_username, db_password); 
        statement = connection.createStatement(); 
    	
    	String sql = "SELECT TheSQL,Filter FROM t_L1_HRB_global_adhoc_filters_updatable WHERE Nr = '"+OT_nr+"'";
    	rs = statement.executeQuery(sql); 
    	if (rs.next()) { OT_filter = rs.getString("TheSQL"); OT_name= rs.getString("Filter");}
    
    	sql = "SELECT TheSQL,Filter FROM t_L1_HRB_global_adhoc_filters_updatable WHERE Nr = '"+Pers_nr+"'";
    	rs = statement.executeQuery(sql); 
    	if (rs.next()) { Pers_filter = rs.getString("TheSQL"); Pers_name= rs.getString("Filter");}
    	
    	
    
    	initialPersSql = getSQL(Pers_filter.toLowerCase(), true) ;
    	initialPersSql = initialPersSql.substring(0,initialPersSql.indexOf("ORDER BY"));
    	initialOTSql = getOTSQL(OT_filter.toLowerCase()) ;
    	postPersSql = initialPersSql.replace("ifnull(PD.Stamnummer,'') as STAMNUMMER,ifnull(PD.Graad_kort,'') as GRAAD_kort,ifnull(PD.Naam,'') as NAAM,ifnull(PD.Voornamen,'') as VOORNAMEN, ifnull(PD.Org,'') as ORG, ifnull(PD.orgnum,'') as ORGNUM ,","ifnull(PD.Stamnummer,'') as STAMNUMMER,");
    
    	sql = "SELECT Table_Name_DB, FieldName_DB FROM t_L1_HRB_global_adhoc_selectfields WHERE AdHocType  = '1'";
    			rs = statement.executeQuery(sql);
    			String tempo = postPersSql.substring(postPersSql.indexOf("SELECT"), postPersSql.indexOf("FROM")).toLowerCase();
    			while (rs.next()) { 
    				if ((tempo.contains("pd."+rs.getString("FieldName_DB").toLowerCase()+",") ^ tempo.contains(rs.getString("Table_Name_DB")+"1"+rs.getString("FieldName_DB").toLowerCase()+",")) && !MergeSelectedField.toLowerCase().contains(rs.getString("FieldName_DB").toLowerCase())){
    					MergeSelectedField+= ",T2."+rs.getString("FieldName_DB");
    					if (!rs.getString("FieldName_DB").equals("Stamnummer")) MergeSelectedFieldList.add(rs.getString("FieldName_DB"));
    				};
    			}
    			MergeSelectedField = MergeSelectedField.replace(",T2.Stamnummer","");		
    
    	whereclauseOTSql = initialOTSql.substring(initialOTSql.indexOf("WHERE"));
    
    	sql = "SELECT * "+MergeSelectedField+" FROM t_L1_HRB_global_org_ot AS OT LEFT JOIN ("+postPersSql+") AS T2 ON OT.Stamnummer = T2.Stamnummer " +whereclauseOTSql;
    
    
    
    String sheetname = "Global "+version+" : Ad Hoc query OT"; 
    OutputStream os = response.getOutputStream();// will write to the output stream WritableWorkbook 
    
    	
     
       try{  
       response.reset ();  
       response.setContentType ( "application / vnd.ms-excel; charset = UTF-8");  
       response.setHeader ( "Content-Disposition", "attachment; filename =" + new String ( (sheetname+".xls").getBytes (), "iso-8859-1"));  
    
       int NrToAdd = MergeSelectedFieldList.size();
        
       jxl.write.WritableWorkbook wwb = jxl.Workbook.createWorkbook (os); 
       jxl.write.WritableSheet ws = wwb.createSheet ( sheetname, 0);  
       ws.getSettings().setOrientation(jxl.format.PageOrientation.LANDSCAPE);
       ws.getSettings().setScaleFactor(50);
       ws.getSettings().setFitWidth(1);
       ws.getSettings().setPaperSize(jxl.format.PaperSize.A3);
       HeaderFooter footer = new HeaderFooter();
    
    ...
    Can someone help me?
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    I believe this may be the source of the problem

    Code:
    OutputStream os = response.getOutputStream();

IMN logo majestic logo threadwatch logo seochat tools logo