By: Outey Ky - chhunak
problem displaying pdf: Cannot set header
2002-10-29 08:03
Hi,
I am encountering the following problem when I am trying to generate the pdf output of the JasperReports. I am using Websphere 3.5.3 and the server output the following message:
ERROR: Cannot set header. Response already committed.
Here is my servlet code. (My jsp page that I am targetting only contain javascripts, html, and a few scriptlet.)
public void doDisplayTestReport(HttpServletRequest request, HttpTaskTarget target)
throws javax.servlet.ServletException, java.io.IOException {
HttpServletResponse resp = target.getHttpServletResponse();
//check to verify if connection is still active
if (getUser(request) == null) {
goRoot(request, target.getHttpServletResponse());
return;
}
java.sql.Connection connection = null;
ServletContext context = this.getServletConfig().getServletContext();
try {
connection = getConnection(request, target);
//********************************************************
File reportFile = new File(context.getRealPath("/reports/mydoc.jasper"));
Map parameters = new HashMap();
parameters.put("ReportTitle","The Test JasperReport");
parameters.put("BaseDir", reportFile.getParentFile());
byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,connection);
if (bytes !=null && bytes.length > 0) {
resp.setContentType("application/pdf");
//resp.setHeader("Cache-Control", "no-cache"); I try this.
resp.setContentLength(bytes.length);
ServletOutputStream outputStream = resp.getOutputStream();
outputStream.write(bytes, 0, bytes.length);
outputStream.flush();
outputStream.close();
}
else {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>JasperReports - Test Sample Report</title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<span class=\"bold\">Empty response.</span>");
out.println("</body>");
out.println("</html>");
System.out.println("There have been an error in creating the report.");
}
//********************************************************
//target view jsp page
stockMenuAdmin(connection, request);
target.setName("view_DisplayTestReport");
return;
} catch (Exception e) {
goBack(
request,
target.getHttpServletResponse(),
getError(request, "19", "Error", e));
return;
} finally {
releaseConnection(connection);
}
}
Any help is greatly appreciated...
By: Teodor Danciu - teodord
RE: problem displaying pdf: Cannot set header
2002-10-29 13:00
Hi,
The problem is that if you have already started
to output HTML content (javascript code included)
in your JSP page, you cannot open an output
stream to start sending bytes.
Too late for that ... This is why you get the error.
You can find details about this in the servlet API
documentation.
Anyway, with JSP you can never be sure that even
if you don't have HTML content, the server does not
call response.getWriter() and ruin your plans.
Maybe thare are some blank characters and lines
in the JSP that the server does not ignore and
sends them to the browser.
This is why I recommend you to transform your JSP
into a servlet, because this way you have a better
control over the output.
Check the "webapp" sample provided.
I hope this helps.
Teodor
By: Outey Ky - chhunak
RE: problem displaying pdf: Cannot set header
2002-10-29 16:15
Thank you. So, I should place all of my codes in the JSP instead of in both place.
I have shrink my servlet to this:
public void doDisplayTestReport(HttpServletRequest request, HttpTaskTarget target)
throws javax.servlet.ServletException, java.io.IOException {
//HttpServletResponse response = target.getHttpServletResponse();
//check to verify if connection is still active
if (getUser(request) == null) {
goRoot(request, target.getHttpServletResponse());
return;
}
//else continue
java.sql.Connection connection = null;
try {
connection = getConnection(request, target);
//target view jsp page
stockMenuAdmin(connection, request);
target.setName("view_DisplayTestReport");
return;
} catch (Exception e) {
goBack(
request,
target.getHttpServletResponse(),
getError(request, "19", "Error", e));
return;
} finally {
releaseConnection(connection);
}
}
and my JSP (from the example of pdf.jsp)
<%@ page import="dori.jasper.*" %>
<%@ page import="dori.jasper.engine.*" %>
<%@ page import="dori.jasper.engine.images.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="com.ssr.gedi.servlet.*" %>
<%@ page import="com.ssr.gedi.servlet.ServletReport" %>
<%@ page import="com.ssr.gedi.servlet.ServletLogin" %>
<%@ page import="com.ssr.gedi.servlet.ServletFather" %>
<%@ page import="com.ssr.gedi.classes.*" %>
<%
ServletFather servletFather = new ServletFather();
java.sql.Connection connection = null;
//connection = getConnection(request, response);
File reportFile = new File(application.getRealPath("/reports/mydoc.jasper"));
Map parameters = new HashMap();
parameters.put("ReportTitle", "TestReport");
parameters.put("BaseDir", reportFile.getParentFile());
byte[] bytes =
JasperRunManager.runReportToPdf(
reportFile.getPath(),
parameters,
new JREmptyDataSource());
response.setContentType("application/pdf");
response.setContentLength((int)bytes.length);
response.setHeader("ContentDisposition", "attachment; filename=\"mydoc.pdf\""); // I am playing around with this settings.
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
%>
My problem is it open a blank page in html, and when I change the setting of acrobat in preferences, general, and uncheck "Web Browser integration" it opens find in Acrobat after the second time, but the first time it open just junk text starting with this character "%pdf..........etc". I am not sure why?
I would like to keep the Web Browser Integration check and have acrobat open in Internet Explorer.
I am also not able to call my connection....so I default to JREmptyDataSource....I should be able to reference a class with method...such as
ServletFather.getConnection(), but for some reason it was not able to compile.
Thanks.
problem displaying pdf: Cannot set header
2002-10-29 08:03
Hi,
I am encountering the following problem when I am trying to generate the pdf output of the JasperReports. I am using Websphere 3.5.3 and the server output the following message:
ERROR: Cannot set header. Response already committed.
Here is my servlet code. (My jsp page that I am targetting only contain javascripts, html, and a few scriptlet.)
public void doDisplayTestReport(HttpServletRequest request, HttpTaskTarget target)
throws javax.servlet.ServletException, java.io.IOException {
HttpServletResponse resp = target.getHttpServletResponse();
//check to verify if connection is still active
if (getUser(request) == null) {
goRoot(request, target.getHttpServletResponse());
return;
}
java.sql.Connection connection = null;
ServletContext context = this.getServletConfig().getServletContext();
try {
connection = getConnection(request, target);
//********************************************************
File reportFile = new File(context.getRealPath("/reports/mydoc.jasper"));
Map parameters = new HashMap();
parameters.put("ReportTitle","The Test JasperReport");
parameters.put("BaseDir", reportFile.getParentFile());
byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,connection);
if (bytes !=null && bytes.length > 0) {
resp.setContentType("application/pdf");
//resp.setHeader("Cache-Control", "no-cache"); I try this.
resp.setContentLength(bytes.length);
ServletOutputStream outputStream = resp.getOutputStream();
outputStream.write(bytes, 0, bytes.length);
outputStream.flush();
outputStream.close();
}
else {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>JasperReports - Test Sample Report</title>");
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
out.println("</head>");
out.println("<body bgcolor=\"white\">");
out.println("<span class=\"bold\">Empty response.</span>");
out.println("</body>");
out.println("</html>");
System.out.println("There have been an error in creating the report.");
}
//********************************************************
//target view jsp page
stockMenuAdmin(connection, request);
target.setName("view_DisplayTestReport");
return;
} catch (Exception e) {
goBack(
request,
target.getHttpServletResponse(),
getError(request, "19", "Error", e));
return;
} finally {
releaseConnection(connection);
}
}
Any help is greatly appreciated...
By: Teodor Danciu - teodord
RE: problem displaying pdf: Cannot set header
2002-10-29 13:00
Hi,
The problem is that if you have already started
to output HTML content (javascript code included)
in your JSP page, you cannot open an output
stream to start sending bytes.
Too late for that ... This is why you get the error.
You can find details about this in the servlet API
documentation.
Anyway, with JSP you can never be sure that even
if you don't have HTML content, the server does not
call response.getWriter() and ruin your plans.
Maybe thare are some blank characters and lines
in the JSP that the server does not ignore and
sends them to the browser.
This is why I recommend you to transform your JSP
into a servlet, because this way you have a better
control over the output.
Check the "webapp" sample provided.
I hope this helps.
Teodor
By: Outey Ky - chhunak
RE: problem displaying pdf: Cannot set header
2002-10-29 16:15
Thank you. So, I should place all of my codes in the JSP instead of in both place.
I have shrink my servlet to this:
public void doDisplayTestReport(HttpServletRequest request, HttpTaskTarget target)
throws javax.servlet.ServletException, java.io.IOException {
//HttpServletResponse response = target.getHttpServletResponse();
//check to verify if connection is still active
if (getUser(request) == null) {
goRoot(request, target.getHttpServletResponse());
return;
}
//else continue
java.sql.Connection connection = null;
try {
connection = getConnection(request, target);
//target view jsp page
stockMenuAdmin(connection, request);
target.setName("view_DisplayTestReport");
return;
} catch (Exception e) {
goBack(
request,
target.getHttpServletResponse(),
getError(request, "19", "Error", e));
return;
} finally {
releaseConnection(connection);
}
}
and my JSP (from the example of pdf.jsp)
<%@ page import="dori.jasper.*" %>
<%@ page import="dori.jasper.engine.*" %>
<%@ page import="dori.jasper.engine.images.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="javax.servlet.*" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="com.ssr.gedi.servlet.*" %>
<%@ page import="com.ssr.gedi.servlet.ServletReport" %>
<%@ page import="com.ssr.gedi.servlet.ServletLogin" %>
<%@ page import="com.ssr.gedi.servlet.ServletFather" %>
<%@ page import="com.ssr.gedi.classes.*" %>
<%
ServletFather servletFather = new ServletFather();
java.sql.Connection connection = null;
//connection = getConnection(request, response);
File reportFile = new File(application.getRealPath("/reports/mydoc.jasper"));
Map parameters = new HashMap();
parameters.put("ReportTitle", "TestReport");
parameters.put("BaseDir", reportFile.getParentFile());
byte[] bytes =
JasperRunManager.runReportToPdf(
reportFile.getPath(),
parameters,
new JREmptyDataSource());
response.setContentType("application/pdf");
response.setContentLength((int)bytes.length);
response.setHeader("ContentDisposition", "attachment; filename=\"mydoc.pdf\""); // I am playing around with this settings.
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
%>
My problem is it open a blank page in html, and when I change the setting of acrobat in preferences, general, and uncheck "Web Browser integration" it opens find in Acrobat after the second time, but the first time it open just junk text starting with this character "%pdf..........etc". I am not sure why?
I would like to keep the Web Browser Integration check and have acrobat open in Internet Explorer.
I am also not able to call my connection....so I default to JREmptyDataSource....I should be able to reference a class with method...such as
ServletFather.getConnection(), but for some reason it was not able to compile.
Thanks.
0 Answers:
No answers yet