2017-04-25 89 views
-1

我有这个问题,我创建和pdf在类中调用页面jsp。 这是代码。 PDF工作,但在控制台中,我有一个错误。servlet响应中的错误getOutputStream()

的index.jsp - >这是JSP页面

<%@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 - >这是类的包装

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();  





     } 
} 

和错误创建。 - >这是有关控制台中的错误的信息。

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页面中,你首先设置内容类型为text/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页面中的文本/ HTML类型的响应里面包含了一些HTML标记,一个PDF ,还有一些html标签。这既不是有效的html也不是有效的pdf。

通常,在JSP页面中不应该生成pdf(或一般的二进制结果)。但是如果有人这样做,就必须注意不要意外发送任何html,甚至不要有空行。

因此,删除<% ...%>块以外的所有html和任何空行。