-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求助。
錯誤出現在'HttpServletResponse.getOutputStream()'中。與iText無論如何都沒有關係。即使對我來說,錯誤信息也很清楚,我甚至不會說西班牙語。 – EJP