Java – how to display a jasper report in a web browser

jasper-reportsjava

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 .jrxml file, compiled it to a .jasper file and filled it to a .jprint 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 build.xml file.

My problem now is how to display the report on a web browser.

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:

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());
}

}
}

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..

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.

Best Answer

I recommend you to use an IDE such as eclipse. Install this version that contains all the tools for web development.

http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/keplersr1

Then you can create a "dynamic web project" and configure some servlet container (maybe tomcat - http://tomcat.apache.org/download-70.cgi).

And then (finally) you can right-click on your project and choose "export", and then, "war". Save it directly into tomcat's webapp directory.

It will generate a file that you can just drop into your container and hopefully everything will be fine.

Of course, there's a learning curve here, but using an IDE will save your time later.

Check this tutorial - http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.stardust.docs.wst%2Fhtml%2Fwst-integration%2Fdynamic-web-proj.html