http://wiki.eclipse.org/Java_-_Dynamic_Report_Servlet_(BIRT)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Html代码 <html> <head> </head> <body> The Query that is used for this example is Select * From OFFICES <form id="form1" method="get" action="http://localhost:8080/DynamicReport/run"> <select name="dyna1" multiple size="8" > <option value="COUNTRY">COUNTRY</option> <option value="CITY">CITY</option> <option value="STATE">STATE</option> <option value="OFFICECODE">OFFICECODE</option> <option value="ADDRESSLINE1">ADDRESSLINE1</option> <option value="ADDRESSLINE2">ADDRESSLINE2</option> <option value="POSTALCODE">POSTALCODE</option> <option value="TERRITORY">TERRITORY</option> </select> <INPUT TYPE="SUBMIT" VALUE="Run Dynamic Report"> <INPUT TYPE="HIDDEN" NAME="ReportName" Value="DynamicTableExample.rpttemplate"> </form> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
Java代码 import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.eclipse.birt.report.engine.api.EngineConstants; import org.eclipse.birt.report.engine.api.HTMLRenderOption; import org.eclipse.birt.report.engine.api.IReportRunnable; import org.eclipse.birt.report.engine.api.IRunAndRenderTask; import org.eclipse.birt.report.engine.api.IReportEngine; import org.eclipse.birt.report.model.api.CellHandle; import org.eclipse.birt.report.model.api.DataItemHandle; import org.eclipse.birt.report.model.api.ElementFactory; import org.eclipse.birt.report.model.api.LabelHandle; import org.eclipse.birt.report.model.api.OdaDataSetHandle; import org.eclipse.birt.report.model.api.OdaDataSourceHandle; import org.eclipse.birt.report.model.api.PropertyHandle; import org.eclipse.birt.report.model.api.ReportDesignHandle; import org.eclipse.birt.report.model.api.RowHandle; import org.eclipse.birt.report.model.api.StructureFactory; import org.eclipse.birt.report.model.api.TableHandle; import org.eclipse.birt.report.model.api.activity.SemanticException; import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn; import org.eclipse.birt.report.engine.api.HTMLServerImageHandler; public class DynamicReport extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; /** * Constructor of the object. */ private IReportEngine birtReportEngine = null; protected static Logger logger = Logger.getLogger( "org.eclipse.birt" ); public DynamicReport() { 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 { //get report name and launch the engine resp.setContentType("text/html"); //resp.setContentType( "application/pdf" ); //resp.setHeader ("Content-Disposition","inline; filename=test.pdf"); String reportName = req.getParameter("ReportName"); String[] cols = (String[])req.getParameterMap().get("dyna1"); ServletContext sc = req.getSession().getServletContext(); this.birtReportEngine = BirtEngine.getBirtEngine(sc); IReportRunnable design; try { //Open report design design = birtReportEngine.openReportDesign( sc.getRealPath("/Reports")+"/"+reportName ); ReportDesignHandle report = (ReportDesignHandle) design.getDesignHandle( ); buildReport( cols, report ); //create task to run and render report IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask( design ); task.setAppContext( contextMap ); //set output options HTMLRenderOption options = new HTMLRenderOption(); options.setImageHandler( new HTMLServerImageHandler() ); options.setImageDirectory( sc.getRealPath("/images"); options.setBaseImageURL( req.getContextPath() + "/images" ); options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML); //options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF); options.setOutputStream(resp.getOutputStream()); task.setRenderOption(options); //run report task.run(); task.close(); }catch (Exception e){ e.printStackTrace(); throw new ServletException( e ); } } public void buildReport(String[] cols, ReportDesignHandle designHandle){ try{ ElementFactory designFactory = designHandle.getElementFactory( ); buildDataSource(designFactory, designHandle); //ArrayList cols = new ArrayList(); //cols.add("OFFICECODE"); //cols.add("CITY"); //cols.add("COUNTRY"); buildDataSet(cols, "From Offices", designFactory, designHandle); TableHandle table = designFactory.newTableItem( "table", cols.length ); table.setWidth( "100%" ); table.setDataSet( designHandle.findDataSet( "ds" ) ); PropertyHandle computedSet = table.getColumnBindings( ); ComputedColumn cs1 = null; for( int i=0; i < cols.length; i++){ cs1 = StructureFactory.createComputedColumn(); cs1.setName((String)cols[i]); cs1.setExpression("dataSetRow[\"" + (String)cols[i] + "\"]"); computedSet.addItem(cs1); } // table header RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 ); for( int i=0; i < cols.length; i++){ LabelHandle label1 = designFactory.newLabel( (String)cols[i] ); label1.setText((String)cols[i]); CellHandle cell = (CellHandle) tableheader.getCells( ).get( i ); cell.getContent( ).add( label1 ); } // table detail RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 ); for( int i=0; i < cols.length; i++){ CellHandle cell = (CellHandle) tabledetail.getCells( ).get( i ); DataItemHandle data = designFactory.newDataItem( "data_"+(String)cols[i] ); data.setResultSetColumn( (String)cols[i]); cell.getContent( ).add( data ); } designHandle.getBody( ).add( table ); }catch(Exception e){ e.printStackTrace(); } } void buildDataSource( ElementFactory designFactory, ReportDesignHandle designHandle ) throws SemanticException { OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource( "Data Source", "org.eclipse.birt.report.data.oda.jdbc" ); dsHandle.setProperty( "odaDriverClass", "org.eclipse.birt.report.data.oda.sampledb.Driver" ); dsHandle.setProperty( "odaURL", "jdbc:classicmodels:sampledb" ); dsHandle.setProperty( "odaUser", "ClassicModels" ); dsHandle.setProperty( "odaPassword", "" ); designHandle.getDataSources( ).add( dsHandle ); } void buildDataSet(String[] cols, String fromClause, ElementFactory designFactory, ReportDesignHandle designHandle ) throws SemanticException { OdaDataSetHandle dsHandle = designFactory.newOdaDataSet( "ds", "org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" ); dsHandle.setDataSource( "Data Source" ); String qry = "Select "; for( int i=0; i < cols.length; i++){ qry += " " + cols[i]; if( i != (cols.length -1) ){ qry += ","; } } qry += " " + fromClause; dsHandle.setQueryText( qry ); designHandle.getDataSets( ).add( dsHandle ); } /** * 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(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.println(" Post Not Supported"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * Initialization of the servlet. * * @throws ServletException if an error occure */ public void init() throws ServletException { BirtEngine.initBirtConfig(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Xml代码 <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <display-name>DynamicReport</display-name> <servlet> <servlet-name>DynamicReport</servlet-name> <servlet-class>DynamicReport</servlet-class> </servlet> <servlet-mapping> <servlet-name>DynamicReport</servlet-name> <url-pattern>/run</url-pattern> </servlet-mapping> </web-app> |