2017-04-25 20 views
-1

私はこの問題を抱えています。 これはコードです。 pdfは動作しますが、コンソールにはエラーがあります。サーブレット応答のエラーgetOutputStream()

がindex.jsp - >これはjspでpageある

<%@page import="com.itextpdf.text.pdf.codec.Base64.InputStream"%> 
<%@page import="pdf.pdf.Pdf"%> 
<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 

<% 


    String path=application.getRealPath("/"); 
    System.out.println("Path: "+path); 

    //CuponPDF_old c=new CuponPDF_old(request, response,path,Estudiante); 
    //Cupon.CuponPDFNew c=new Cupon.CuponPDFNew(request, response,path,Estudiante); 
    //CuponPDFNew c = new CuponPDFNew(request,response,path,Estudiante); 

    Pdf c = new Pdf(request,response); 
     c.generarCupon(); 
%> 

</body> 
</html> 

Pdf.java - >これはclassがPackage

package pdf.pdf; 


import java.io.IOException; 
import java.sql.SQLException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import com.itextpdf.text.Document; 
import com.itextpdf.text.DocumentException; 
import com.itextpdf.text.Element; 
import com.itextpdf.text.PageSize; 
import com.itextpdf.text.Phrase; 
import com.itextpdf.text.pdf.PdfPCell; 
import com.itextpdf.text.pdf.PdfPTable; 
import com.itextpdf.text.pdf.PdfWriter; 


public class Pdf { 

    HttpServletResponse response=null; 

    public Pdf (HttpServletRequest request, HttpServletResponse _response) { 

     response=_response; 


    } 


    public Document cuponPage (Document document) throws DocumentException 
    { 

     PdfPTable table = new PdfPTable(3); 
     PdfPCell cell; 

     cell = new PdfPCell(new Phrase("Cell with colspan 3")); 
     cell.setColspan(3); 
     cell.setHorizontalAlignment(Element.ALIGN_CENTER); 
     table.addCell(cell); 





      document.add(table); 
     /*float widCPrincipal[]={97,3};      

     PdfPTable tablePrincipal=new PdfPTable(2); 
     tablePrincipal.setWidths(widCPrincipal); 
     document.add(tablePrincipal); 

     */ 
     return document; 

    } 



    public void generarCupon()throws IOException, DocumentException, ClassNotFoundException, SQLException 
    { 
      Document document = new Document(PageSize.A4); 




      try { 
       response.setContentType("application/pdf"); 
       PdfWriter.getInstance(document, response.getOutputStream()); 
       document.open(); 



        document=cuponPage(document); 
       } 





     catch(DocumentException e) { 
      e.printStackTrace(); 
      System.out.println("Error:"+e.getMessage()); 
     } 
     document.close();  





     } 
} 

そしてerrorでcreateある。 - >これはコンソールのエラーに関する情報です。

14:31:02,830 ERROR [[jsp]] Servlet.service() para servlet jsp lanzó excepción 
java.lang.IllegalStateException: getOutputStream() ya ha sido llamado para esta respuesta 
    at org.apache.catalina.connector.Response.getWriter(Response.java:619) 
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198) 
    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125) 
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118) 
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:188) 
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118) 
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:77) 
    at org.apache.jsp.index_jsp._jspService(index_jsp.java:93) 
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369) 
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:322) 
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:249) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190) 
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92) 
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126) 
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) 
    at java.lang.Thread.run(Thread.java:745) 

thxです。

+0

エラーは 'HttpServletResponse.getOutputStream()'にあります。 iTextとは何も関係ありません。エラーメッセージは私にとってさえも完全にはっきりしていて、私はスペイン語を話せません。 – EJP

答えて

0

JSPページでは、あなたが最初の/ htmlの

<%@page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 

をテキストにコンテンツタイプを設定し、あなたはすでにあなたがすでにコンテンツを送ったとして、あなたが今、変更することはできませんいくつかのHTMLコンテンツ

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 

を送信これ以上ヘッダーはありません。

今度はPdfクラスに電話します。このクラスはコンテンツタイプを設定しようとします

response.setContentType("application/pdf"); 

これはもう動作しません。これは、温暖化があなたに伝えるものかもしれません。私はEJPが想定しているように、スペイン語を外国語として読むことはできません。

は、その後、あなたのクラスは、結果ストリームにPDFを生成し、その後、再度、いくつかのHTML送る:このよう

</body> 
</html> 

を、JSPページは、text/htmlのは、PDFファイルをいくつかのHTMLタグが含まれている応答を入力生成しました、そしていくつかのhtmlタグ。これは有効なhtmlでも有効なpdfでもありません。

一般に、JSPページではpdfs(または一般的にはバイナリ結果)を生成すべきではありません。しかし、もしそうであれば、偶然にもHTMLを送信しないよう注意しなければなりません。

したがって、すべてのhtmlを削除し、空の行を<% ...%>ブロックの外に削除します。

関連する問題