How to create a web interface for .jrxml file

After two full days work, I managed to create a web interface for jasper reports. Now I can open a .pdf file in the browser created using any .jrxml file.

The procedure to open a .pdf in a web browser created using .jrxml file :

Note: I’m using postgresql and created a database called “jasper” by giving the username “postgres” and the password “root”.
Database driver: org.postgresql.Driver
Location: jdbc:postgresql://localhost:5432/jasper”,”postgres”,”root”

1. Open netbeans and create a new project “WebApplication”. When you are creating the new project select your server. Here I am using Tomcat 5.5 .

2. Right click on the “Libraries” folder in the “WebApplication” project.
Click on “Add jar/folder” and select the given list of .jar files.

* commons-beanutils-1.8.0.jar
* commons-collections-3.2.1.jar
* commons-digester-1.7.jar
* commons-logging-1.1.jar
* groovy-all-1.5.5.jar
* iText-2.1.0.jar
* spring.jar
* jasperreports-3.6.2.jar
* postgresql-8.3.-603.jdbc3.jar
(This is for postgresql users only. Select this .jar file according to the database you are using.)

If you have installed iReports-3.6.2 in the default path you could find those files in “C:\Program Files\Jaspersoft\iReport-3.6.2\ireport\modules\ext”

3.Create a new Servlet as “Jasper” and copy and paste these code there.

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
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.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;

public class Jasper extends HttpServlet
{

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{

JasperReport jasperReport = null;
JasperPrint jasperPrint;

Connection conn=null;
ServletOutputStream servletOutputStream = response.getOutputStream();

try{
Class.forName(“org.postgresql.Driver”);
System.out.println(“I am done”);

conn= DriverManager.getConnection(“jdbc:postgresql://localhost:5432/jasper”,”postgres”,”root”);
System.out.print(“connected”);

jasperReport = JasperCompileManager.compileReport(getServletConfig().getServletContext().getRealPath(“/reports/jasperreport_demo.jrxml”));

} catch (Exception ex) {
ex.printStackTrace();
}

byte[] bytes = null;

try
{
bytes = JasperRunManager.runReportToPdf(jasperReport,new HashMap(), conn);

response.setContentType(“application/pdf”);
response.setContentLength(bytes.length);

servletOutputStream.write(bytes, 0, bytes.length);
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());
}
}
}

4. Build your web project. Don’t worry about the errors you get 😉

5.Go to the folder where your netbeans web project is and go to the web folder inside the build folder(..\WebApplication\build\web) and create a new folder called “reports”.

6. Copy your “jasperreport_demo.jrxml” file from the place where you have created it and paste it inside the “reports” folder you just create.

7. Build your project and Run the “Jasper.java” servlet created using the above code.

The .pdf file will be display in your web browser with the data you retrieved from the database.

Well…A happy ending..

Advertisements

Author: Harshani Nawarathna

Sri Lankan woman in Computing

10 thoughts on “How to create a web interface for .jrxml file”

  1. Hiiii I follow all u r instruction and i get some Null Pointer Exception on the browser ,only thing is that i made a little change in the Jdbc Connection as follows:

    JasperReport jasperReport = null;
    JasperPrint jasperPrint;

    Connection conn=null;
    ServletOutputStream servletOutputStream = response.getOutputStream();
    Class.forName(“oracle.jdbc.driver.OracleDriver”);
    conn = DriverManager.getConnection
    (“jdbc:oracle:thin:@10.0.0.25:1521:xe”, “user_name”,”password”);

    rest all code was same as yours …

    and the exception that occured was like
    java.lang.NullPointerException
    net.sf.jasperreports.engine.fill.JRFiller.createFiller(JRFiller.java:134)
    net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:53)
    net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:417)
    net.sf.jasperreports.engine.JasperRunManager.runReportToPdf(JasperRunManager.java:281)
    Jasper.Jasper.doGet(Jasper.java:72)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)

    any idea what could possible reaon for this errors…

    Like

  2. hi ShashankRaj,

    Did you add the relevant jar file of your database to the library?

    In my case it is postgresql-8.3.-603.jdbc3.jar (This is for postgresql users only. Select this .jar file according to the database you are using.)

    Like

  3. Hiii ,thanx for the reply ….i have added the jars u mentioned including odjbc14.jar for oracle 10g still the problem does not seems to get resolved…also on server console this was printed System.out.println(“connected”) after connection …that shows there was no problem with the connection..
    guess the exception is thrown due to this line
    jasperReport = JasperCompileManager.compileReport(getServletConfig().getServletContext().getRealPath(“/reports/FirstReport.jrxml”));
    i guess problem occured as it not able to find the path of the .jrxml file and so
    before this i print this line System.out.println(“Current working directory: “+System.getProperty(“user.dir”));
    at which i got
    C:\Program Files\NetBeans 6.7.1 ..do let me
    know any other possible reason ..if u think???

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s