1

I Am trying to generate a table using iText (for testing only, thats why i am not using any servlet and class).I have used this tutorial as my reference. The code doesnt shows any errors but am getting an exception in run time

org.apache.jasper.JasperException: getOutputStream() has already been called for this response

I use itext version 5.3.2,java 5. my code is given below.

MY CODE

<%@page import="java.io.DataOutputStream"%>
<%@page import="com.itextpdf.text.DocumentException"%>
<%@page import="java.io.DataOutput"%>
<%@page import="com.itextpdf.text.pdf.PdfPTable"%>
<%@page import="com.itextpdf.text.pdf.PdfWriter"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="com.itextpdf.text.Document"%>       
<%

response.setContentType("application/pdf");
Document document = new Document();
try{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter.getInstance(document, buffer);
document.open();


PdfPTable table = new PdfPTable(2);
table.addCell("1");
table.addCell("2");
table.addCell("3");
table.addCell("4");
table.addCell("5");
table.addCell("6");


document.add(table);
document.close();

DataOutput dataOutput = new DataOutputStream(response.getOutputStream());
byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
for(int i = 0; i < bytes.length; i++)
{
dataOutput.writeByte(bytes[i]);
}

}catch(DocumentException e){
e.printStackTrace();
}

%>

EXCEPTION

org.apache.jasper.JasperException: getOutputStream() has already been called for this response
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:387)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.lang.IllegalStateException: getOutputStream() has already been called for this response
    org.apache.catalina.connector.Response.getWriter(Response.java:606)
    org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:195)
    org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:124)
    org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:117)
    org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:191)
    org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:115)
    org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75)
    org.apache.jsp.generatepdf_jsp._jspService(org.apache.jsp.generatepdf_jsp:99)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
thebuffer
  • 47
  • 9

1 Answers1

1

on the top of the page use the this

<%@ page trimDirectiveWhitespaces="true" %>

we should also add flush/close for you dataOutput.

dataOutput.flush();
dataOutput.close();
return;
Dinup Kandel
  • 2,457
  • 4
  • 21
  • 38
  • Thanks for your answer. But, <%@ page trimDirectiveWhitespaces="true" %> shows an error. dataOutput.close(); return; stream close works fine, And I am getting the out put, But still the consolve shows exception like java.lang.IllegalStateException: getOutputStream() has already been called for this response – thebuffer Sep 04 '12 at 04:28
  • if so in the jsp-config section your web.xml *.jsp true – Dinup Kandel Sep 04 '12 at 04:33
  • i put in in web.xml it shows an error with the tag . can you tell me the problem with the code? so i can google it for alternative solutions – thebuffer Sep 04 '12 at 04:36
  • @thebuffer here is the link that show your proper solution http://stackoverflow.com/questions/5519642/jsp-trimspaces-directive-not-working – Dinup Kandel Sep 04 '12 at 04:39
  • 1
    OK i get it. unfortunately i am using tomcat 5. so i think this tag is not supported – thebuffer Sep 04 '12 at 04:41
  • :-) i think you should upgrade you tomcat version. – Dinup Kandel Sep 04 '12 at 04:43