error while generating pdf report

0
By: madhuri - madhuri_ls
error while generating pdf report
2004-03-11 02:01
Hi

while generating PDF report java is throwing exception :

StandardWrapperValve[debugjsp]: Servlet.service() for servlet debugjsp threw exception

java.lang.IllegalStateException: getOutputStream() has already been called for this response

java.lang.IllegalStateException: getOutputStream() has already been called for this response

at org.apache.catalina.connector.ResponseBase.getWriter(ResponseBase.java:755)

at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:162)

at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:166)

at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:158)

at org.apache.jasper.runtime.JspWriterImpl.flush(JspWriterImpl.java:205)

at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:176)

at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:198)

at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:193)

at org.apache.jsp.PdfMerchantPaymentReportView$jsp._jspService(PdfMerchantPaymentReportView$jsp.java:445)

at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:107)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at org.apache.jasper.servlet.JspServlet$JspServletWrapper.service(JspServlet.java:202)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:382)

at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:474)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:247)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:193)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:243)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:201)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2344)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:170)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:170)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:564)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:163)

at org.apache.catalina.core.StandardPipeline.invokeNext(StandardPipeline.java:566)

at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:472)

at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:943)

at org.apache.catalina.connector.http.HttpProcessor.process(HttpProcessor.java:1011)

at org.apache.catalina.connector.http.HttpProcessor.run(HttpProcessor.java:1106)

at java.lang.Thread.run(Thread.java:484)





please help




By: Chuck Deal - cdeal
RE: error while generating pdf report
2004-03-11 08:11
Search the forums for getOutputStream and you should find a couple of discussions on the topic.

But the long and short of it is: it looks like you are using a JSP. You probably have used the 'out' variable somewhere on your page. If you check the documentation, you cannot use both 'out' and response.getOutputStream(), as they conflict with each other (or something along those lines).




By: madhuri - madhuri_ls
RE: error while generating pdf report
2004-03-11 22:02
This is the generated servlet by JSP .


package org.apache.jsp;

import dori.jasper.engine.design.JasperDesign.*;
import dori.jasper.engine.*;
import dori.jasper.engine.util.*;
import dori.jasper.engine.export.*;
import java.util.*;
import java.io.*;
import com.ads.hsbc.mps.db.DbConnector;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import org.apache.jasper.runtime.*;


public class PdfMerchantPaymentReportView$jsp extends HttpJspBase {

// begin [file="/PdfMerchantPaymentReportView.jsp";from=(10,3);to=(10,27)]
java.sql.Date fromDate;
// end
// begin [file="/PdfMerchantPaymentReportView.jsp";from=(18,0);to=(18,87)]
// end

static {
}
public PdfMerchantPaymentReportView$jsp( ) {
}

private static boolean _jspx_inited = false;

public final void _jspx_init() throws org.apache.jasper.runtime.JspException {
}

public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {

JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
String _value = null;
try {

if (_jspx_inited == false) {
synchronized (this) {
if (_jspx_inited == false) {
_jspx_init();
_jspx_inited = true;
}
}
}
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=ISO-8859-1");
pageContext = _jspxFactory.getPageContext(this, request, response,
"", true, 8192, true);

application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();

// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(0,41);to=(1,0)]
out.write("\r\n");

// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(1,35);to=(2,0)]
out.write("\r\n");

// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(2,61);to=(3,0)]
out.write("\r\n");

// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(3,41);to=(4,0)]
out.write("\r\n");

// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(4,46);to=(5,0)]
out.write("\r\n");

// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(5,48);to=(6,0)]
out.write("\r\n");

// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(6,32);to=(7,0)]
out.write("\r\n");

// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(7,30);to=(8,0)]
out.write("\r\n");

// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(8,52);to=(9,0)]
out.write("\r\n");

// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(9,31);to=(10,0)]
out.write("\r\n");

// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(10,29);to=(11,0)]
out.write("\r\n");

// end
// begin [file="/PdfMerchantPaymentReportView.jsp";from=(11,2);to=(17,0)]

response.resetBuffer();
String strQry="";
String userName="";
boolean dataAvailable=true;
ServletOutputStream outputStream=null;
// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(17,2);to=(18,0)]
out.write("\r\n");

// end
// begin [file="/PdfMerchantPaymentReportView.jsp";from=(18,0);to=(18,87)]
com.ads.hsbc.mps.common.CommonBean commonBean = null;
boolean _jspx_specialcommonBean = false;
synchronized (pageContext) {
commonBean= (com.ads.hsbc.mps.common.CommonBean)
pageContext.getAttribute("commonBean",PageContext.PAGE_SCOPE);
if ( commonBean == null ) {
_jspx_specialcommonBean = true;
try {
commonBean = (com.ads.hsbc.mps.common.CommonBean) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "com.ads.hsbc.mps.common.CommonBean");
} catch (ClassNotFoundException exc) {
throw new InstantiationException(exc.getMessage());
} catch (Exception exc) {
throw new ServletException (" Cannot create bean of class "+"com.ads.hsbc.mps.common.CommonBean", exc);
}
pageContext.setAttribute("commonBean", commonBean, PageContext.PAGE_SCOPE);
}
}
if(_jspx_specialcommonBean == true) {
// end
// begin [file="/PdfMerchantPaymentReportView.jsp";from=(18,0);to=(18,87)]
}
// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(18,87);to=(19,0)]
out.write("\r\n");

// end
// begin [file="/PdfMerchantPaymentReportView.jsp";from=(19,2);to=(285,3)]

String date="";
String tDate="";
String tableName="";
String table1="";
String table2="";
String table3="";
String temp="";
String description="";
String regionCode="";
String uploadid="";
temp=request.getParameter("sDate");
tDate=request.getParameter("sDate");
regionCode=request.getParameter("regionCode");
userName=session.getAttribute("HSBCMPSUserCode").toString();
tableName=""+temp.substring(3,5)+temp.substring(8);
table1="mdtp_"+tableName;/*0603*/
table2="txcp_"+tableName;
table3="txsp_"+tableName;
if (commonBean.checkTable(table2)==false){
/*outputStream.write("<html><body><table border=0 align=center><tr><td align=center><br>");
outputStream.write("<font style='FONT-SIZE: 10pt;FONT-FAMILY: Verdana, Arial,Helvetica; COLOR: #ff0000; FONT-WEIGHT: bold;text-align:center;'>");
outputStream.write("The data for the given date is not uploaded.<br>&nbsp;<a href='javascript:window.close();'>Close</a>");
outputStream.write("</font>");
outputStream.write("</td></tr></table></body></html>");
*/
dataAvailable=false;
}else{
dataAvailable=true;
}
regionCode=request.getParameter("regionCode");
if (dataAvailable==true){

//try{
java.text.SimpleDateFormat sdf=new java.text.SimpleDateFormat("dd-MM-yyyy");
fromDate=new java.sql.Date(sdf.parse(tDate.trim()).getTime());
dataAvailable=true;
uploadid=commonBean.checkUpload(fromDate.toString());
/* }catch(Exception e){
dataAvailable=false;
outputStream.write("<html><body><table border=0 align=center><tr><td align=center><br>");
outputStream.write("<font style='FONT-SIZE: 10pt;FONT-FAMILY: Verdana, Arial,Helvetica; COLOR: #ff0000; FONT-WEIGHT: bold;text-align:center;'>");
outputStream.write("Error Invalid Date<br>&nbsp;<a href='javascript:window.close();'>Close</a>");
outputStream.write("</font>");
outputStream.write("</td></tr></table></body></html>");
}*/
}
if ("ERROR".equalsIgnoreCase(uploadid.substring(0,5))){
/*outputStream.write("<html><body><table border=0 align=center><tr><td align=center><br>");
outputStream.write("<font style='FONT-SIZE: 10pt;FONT-FAMILY: Verdana, Arial,Helvetica; COLOR: #ff0000; FONT-WEIGHT: bold;text-align:center;'>");
outputStream.write(uploadid);
outputStream.write("<a href='javascript:window.close();'>Close</a>");
outputStream.write("</font>");
outputStream.write("</td></tr></table></body></html>");*/
}else{
DbConnector db=null;
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
File reportFile=null;
if (dataAvailable==true){
//try{
//creating the connection
db = new DbConnector();
Class.forName(db.dbDriver);
con = DriverManager.getConnection(db.dbURL,db.dbUser,db.dbPassword);
strQry="";
strQry="delete from usrmpr where username='"+userName+"' ";
ps = con.prepareStatement(strQry);
ps.executeUpdate();
ps.close();
strQry=" insert into usrmpr select paymentmode,lpad(accountbranch,3,'0') as accountbranch,"+
" lpad(accountnumberserial,6,'0') as accountnumberserial,"+
" lpad(accountsuffix,3,'0') as accountsuffix,"+
" cardcentreregioncode,merchantname, "+
" 0 as netamount, "+
" sum(adjustmentamount)/100 as adjustmentamount, "+
" case when(sum(((netamount))+((adjustmentamount)))/100) < 0 then 0 else "+
" (sum(((netamount))+((adjustmentamount)))/100) end as grossdepamount, sequenceno , "+
" sum(coamount)/100 as coamount ,sum(tramount)/100 as tramount, "+
" sum(mpamount)/100 as mpamount,cocount,'"+fromDate+"' as postdate , "+
" '"+userName+"' as username "+
" from ( "+
" select "+
" 'B' as paymentmode,md.cardcentreregioncode, "+
" lpad(md.accountbranch,3,'0') as accountbranch,"+
" lpad(md.accountnumberserial,6,'0') as accountnumberserial,"+
" lpad(md.accountsuffix,3,'0') as accountsuffix,"+
" case when md.paymentname1 is null then "+
" md.paymentname else (md.paymentname || md.paymentname1) end "+
" as merchantname, "+
" sum(((txs.netamount))) as netamount, "+
" sum(((txs.adjustmentamount+ case when txs.netamount=0 then 0 else (txs.netdepamount-txs.netamount) end))) as adjustmentamount, "+
" sum((txs.netamount + txs.adjustmentamount)) as grossdepamount, "+
" cod.sequenceno::text as sequenceno , "+
" sum((txs.netdepamount)) as coamount, "+
" sum(0.00) as tramount,sum(0.00) as mpamount, "+
" count(cod.sequenceno) as cocount, "+
" '"+fromDate+"' as postdate "+
" from "+
" "+table3+" as txs, "+
" "+table1+" as md, cash_order_details as cod "+
" where "+
" md.cardcentreregioncode = txs.areacode "+
" and md.accountbranch = txs.accountbranch "+
" and md.accountnumberserial = txs.accountnumberserial "+
" and md.accountsuffix = txs.accountsuffix "+
" and md.paymentmode = 'CO' "+
" and md.cardcentreregioncode ='"+regionCode+"' "+
" and txs.uploadid=md.uploadid "+
" and txs.accountbranch = cod.accountbranch "+
" and txs.accountnumberserial = cod.accountnumberserial "+
" and txs.accountsuffix = cod.accountsuffix "+
" and cod.codate='"+fromDate+"' "+
" and txs.uploadid ='"+uploadid+"' "+
" group by md.accountbranch,md.accountnumberserial,md.accountsuffix,"+
" md.cardcentreregioncode, md.paymentname,md.paymentname1,sequenceno "+
" union all "+
" select "+
" 'C' as paymentmode,md.cardcentreregioncode, "+
" lpad(md.accountbranch,3,'0') as accountbranch,"+
" lpad(md.accountnumberserial,6,'0') as accountnumberserial,"+
" lpad(md.accountsuffix,3,'0') as accountsuffix,"+
" case when md.paymentname1 is null then "+
" md.paymentname else (md.paymentname || md.paymentname1) end "+
" as merchantname, "+
" sum((txs.netamount)) as netamount, "+
" sum(((txs.adjustmentamount+ case when txs.netamount=0 then 0 else (txs.netdepamount-txs.netamount) end))) as adjustmentamount, "+
" sum((txs.netamount + txs.adjustmentamount)) as grossdepamount, "+
" md.accounttransfer as sequenceno, sum(0.00) as coamount , "+
" sum((txs.netdepamount)) as tramount, "+
" sum(0.00) as mpamount,sum(0) as cocount,'"+fromDate+"' as postdate "+
" from "+
" "+table3+" as txs, "+
" "+table1+" as md "+
" where md.cardcentreregioncode = txs.areacode "+
" and md.accountbranch = txs.accountbranch "+
" and md.accountnumberserial = txs.accountnumberserial "+
" and md.accountsuffix = txs.accountsuffix "+
" and md.paymentmode = 'TR' "+
" and md.cardcentreregioncode ='"+regionCode+"' "+
" and txs.uploadid=md.uploadid "+
" and txs.uploadid ='"+uploadid+"' "+
" group by md.accountbranch,md.accountnumberserial,md.accountsuffix,"+
" md.cardcentreregioncode, md.paymentname,md.paymentname1,sequenceno "+
" union all "+
" select "+
" 'A' as paymentmode,md.cardcentreregioncode, "+
" lpad(md.accountbranch,3,'0') as accountbranch,"+
" lpad(md.accountnumberserial,6,'0') as accountnumberserial,"+
" lpad(md.accountsuffix,3,'0') as accountsuffix,"+
" case when md.paymentname1 is null then "+
" md.paymentname else (md.paymentname || md.paymentname1) end "+
" as merchantname, "+
" sum((txs.netamount) ) as netamount, "+
" sum(((txs.adjustmentamount+ case when txs.netamount=0 then 0 else (txs.netdepamount-txs.netamount) end))) as adjustmentamount, "+
" sum((txs.netamount + txs.adjustmentamount)) as grossdepamount, "+
" null::text as sequenceno,0.00 as coamount , "+
" sum(0.00) as tramount, "+
" sum((txs.netdepamount)) as mpamount, "+
" sum(0) as cocount,'"+fromDate+"' as postdate "+
" from "+
" "+table3+" as txs, "+
" "+table1+" as md "+
" where "+
" md.cardcentreregioncode = txs.areacode "+
" and md.accountbranch = txs.accountbranch "+
" and md.accountnumberserial = txs.accountnumberserial "+
" and md.accountsuffix = txs.accountsuffix "+
" and md.paymentmode = 'MP' "+
" and md.cardcentreregioncode ='"+regionCode+"' "+
" and txs.uploadid=md.uploadid "+
" and txs.uploadid ='"+uploadid+"' "+
" group by md.accountbranch,md.accountnumberserial,md.accountsuffix,"+
" md.cardcentreregioncode, md.paymentname,md.paymentname1,sequenceno "+
" ) as subq group by paymentmode,accountbranch,accountnumberserial,accountsuffix,cardcentreregioncode, merchantname,sequenceno ,cocount,postdate "+
" order by paymentmode,accountbranch,accountnumberserial,accountsuffix,sequenceno ";
ps = con.prepareStatement(strQry);
ps.executeUpdate();
ps.close();
//System.out.println(strQry);
strQry= " update usrMPR set netamount=subq.netamount "+
" from "+
" (select mpr.accountbranch, mpr.accountnumberserial, mpr.accountsuffix,"+
//" case when abs(sum(netamount))=sum(adjustmentamount) then 0.00 else sum(netamount)/100 end as netamount, "+
" sum(paymentamount/100) as netamount "+
" from "+table2+" as txc, "+
" usrMPR as mpr where mpr.cardcentreregioncode = txc.regioncode "+
" and mpr.accountbranch = lpad(txc.accountbranch,3,'0') "+
" and mpr.accountnumberserial = lpad(txc.accountnumberserial,6,'0') "+
" and mpr.accountsuffix = txc.accountsuffix and txc.uploadid ='"+uploadid+"' "+
" and mpr.cardcentreregioncode ='"+regionCode+"' "+
" group by mpr.accountbranch,mpr.accountnumberserial,mpr.accountsuffix) "+
" as subq where usrmpr.accountbranch = subq.accountbranch and "+
" usrmpr.accountnumberserial = subq.accountnumberserial "+
" and usrmpr.accountsuffix = subq.accountsuffix "+
" and usrmpr.username='"+userName+"' ";

//System.out.println(strQry);
ps = con.prepareStatement(strQry);
ps.executeUpdate();
ps.close();
strQry=" select * from usrmpr where username='"+userName+"' "+
" order by paymentmode,accountbranch,accountnumberserial,accountsuffix,sequenceno ";
response.resetBuffer();
reportFile = new File(application.getRealPath("/reports/PdfMerchantPaymentReport.jasper"));
Map parameters = new HashMap();
parameters.put("strQry",strQry);
parameters.put("table1",table1);
parameters.put("table2",table2);
parameters.put("table3",table3);
parameters.put("regionCode",regionCode);

dori.jasper.engine.design.JasperDesign jasperDesign = null;
try {
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile);
JasperPrint jasperPrint = JasperManager.fillReport( jasperReport, parameters,
con);

byte[] pdfStream = JasperManager.printReportToPdf( jasperPrint );

response.setContentType("application/pdf");
response.setContentLength( pdfStream.length );
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(pdfStream, 0, pdfStream.length);
ouputStream.flush();
ouputStream.close();


} catch (JRException e) {
e.printStackTrace(); //To change body of catch statement use Options | File Templates.
}

/* response.setContentType("application/pdf");
byte[] bytes =
JasperRunManager.runReportToPdf(
reportFile.getPath(),
parameters,
con
);
response.setContentLength(bytes.length);
outputStream = response.getOutputStream();
outputStream.write(bytes, 0, bytes.length);*/
//outputStream.flush();
//outputStream.close();
//response.reset();
/*}catch(Exception exceptionReport){
outputStream.write("Error: " + exceptionReport.getMessage());
}finally{
try{*/
if(rs!=null){
rs.close();
}
if(ps!=null){
ps.close();
}
if(con!=null){
con.close();
}
dataAvailable=false;
/*}catch(Exception exceptionDeleteFile){
response.getWriter().println(exceptionDeleteFile.getMessage());
}
}*/
}
}

// end
// HTML // begin [file="/PdfMerchantPaymentReportView.jsp";from=(285,5);to=(286,0)]
out.write("\r\n");

// end

} catch (Throwable t) {
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (pageContext != null) pageContext.handlePageException(t);
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(pageContext);
}
}
}


pls help





By: madhuri - madhuri_ls
RE: error while generating pdf report
2004-03-11 22:00
ya i am using JSP. when it gets converted to servlet it adds out.write("\r\n"); & it throws same exception.

any solution for this ?? should i use servlets instead of JSPs ??

Thanks




By: Chuck Deal - cdeal
RE: error while generating pdf report
2004-03-12 05:03
I switched to a Servlet to ease my pain as well. There are samples of how to generate reports with JSP, but Servlets are just as easy (if not better) for this job.
2005 IR Help's picture
Joined: Aug 9 2006 - 3:40am
Last seen: 7 years 11 months ago

Answers:

No answers yet
Feedback