Birt servlet Extension

Source: Internet
Author: User

 

Since you do not want to use the Birt default viewer, refer to the official example to use servlet extension!

The birtengine code is as follows:

 

Package COM. yjga. birt; import Java. util. logging. level; import javax. servlet. servletcontext; import Org. eclipse. birt. core. exception. birtexception; import Org. eclipse. birt. core. framework. iplatformcontext; import Org. eclipse. birt. core. framework. platform; import Org. eclipse. birt. core. framework. platformservletcontext; import Org. eclipse. birt. report. engine. API. engineconfig; import Org. eclipse. birt. report. engine. API. htmlactionhandler; import Org. eclipse. birt. report. engine. API. htmlemitterconfig; import Org. eclipse. birt. report. engine. API. htmlserverimagehandler; import Org. eclipse. birt. report. engine. API. ireportengine; import Org. eclipse. birt. report. engine. API. response; public class birtengine {Private Static ireportengine birtengine = NULL; public static response void initbirtconfig () {} public static response ireportengine getbirtengine (servletcontext SC) {engineconfig Config = new engineconfig (); config. setlogconfig (SC. getrealpath ("/logs"), level. fine); config. setenginehome (""); iplatformcontext context = new platformservletcontext (SC); config. setplatformcontext (context); // register new image handler solves the path problem when reading images. htmlemitterconfig emitterconfig = new htmlemitterconfig (); emitterconfig. setactionhandler (New htmlactionhandler (); htmlserverimagehandler imagehandler = new htmlserverimagehandler (); emitterconfig. setimagehandler (imagehandler); config. getemitterconfigs (). put ("html", emitterconfig); //-NLS-1 $ try {platform. startup (config);} catch (birtexception e) {e. printstacktrace ();} ireportenginefactory factory = (ireportenginefactory) platform. createfactoryobject (ireportenginefactory. extension_report_engine_factory); birtengine = factory. createreportengine (config); Return birtengine;} public static synchronized void destroybirtengine () {If (birtengine = NULL) {return;} birtengine. shutdown (); platform. shutdown (); birtengine = NULL;} public object clone () throws clonenotsupportedexception {Throw new clonenotsupportedexception ();}}

The ajzsservlet code is as follows:

Package COM. yjga. birt; import Java. io. ioexception; import Java. io. printwriter; import Java. util. arraylist; import Java. util. collection; import Java. util. date; import Java. util. hashmap; import Java. util. iterator; import Java. util. list; import Java. util. logging. logger; import javax. servlet. servletcontext; import javax. servlet. servletexception; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. HTTPS Ervletrequest; import javax. servlet. HTTP. httpservletresponse; import Org. eclipse. birt. report. engine. API. engineconstants; import Org. eclipse. birt. report. engine. API. htmlrendercontext; import Org. eclipse. birt. report. engine. API. htmlrenderoption; import Org. eclipse. birt. report. engine. API. igetparameterdefinitiontask; import Org. eclipse. birt. report. engine. API. iparameterdefnbase; import Org. eclipse. birt. report. E Ngine. API. ireportengine; import Org. eclipse. birt. report. engine. API. ireportrunnable; import Org. eclipse. birt. report. engine. API. irunandrendertask; import Org. eclipse. birt. report. engine. API. iscalarparameterdefn; import COM. yjga. util. dateutil; public class ajzsservlet extends httpservlet {Private Static final long serialversionuid = 1l; private ireportengine birtreportengine = NULL; protected static logger L Ogger = logger. getlogger ("org. eclipse. birt "); Public ajzsservlet () {super ();}/*** destruction of the servlet. */Public void destroy () {super. destroy (); birtengine. destroybirtengine ();}/*** the doget method of the servlet. **/Public void doget (httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {hashmap parmdetails = new hashmap (); // get report name and launch Engineresp. setcontenttype ("text/html"); // resp. setcontenttype ("application/pdf"); // resp. setheader ("content-disposition", "inline; filename=testposition"); // obtain the parameter and add it to the paramslist set string reportname = req. getparameter ("reportname"); string Bm = new string (req. getparameter ("BM "). getbytes ("iso8859_1"), "gb2312"); string sj1 = req. getparameter ("sj1"); string sj2 = req. getparameter ("sj2"); List paramslist = new Arraylist (); If (BM! = NULL &&! BM. Equals ("")&&! BM. equals ("null") paramslist. add (BM); paramslist. add (sj1); paramslist. add (sj2); servletcontext SC = req. getsession (). getservletcontext (); this. birtreportengine = birtengine. getbirtengine (SC); // setup image directoryhtmlrendercontext rendercontext = new htmlrendercontext (); rendercontext. setbaseimageurl (req. getcontextpath () + "/report/images"); rendercontext. setimagedirectory (SC. getrealpath ("/report/Image S "); // logger. log (level. fine, "image directory" // + SC. getrealpath ("/report/images"); // system. out. println (rendercontext. getbaseimageurl (); // system. out. println (rendercontext. getimagedirectory (); hashmap <string, htmlrendercontext> contextmap = new hashmap <string, htmlrendercontext> (); contextmap. put (engineconstants. appcontext_html_render_context, rendercontext); ireportrunnable design; try {// open re Port designdesign = birtreportengine. openreportdesign (SC. getrealpath ("/Report") + "/" + reportname); // the parameters in the report are matched with the passed parameters and placed in the parmdetails set igetparameterdefinitiontask taskparam = birtreportengine. creategetparameterdefinitiontask (Design); Collection Params = taskparam. getparameterdefns (true); iterator iter = Params. iterator (); iterator iterlist = paramslist. iterator (); // iterate over all Parameter Swhile (ITER. hasnext () & iterlist. hasnext () {iparameterdefnbase Param = (iparameterdefnbase) ITER. next (); // parameters are not in a groupiscalarparameterdefn scalar = (iscalarparameterdefn) Param; If (scalar. getname (). equals ("BM") {string paramlist = (string) iterlist. next (); parmdetails. put (scalar. getname (), paramlist); // system. out. println (scalar. getname () + "" + paramlist);} else {date paramlist = New date (); try {paramlist = dateutil. parse (string) iterlist. next (), "yyyy-m-d");} catch (exception e) {e. printstacktrace ();} parmdetails. put (scalar. getname (), paramlist); // system. out. println (scalar. getname () + "" + paramlist);} taskparam. close (); // Create task to run and render reportirunandrendertask task = birtreportengine. createrunandrendertask (Design); task. setappcontext (contextmap); task. setpara Metervalues (parmdetails); // set output optionshtmlrenderoption Options = new htmlrenderoption (); options. setoutputformat (htmlrenderoption. output_format_html); // options. setoutputformat (htmlrenderoption. output_format_pdf); options. setoutputstream (resp. getoutputstream (); task. setrenderoption (options); // run reporttask. run (); task. close ();} catch (exception e) {e. printstacktrace (); throw new servletexcep Tion (e) ;}/ *** the dopost method of the servlet. **/Public void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html"); printwriter out = response. getwriter (); out. println ("<! Doctype HTML public \ "-// W3C // dtd html 4.01 transitional // en \"> "); out. println ("<HTML>"); out. println ("

The web layer only needs to pass the parameters to the servlet. However, this type of extension is quite general and the code is messy. Further improvements are needed!

 

 

 

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.