0

In my java web application I want to print the information of a user when I enter his id, I used the plugin jasperreport with netbeans, the problem is that when I compiled and previewed the report it worked, but when I used my page html I had an empty page and this error:

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

page.html

  <form action="info.jsp">
   <input type="text"  name="id" >
  </form>

info.jsp

  <body>
    <% 

  int id=Integer.parseInt(request.getParameter("id"));

 try{
 Class.forName("com.mysql.jdbc.Driver");
 Connection conn=DriverManager.getConnection("jdbc:mysql://localhost/test","root","");
Statement st = conn.createStatement();
File reportFile = new File(application.getRealPath("//info.jasper"));
Map param=new HashMap();
param.put("id", id);
 byte[]bytes=JasperRunManager.runReportToPdf(reportFile.getPath(),param,conn);
  response.setContentType("application/pdf");
  response.setContentLength(bytes.length);
  ServletOutputStream outStream=response.getOutputStream();
  outStream.write(bytes,0,bytes.length);
  outStream.flush();
  outStream.close();
  }catch(Exception ex){
  ex.printStackTrace();
  }      

    %>

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
mass_develop
  • 43
  • 1
  • 7
  • http://stackoverflow.com/tags/servlets/info – BalusC May 18 '15 at 21:40
  • I saw the other answers but it didn't work for me, here the list of libraries which I downloaded: `jasperreports-6.0.4.jar jasperreports-font-6.0.4.jar jasperreports-javaflow-6.0.4.jar barecode4j-2.1.jar commons-beanutils-1.9.0.jar commons-codec-1.5.jar commons-collections-3.2.1.jar commons-digester-2.1.jar commons-logging-1.1.1.jar iText-2.1.7.js2.jar com-jaspersoft-ireport.jar ` – mass_develop May 19 '15 at 09:13
  • All the answers tell to use a servlet. JSP is a HTML code producer. You want to produce a PDF file, not a HTML file with PDF content inside. If you absolutely insist in abusing a JSP for this, then remove ALL characters outside `<% %>`, otherwise JSP would write them anyway via `getWriter()`. As told in those answers. – BalusC May 19 '15 at 09:14
  • Thanks for the reply, I used a servlet but I had an empty page, I have a question, I used JSP and I tried to print the list of the users and it worked very well, the problem is that I want to print the information of a particular user by entering his id. – mass_develop May 19 '15 at 10:07
  • @BalusC I changed and I used a servlet, the problem is when I want the list of users, it works very well but if I want to print a particular user I had an empty page ? – mass_develop May 21 '15 at 18:43

0 Answers0