BIRT integration servlet

Source: Internet
Author: User

Summarize the two BIRT web deployment methods used in the project:

1. Independently deploy the Birt Web Viewer application and generate reports through URL access.

Ii. Integrate Birt Web into existing systems and use servlet and API for direct calls.

 

I. Independent Birt Web applications:

It is undoubtedly the fastest way to use Birt for the first time. In the era of sensitive development, what is more exciting than making the system run immediately? It's a donkey or a horse. Please pull it out and walk away! (This is what we actually do)

As an open-source project, BIRT has done a lot for us selflessly ). You only need to download the Runtime package from the Birt official website. The WebViewerExample package inside is a complete Web application. Use eclipse to import a web project,

The directory is as follows (figure_1 ):

 

 

Store the designed report template in/report/files (for how to design the template, see Appendix 1 ). After the web application is published to tomcat, it can be accessed through the URL.

For example: http: // 127.0.0.1: 8080/report/preview? _ Report = report/files/gz_hard_count.rptdesign & __format = html

 

Using this method, you can easily embed a Birt report on the existing Web, as long as you call the report through URL in the existing system. Simple and practical! This is also the case in our projects. Although there are some problems with processing the format of "Chinese-style" reports, it can be overcome through XSLT conversion and other methods. Until recently, when using a chart report, the image in the saved report file cannot be normally displayed. See http://www.birthome.cn/read.php for details? Tid-2160.html

 

It was this time that I made up my mind to change the deployment method of Birt. View the chart source file generated by the birt preview servlet. The image is a link. The link is maintained by the birt preview servlet. images are automatically generated in folders differentiated by sessions. As a result, the saved chart file cannot be accessed again (as shown below ). We know that the birt api provides RunAndRenderTask to generate files and specify the path for storing Chart files. If you can directly call the API, the problem will be solved.

src = "/report/preview? Export imageid1_custom180b22e1306d2c28f26.png "<br/> alt =" "style =" width: 871.5pt; Height: 372.75pt; "> </img> </P> <p>

 

 

 

2. Integrate Birt servlet.

To make full use of the rich AJAX rendering effects of Birt, it is necessary to fully integrate the Birt servlet for my use. The following describes how to deploy Birt in detail.

 

1: InCreate a new birt-lib folder in WebReport/WEB-INF/lib to store the jar

2: Copy all the jar files under birt-runtime-2_1_0/ReportEngine/lib to the folder created in 1

3: Copy birt-runtime-2_1_0/Report Engine/plugins and Birt-runtime-2_1_0/ReportEngine/configurationToWEB-INFLower

4. Create the reports and images directories to store report template files and images.

5: copy the configuration file to the WEB-INF, jrun. web. xml, server-config.wsdd, viewer. properties. Add the Brit servlet to web. xml.

 

After completing the preceding steps, the directory structure of your application is roughly as shown in figure (figure_2)

 

Then you can customize the servlet, start the birt platform, and call the Birt api to run the birt template and generate a file to the specified path.

2.1 compile BirtEngine. java, load birt platform, and create birtEngine

2.1.1 load birt plugins:

Config. setEngineHome (""); <br/> IPlatformContext context = <br/> new PlatformServletContext (SC); <br/> config. setPlatformContext (context );

Start platform and create birt Engine

Public static synchronized IReportEngine getBirtEngine (ServletContext SC) {<br/> if (birtEngine = null) <br/>{< br/>. <br/>. <br/> try <br/> {<br/> // Start up the OSGi framework <br/> Platform. startup (config); <br/>}< br/> catch (BirtException e) <br/>{< br/> e. printStackTrace (); <br/>}</p> <p> IReportEngineFactory factory = <br/> (IReportEngineFactory) Platform. <br/> createFactoryObject (<br/> IReportEngineFactory. <br/> EXTENSION_REPORT_ENGINE_FACTORY <br/>); </p> <p> birtEngine = <br/> factory. createReportEngine (config); <br/>}< br/> return birtEngine; <br/>}< br/>}

 

2.2 customize servlet (WebReport. java), create RunAndRenderTask to call the report template, and generate a file to the specified directory.

2.2.1: Mount birt platform using the init Method

Public void init () throws ServletException {<br/> BirtEngine. initBirtConfig (); <br/>}

2.2.2: destory method. when the service is disabled, disable birt platform.

Public void destroy () {<br/> super. destroy (); <br/> // call engine and platform shutdown <br/> BirtEngine. destroyBirtEngine (); <br/>}

2.2.3: The doget method calls the Report Template and generates files to the specified directory.

Public void doget (httpservletrequest req, httpservletresponse resp) <br/> throws servletexception, ioexception {</P> <p> // get report name and launch the engine <br/> resp. setcontenttype ("text/html"); <br/> // resp. setcontenttype ("application/pdf"); <br/> // resp. setheader ("content-disposition", "inline; filename=testposition"); <br/> string reportname = req. getparameter ("reportname"); <br/> servletcontext SC = req. getsession (). getservletcontext (); <br/> This. birtreportengine = birtengine. getbirtengine (SC); </P> <p> // setup image directory <br/> htmlrendercontext rendercontext = new htmlrendercontext (); <br/> rendercontext. setbaseimageurl (req. getcontextpath () + "/report/images"); <br/> rendercontext. setimagedirectory (SC. getrealpath ("/report/images"); </P> <p> logger. log (level. fine, "image directory" + SC. g Etrealpath ("/report/images"); <br/> system. out. println ("stdout image directory" <br/> + SC. getrealpath ("/report/images"); </P> <p> hashmap <string, htmlrendercontext> contextmap = new hashmap <string, htmlrendercontext> (); <br/> contextmap. put (engineconstants. appcontext_html_render_context, <br/> rendercontext); </P> <p> ireportrunnable design; <br/> try {<br/> // open report design <br/> Design = birtreport Engine. openreportdesign (SC <br/>. getrealpath ("/report/Files") <br/> + "/" + reportname ); <br/> // Create task to run and render Report <br/> irunandrendertask task = birtreportengine <br/>. createrunandrendertask (Design); <br/> task. setappcontext (contextmap); </P> <p> // set output options <br/> htmlrenderoption Options = new htmlrenderoption (); </P> <p>/options. setoutputformat (htmlrenderoption. output_form At_pdf); <br/> // options. setoutputstream (resp. getoutputstream (); <br/> string savepath = "D:/Works/worktools/Apache-Tomcat-6.0.16/webapps/reportoutputpath"; <br/>/options. setoutputfilename (".. /output.html "); <br/> string filename = savepath + file. separator + "output.html"; <br/> system. out. println ("filename is:" + filename); <br/> outputstream fout = new fileoutputstream (new file (filename); <br/> Options. setoutputstream (fout); <br/> options. setoutputformat (htmlrenderoption. output_format_html); <br/> // options. setoutputformat ("html"); <br/> options. setimagedirectory ("/report/images"); <br/> options. setenableagentstyleengine (true); <br/> options. setembeddable (true); <br/> task. setrenderoption (options); </P> <p> // Run Report <br/> task. run (); <br/> servletoutputstream out = resp. getoutputstream (); <br/> Out. println ("done OK !!!! "); <Br/> out. close (); <br/> fout. close (); <br/> task. close (); <br/>}catch (exception e) {</P> <p> E. printstacktrace (); <br/> throw new servletexception (E); <br/>}< br/>}

2.2.4: configure the servlet and call it. For exampleHttp: // localhost: 8080/report/run? ReportName = testWebReport. rptdesign

<Servlet> <br/> <servlet-name> CreateWebReport </servlet-name> <br/> <servlet-class> hob. integration. birt. webReport </servlet-class> <br/> </servlet> </p> <servlet-mapping> <br/> <servlet-name> CreateWebReport </servlet -name> <br/> <url-pattern>/creatCharReport </url-pattern> <br/> </servlet-mapping>

 

Refer:

1: Birt runtime package download path http://download.eclipse.org/birt/downloads/

2: The Birt chart shows abnormal http://www.birthome.cn/read.php? Tid-2160.html

3: Birt deployment by Jason Weathersby 07/26/2006 http://onjava.com/pub/a/onjava/2006/07/26/deploying-birt.html? Page = 2

4: Birt Chinese manual http://download.csdn.net/source/3318430

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.