Ireport4.6+tomcat+javabean Data Source Report 1

Source: Internet
Author: User

Boos let use jasperreports do report, can only honestly ask degrees Niang, but online resources and chaos, and many are copy, code do not know if there is no validation 、、、

I did it once, put it out for everyone to learn.

First make sure your Tomcat, JDK, and ireport are configured, and below we start, I use the things myeclipse development site

A very common example

I. Defining BEANS

Productbean.java

Package Test6;public class productbean{    private String productName;    Public String getproductname ()    {        return productName;    }    public void Setproductname (String productName)    {        this.productname = ProductName;    }}
Ii. setting up data sources and creating reports in ireport

1, first add the path, is the path of your compiled class file: IReport, Tools--Options--Path (classpath) add my path is the bottom one, is the compiled class file path

2. Find the Reportdatasources tool in the toolbar and add the data source: Click New, then select JavaBeans Set DataSource

When selected, the following screen will appear:


You need to fill in the name, Factory class, and static method, this place to note, if you have already written a data factory, that is not based on the network to do, then

You need to fill out the factory class column with the path: package. XXXXX, where xxxx is the name of the factory class you created yourself, and in the static method, fill in the function you created, which is the list of returned lists.

This way, the next time I will write it out, today said network-based.

Web-based so you don't have to fill in the following two items, just write the name, like mine is

Create a new report, all the way next after you've created it, it's easy to find a button on the interface that adds data: Select the second option, JavaBean Datasource

My new package is TEST6 so the input classname is test6. Productbean

Click on the right read attributes will appear a Class A ProductName, of course, the following is a new property we created before, add the will of the bar

After adding in the left column of our report in the field to see, drag it into the edit panel, to drag into the details inside, I don't say, do not understand the document

This is then the effect:

Save the compilation, then find the path you create the report, there will be compiled files, my file name is Repor_tservlet, so generate two

We use the above. Jasper file this time, of course, the following is also available, but the code is different, if people need to leave a message, the following will report_servlet.jasper copy to your server folder, just create a new folder on the line

I have created a new Jasper folder,

Write our servlet program below: Beanreportservlet.java

Package Test6;import Java.io.file;import Java.io.ioexception;import java.io.outputstream;import java.net.URLEncoder ; Import Java.util.arraylist;import Java.util.list;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Net.sf.jasperreports.engine.jrdatasource;import Net.sf.jasperreports.engine.jrexporterparameter;import Net.sf.jasperreports.engine.jasperfillmanager;import Net.sf.jasperreports.engine.jasperprint;import Net.sf.jasperreports.engine.jasperreport;import Net.sf.jasperreports.engine.data.jrbeancollectiondatasource;import Net.sf.jasperreports.engine.export.jrpdfexporter;import Net.sf.jasperreports.engine.util.jrloader;import Net.sf.jasperreports.j2ee.servlets.basehttpservlet;public class Beanreportservlet extends httpservlet{private    Static final Long serialversionuid = 348226870594216833L; @Override protected void doget (HttpServletRequest req, HttpSErvletresponse resp) throws Servletexception, IOException {this.dopost (req, resp); } @Override protected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexcepti On, IOException {try {//Generate test data arraylist<productbean> = new arraylist& Lt            Productbean> ();                for (int i = 1; i <=; i++) {Productbean bean = new Productbean ();                Bean.setproductname ("Product" + i);            Data.add (Bean);                        } jrdatasource DataSource = new Jrbeancollectiondatasource (data);            Gets the report template file String Root_path = This.getservletcontext (). Getrealpath ("/");            Root_path = Root_path.replaceall ("\\\\", "/");            String Reportfilepath = Root_path + "Jasper/report_servlet.jasper";         System.out.println ("Jasper file is" + Reportfilepath); File Reportfile =  New File (Reportfilepath);           Generate Jasperprint Jasperreport report = (Jasperreport) jrloader.loadobject (Reportfilepath);            Jasperreport report = (Jasperreport) jrloader.loadobject (reportfile);            Jasperprint Jasperprint = jasperfillmanager.fillreport (report, NULL, DataSource);              Set output format OutputStream Ouputstream = Resp.getoutputstream ();            Resp.setcontenttype ("Application/pdf");              Resp.setcharacterencoding ("UTF-8"); Resp.setheader ("Content-disposition", "attachment;                                  Filename=\ "" + Urlencoder.encode ("PDF Report", "UTF-8") + ". pdf\" ");              Export PDF Jrpdfexporter exporter = new Jrpdfexporter () using the Jrpdfexproter exporter;              Set Jasperprintlist exporter.setparameter (Jrexporterparameter.jasper_print, jasperprint);              Exporter.setparameter (Jrexporterparameter.output_stream, Ouputstream);                Exporter.exportreport ();        Ouputstream.close ();        } catch (Exception e) {e.printstacktrace (); }        }}
This code is i from other netizens over there copy, of course, a lot of problems, can not be used, I modified this should be no problem, at least in my computer environment can be used.

After the new to pay attention to some of the introduction of the jar package, because I am also a beginner, so the most stupid way, is to D:\Program files\jaspersoft\ireport-4.6.0\ireport\ Modules all the jar packages below the Web-inf lib.

This must be no problem, bring in some of the necessary jar packages into your code

Attention:

If you are copying code from somewhere else, take note of the following points

Root_path = Root_path.replaceall ("\\\\", "/");
Many blog writing is not standardized, is not used, the above is the correct way to write a reference: http://www.blogjava.net/hefang/articles/324456.html

Jasperreport report = (Jasperreport) jrloader.loadobject (Reportfilepath);
This line may be error, said Loadobject what has expired, I am depressed for a long time, finally, all the jar package copy to the Web-inf Lib under the solution, the specific still needs to be researched

String Root_path = This.getservletcontext (). Getrealpath ("/");            Root_path = Root_path.replaceall ("\\\\", "/");            String Reportfilepath = Root_path + "Jasper/report_servlet.jasper";

Note that the path changes to your path

After doing it, remember to configure it in Web. xml

<pre name= "code" class= "HTML" ><?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= " Http://java.sun.com/xml/ns/javaee "xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation=" Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><servlet>  <servlet-name>report</servlet-name>  <servlet-class>test6. beanreportservlet</servlet-class>  </servlet>  <servlet-mapping>  <servlet-name >report</servlet-name>  <url-pattern>/report</url-pattern>  </servlet-mapping >  <display-name></display-name>  <welcome-file-list>    <welcome-file> Index.jsp</welcome-file>  </welcome-file-list></web-app>

Basically there's nothing wrong with it, if I haven't missed it 、、、

Run tomcat inside the browser type: Http://localhost:8080/Jb_test6/report

The report is a map name inside the XML, so don't explain it.

 See the bottom of the hint, that means that our operation is successful, click Open to see our generated 100 products now  Good, gameover if there is any problem can leave a message, Need the original code also message
The first serious blog, the sadness of the egg




Ireport4.6+tomcat+javabean Data Source Report 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.