servlet响应中的错误getOutputStream()

时间:2017-04-25 19:37:22

标签: java jsp servlets jboss

我有这个问题我在课堂上创建和pdf与页面jsp的调用。 这是代码。 pdf工作,但在控制台我有一个错误。

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 - &gt;这是在包中创建的类<​​/ p>

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





        }
}   

错误。 - &GT;这是有关控制台错误的信息。

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)

请求帮助。

1 个答案:

答案 0 :(得分: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页面生成了一个text / html类型的响应,其中包含一些html标签,一个pdf,以及一些html标签。这既不是有效的HTML也不是有效的pdf。

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

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