Ola Galera, estou tendo um problema que está dificil de resolver…
O que quero fazer:
Eu criei um servlet que é chamado atraves de um form em um JSP, este servlet gera um excel e após isso redireciona para um JSP, que pode ser o mesmo que o chamou ou um outro.
O que está acontecendo:
O form chama o servlet, que processa a requisição, onde é gerado o arquivo excel pelo framework POI e aparece a janela de realizar download, após baixar o arquivo a tela nao muda, fica na tela que chamou o servlet.
Abaixo segue os codigos envolvidos:
Parte do web.xml com a configuração do servlet:
<servlet>
<servlet-name>DownloadExcelServlet</servlet-name>
<servlet-class>br.com.eversystems.painel.servlet.DownloadExcelServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadExcelServlet</servlet-name>
<url-pattern>/downloadExcelServlet</url-pattern>
</servlet-mapping>
Formulario que chama o servlet:
<form action="downloadExcelServlet" method="post" >
<input type="submit" value="Exportar" />
</form>
Servlet:
package br.com.eversystems.painel.servlet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import br.com.eversystems.painel.util.ConstantesPainel;
import br.com.eversystems.painel.util.GerarExcel;
public class DownloadExcelServlet extends HttpServlet{
// processa as solicitações "get" dos clientes
public void doGet( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
doPost( request, response );
}
public void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException {
GerarExcel gerarExcel = new GerarExcel();
// declara stream onde será gravado o xls
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// cria um novo documento
HSSFWorkbook workbook = new HSSFWorkbook();
// cria uma planilha
HSSFSheet sheet = workbook.createSheet("Mapa Alocacao");
// cria o header da planilha
gerarExcel.createHeaderMapaAlocacao(sheet, workbook, (List)request.getSession().getAttribute(ConstantesPainel.PAINEL_ALOCACAO_LISTA_MES));
// popula a planilha
gerarExcel.createDataMapaAlocacao(sheet, workbook, (List)request.getSession().getAttribute(ConstantesPainel.PAINEL_ALOCACAO_LISTA_ALOCACAO), (List)request.getSession().getAttribute(ConstantesPainel.PAINEL_ALOCACAO_LISTA_MES));
// ajusta planilha
gerarExcel.adjustSheet(sheet, workbook, 11, 10);
// escreve o documento no stream
workbook.write(outputStream);
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition","attachment;filename=MapaAlocacao.xls");
response.setContentLength((int)outputStream.toByteArray().length);
ServletOutputStream output = response.getOutputStream();
outputStream.writeTo(output);
output.flush();
output.close();
request.getRequestDispatcher((String)request.getSession().getAttribute(ConstantesPainel.PAINEL_ALOCACAO_NEXT_JSP)).forward(request,response);
}
}
Bom, quando no final do codigo utilizei o comando
request.getRequestDispatcher((String)request.getSession().getAttribute(ConstantesPainel.PAINEL_ALOCACAO_NEXT_JSP)).forward(request,response);
ocorreu a seguinte exceção:
[02/09/09 06:27:27:027 GMT] ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/painelEver].[DownloadExcelServlet]] Servlet.service() for servlet DownloadExcelServlet threw exception
java.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:313)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
at br.com.eversystems.painel.servlet.DownloadExcelServlet.doPost(DownloadExcelServlet.java:86)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:568)
e quando utilizei o comando response.sendRedirect((String)request.getSession().getAttribute(ConstantesPainel.PAINEL_ALOCACAO_NEXT_JSP));
ocorreu a seguinte exceção:
[02/09/09 06:29:48:048 GMT] ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/painelEver].[DownloadExcelServlet]] Servlet.service() for servlet DownloadExcelServlet threw exception
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:432)
at br.com.eversystems.painel.servlet.DownloadExcelServlet.doPost(DownloadExcelServlet.java:87)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
at java.lang.Thread.run(Thread.java:568)
Obrigado desde ja pela ajuda.
Caso alguem tenha uma ideia de o que devo fazer por favor me avise, pois estou enrrolado com isso, encontrei alguns exemplos mas nada de resolver.
Abraços a todos,
Victor Torres