title: how do I display a jasper report in a web browser
body: <p>Good morning everyone, my name is david, and am new in jasper reports (urm..am also new in stack over flow too). I downloaded a tutorial on jasper reports that guided me through the procedures of creating my first report. I have successfully created my <code>.jrxml</code> file, compiled it to a <code>.jasper</code> file and filled it to a <code>.jprint</code> file, all with the aid of java codes, I have also previewed the report successfully with jasper viewer with the aid of ant targets in my <code>build.xml</code> file. </p>
  
  <p><strong>My problem now is how to display the report on a web browser.</strong> </p>
  
  <p>All that was being given to me in the tutorial was a java servlet code that's exports the jasper report to a pdf format which will then be displayed on the browser. Here is the code:</p>
  
  <pre><code>package net.ensode.jasperbook;
  
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.PrintWriter;
  import java.io.StringWriter;
  import java.util.HashMap;
  
  import javax.servlet.ServletException;
  import javax.servlet.ServletOutputStream;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import net.sf.jasperreports.engine.JREmptyDataSource;
  import net.sf.jasperreports.engine.JRException;
  import net.sf.jasperreports.engine.JasperRunManager;
  
  public class FirstReportSendToBrowserServlet extends.  HttpServlet{
  
  @SuppressWarnings("unchecked")
  protected void doGet(HttpServletRequest.  request, HttpServletResponse response)
  throws ServletException, IOException{
  
  ServletOutputStream servletOutputStream = response.getOutputStream();
  InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/reports/FirstReport.jasper");
  
  try{
  JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, new HashMap(), new JREmptyDataSource());
  response.setContentType("application/pdf");
  servletOutputStream.flush();
  servletOutputStream.close();
  
  }
  
  catch(JRException e){
  //display stack trace in the browser
  StringWriter stringWriter = new StringWriter();
  PrintWriter printWriter = new PrintWriter(stringWriter);
  e.printStackTrace(printWriter);
  response.setContentType("text/plain");
  response.getOutputStream().print(stringWriter.toString());
  }
  
  }
  }
  </code></pre>
  
  <p>Now my problem is how am going deploy the above code in a servlet continer. So it will display my jasper report in a pdf format on my web browser. The tutorial said an ant script to automate the process could be found at their website but I searched and did not find any..</p>
  
  <p>So If anyone of you could give me instruction on how to deploy the above code in a servlet container, or give me a link that will instruct me on how to create the ant script that automate the process, I will be very very greatefull cause I have spent almost three days on this problem. </p>