title: How to view a jasper report in pdf on running the jsp page
body: <p><a href="https://i.stack.imgur.com/HlJ8F.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HlJ8F.png" alt="enter image description here"></a></p>
  
  <p>I want to view the jasper report that i created when i run "PDFLEAD.jsp" page. But it is showing like"getWriter() has already been called for this response." and it is displaying as the image given below.The code i tried is as follows</p>
  
  <p>PDFLEAD.jsp</p>
  
  <pre><code>&lt;%@page import = "net.sf.jasperreports.engine.design.JRDesignQuery"%&gt;
  &lt;%@page import = "net.sf.jasperreports.engine.xml.JRXmlLoader"%&gt;
  &lt;%@page import = "net.sf.jasperreports.engine.design.JasperDesign"%&gt;
  &lt;%@page import = "net.sf.jasperreports.view.JasperViewer"%&gt;
  &lt;%@page import = "net.sf.jasperreports.engine.JRException"%&gt;
  &lt;%@page import = "net.sf.jasperreports.engine.JRResultSetDataSource"%&gt;
  &lt;%@page import = "net.sf.jasperreports.engine.JasperCompileManager"%&gt;
  &lt;%@page import = "net.sf.jasperreports.engine.JasperExportManager"%&gt;
  &lt;%@page import = "net.sf.jasperreports.engine.JasperFillManager"%&gt;
  &lt;%@page import = "net.sf.jasperreports.engine.JasperPrint"%&gt;
  &lt;%@page import = "net.sf.jasperreports.engine.JasperReport"%&gt;
  &lt;%@page import = "net.sf.jasperreports.engine.data.JRCsvDataSource"%&gt;
  &lt;%@page import = "net.sf.jasperreports.engine.export.ooxml.JRDocxExporter"%&gt;
  &lt;%@page import = "net.sf.jasperreports.engine.export.JRPdfExporter"%&gt;
  &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;%@ include file="/Purchase.jsp" %&gt;
  
  &lt;%@ page import="net.sf.jasperreports.engine.*" %&gt;
  &lt;%@ page import="java.text.SimpleDateFormat" %&gt;
  &lt;%@ page import="java.text.ParseException" %&gt;
  &lt;%@ page import="java.io.File" %&gt;
  &lt;%@ page import="java.io.FileInputStream" %&gt;
  &lt;%@ page import="java.io.FileNotFoundException" %&gt;
  &lt;%@ page import="java.io.InputStream" %&gt;
  &lt;%@ page import="java.sql.Connection" %&gt;
  &lt;%@ page import="java.sql.DriverManager" %&gt;
  &lt;%@ page import="java.sql.SQLException" %&gt;
  &lt;%@ page import="java.io.FileOutputStream" %&gt;
  &lt;%@ page import="java.io.ByteArrayOutputStream" %&gt;
  &lt;%@ page import="java.io.OutputStream" %&gt;
  &lt;%@ page import="java.util.HashMap" %&gt;
  &lt;%@ page import="java.util.Map" %&gt;
  &lt;%@ page import="net.sf.jasperreports.engine.util.*" %&gt; 
  &lt;%@ page import="net.sf.jasperreports.engine.export.*" %&gt;
  
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;% 
  Connection conn = null;
  String sear=(String)session.getAttribute("sea");
  String cate=(String)session.getAttribute("cat");
  String comp=(String)session.getAttribute("com");
  
  System.out.println("1 is:"+sear);
  System.out.println("2 is:"+cate);
  System.out.println("3 is:"+comp);
  
  try 
  {
      Class.forName("com.mysql.jdbc.Driver");
      conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/marketing_database","root","root");
      String jrxmlFile ="D:/dev/tools/jasper files/report10.jrxml";
      InputStream input = new FileInputStream(new File(jrxmlFile));
      JasperDesign jasperDesign = JRXmlLoader.load(input);
  
      System.out.println("Compiling Report Designs");
      JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
  
      System.out.println("Creating JasperPrint Object");
      HashMap&lt;String,Object&gt; map = new HashMap&lt;String,Object&gt;();
      map.put("complan",comp);
      map.put("search",sear);
      map.put("category",cate);
  
      JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,map,conn);
      byte bytes[] = new byte[10000]; 
      JRPdfExporter exporter = new JRPdfExporter();
      ByteArrayOutputStream Stream = new ByteArrayOutputStream(); 
      exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 
      exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, Stream); 
      exporter.exportReport(); 
      System.out.println("Size of byte array:"+Stream.size()); 
      bytes = Stream.toByteArray(); 
      response.setContentType("application/pdf"); 
      System.out.println("After JasperPrint = 1"); 
      response.setContentLength(bytes.length); 
      System.out.println("After JasperPrint = 2"); 
      Stream.close(); 
      System.out.println("After JasperPrint = 3"); 
  
      OutputStream outputStream = response.getOutputStream(); 
      System.out.println("After JasperPrint = 4"); 
      outputStream.write(bytes, 0, bytes.length); 
      outputStream.flush(); 
      outputStream.close(); 
  
  }
  catch(Exception e) 
  {e.printStackTrace();} 
  
  
  %&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>