2001 JI Open Discussion Posted August 16, 2006 Share Posted August 16, 2006 By: Uniq - uniq_2002 error running JasperReport with servlet..... 2002-09-13 02:48Hi, i am trying to running jasperReport in web application. Its running with JBoss+Tomcat and i am use servlet too. my code is : <%@ page errorPage="jasperError.jsp" %> <%@ page import="java.util.*" %> <%@ page import="java.io.*" %> <%@ page import="java.sql.*" %> <%@ page import="java.awt.*" %> <%@ page import="report.*" %> <%File reportFile = new File(application.getRealPath("/XMLAccess.jasper")); Map parameters = new HashMap(); parameters.put("ReportTitle", "Report Test"); parameters.put("BaseDir", reportFile.getParentFile()); byte[] bytes = JasperRunManager.runReportToPdf( reportFile.getPath(), parameters, RPTAccess.getConnection()); response.setContentType("application/pdf"); response.setContentLength(bytes.length); ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(bytes, 0, bytes.length); ouputStream.flush(); ouputStream.close(); %> i got this error : getWriter() has already been called for this response. and i think my problem is because teh outputstream is use by JSP and servlet. if anybody can give me solution, i am very appreciate it...:-) thanks... Regards, uniQ By: natxo - keoko RE: error running JasperReport with servlet..... 2002-09-13 03:49I think it's not a problem with JasperReports but wiht JSP-Servlet.I found this answer in a forum some time ago and maybe is your problem too. ----- START --- The problem is not that getOutputStream is being called twice. It's that you cannot use both a Writer AND an output stream on the same response object. Because JSP automatically opens a writer to output the content of the JSP, you cannot call getOutputStream() without getting this error. Basically there is a line of code in getOutputStream() that says if(writerUsed) throw IllegalStateException() and it's that simple. I am guessing the idea is that the Writer must have control of the output stream once you decide to use the Writer. There are two solutions I have implemented for accessing a servlet: 1. Use getWriter() from the servlet instead of getOutputStream(). 2. Don't use the RequestDispatcher (or <jsp:include> which uses the request dispatcher), but rather you write your own taglib with your own implementation of include. My own include tag will use a totally seperate socket connection to avoid this problem with a troublesome servlet. It's also necessary if you wish to include content from a different server. Another thing to watch out for: If your servlet returns a valid HTML document, then you will actually be including duplicate tags for <html>, <head>, <body>, etc. and this will result in invalid HTML files. My include tag also handled this by only taking the content of the <body> element from the included file. ---- END ---- good luck. By: natxo - keoko RE: error running JasperReport with servlet.... 2002-09-13 04:20Maybe this URL can help you. http://developer.java.sun.com/developer/JDCTechTips/2001/tt0821_update.html good luck! By: Uniq - uniq_2002 RE: error running JasperReport with servlet..... 2002-09-16 03:10Hi, thanks for the answer..:-) in my servlet i am use the PrintWriter, its make error at getWriter(); now my pdf is can show in browser. but i still get error in my console : getOutputStream() has already been called for this response. thanks...:-) regards, uniQ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now