My system: JSP STRTUS EJB DAO DB
My Idea: In the Action (Ro Dispatchction), the parameters needed to report the report, then pass to the unified display servlet
My implementation
1. Show servlet
2. Parameter Class
3.Action
1. Show servlets (HTML here)
Public class jrhtmlservlet extends httpservlet {
/ **
*
* /
Public void service
HTTPSERVLETREQUEST REQUEST,
HTTPSERVLETRESPONSE RESPONSE
"Throws oException, servletexception {
// Get the required parameters
JRPARAMETERFACE JREPORTPARAM = NULL;
JreportParam = (JRPARAMETERFACE) Request.GetaTribute ("jreportparam");
Response.setContentType ("text / html; charSet = GB2312");
PrintWriter out = response.getwriter ();
Try
{
JasperReport JasperReport = (JasperReport) JRLOADER.LOADOBJECT (JREPORTPARAM.GETJASPERFILE (). Getpath ());
JasperPrint JasperPrint =
JasperfillManager.FillReport
JasperReport,
Jreportparam.getParameters (),
Jreportparam.getdataSet ()
);
JRHTMLEXPORTER EXPORTER = New JRHTMLEXPORTER ();
Map imagesmap = new hashmap ();
Request.getations (). SetaTribute ("iMages_map", ImageSmap;
Exporter.SetParameter (JrexporterParameter.jasper_Print, Jasperprint);
Exporter.SetParameter (JrexporterParameter.output_Writer, Out);
Exporter.SetParameter (JRHTMLEXPORTERPARETER.IMAGES_MAP, IMAGESMAP);
Exporter.SetParameter (JRHTMLEXPORTERPARETER.IMAGES_URI, "JRREPORT / IMAGE? Image =");
Exporter.exportReport ();
}
Catch (JRException E)
{
Out.println ("");
Out.println ("
");Out.println ("
// out.println (" "); OUT. Println (" HEAD>");
Out.println ("
Out.println (" jasperreports encountered this error: span>");
OUT.PRINTLN ("
");E.PrintStackTrace (OUT);
OUT.PRINTLN (" pre>");
Out.println (" Body>");
Out.println (" html>");
}
}
}
2. Parameter Class
Public interface jrparameterinterinterface {
// set required parameters
Void setParameters (Object Object, Object Object1);
/ / Return the required file
File getjasperfile ();
/ / Back to page of Parameter
Map getparameters ();
/ / Return to the page of the page, if you are empty, return (New JREMPTYDATASOURCE ()), otherwise return (New WebAppdataSource (DATA))
JRDataSource getDataSet ();
}
Implementation
Public Class JreportParam IMPLEments JRPARETERFACE {
Private Dataset JRDATA;
PRIVATE JASPERFILE;
Private JrdataSource JrdataSource;
Private map parametermap;
Public JreportParam (File Jasperfile, Dataset Data) {
THIS.JASPERFILE = JASPERFILE;
JRDATA = DATA;
Parametermap = new hashmap ();
}
Public JreportParam (File Jasperfile) {
THIS (Jasperfile, (Dataset) NULL;
}
Public JreportParam (File Jasperfile, JRDataSource Jrdata) {
THIS.JASPERFILE = JASPERFILE;
JRDataSource = JRDATA;
Parametermap = new hashmap ();
}
// set required parameters
Public void setParameters (Object Object, Object Object1) {
ParameterMap.Put (Object, Object1);
}
/ / Return the required file
Public file getjasperfile () {
Return jasperfile;
}
/ / Back to page of Parameter
Public map getParameters () {
Return ParameterMap;
}
/ / Return to the page of the page, if you are empty, return (New jremptyDataSource ()), otherwise return (New WebAppDataSource (DATA)) public jrdataser getDataSet () {
IF (JRDataSource == NULL) {
IF ((JRDATA == Null) || (JRDATA.GETRETCODE (). Equals ("NO_DATA"))) {
JRDataSource = New jremptyDataSource ();
} else {
JRDataSource = New WebAppdataSource (JRDATA);
}
}
Return JRDataSource;
}
}
3.Action
/ / Need to get an absolute path PATH
File ReportFile = New File (Path "Demo.jasper");
JreportParam JreportParam = New JreportParam (Reportfile, Dataset);
JreportParam.SetParameters ("ReportTitle", "My Demo");
HttpservletRequest.setttribute ("JreportParam", JreportParam);
ActionForward = ActionMapping.FindForward (ReportType);