
Java API for an application
JReport provides 3 ways for the application to call JReport directly without using JSP pages and URLs.
- JReport Client API: JReport provides a library of methods that allow a thick client application (not using a browser) to login to JReport in either a non-GUI or a GUI mode.
- JReport Server API: JReport provides a library of methods to directly start JReport Server as part of the customers application running in the same JVM as the application.
- JReport Remote Server API: JReport provides methods to connect to an already running instance of JReport Server either running on the local machine in a different JVM or running on an entirely different server. Other than the first call to connect either using RMI or directly in the current JVM, the rest of the API is identical so it is easy to write an application that can work either remotely or locally the same as the JSP pages that JReport provides.
How to write client code using the Client API
If you don't want JReport to pop-up a login request you need to provide a user ID and password that is valid in the JReport Server security context. This could be authenticated many different ways, such as the built in user/password in JReport Server, directly to LDAP or using SSO to call your application.
You then acquire the parameters as needed and build a hashtable of the required parameters and call the JReport Client API to run and view the report. In the code sample below the argument for argRpt is something like "http://localhost:8888/jrserver/SampleReports/SampleReports.cat/CustomerAnalysis.cls"
.
String user = "admin";
String password = "admin";
String report = argRpt;
// set the user ID for the client.
JRClient.setUser(user);
// set the password for the client.
JRClient.setPassword(password);
// try to login into the server.
JRClient.login(null, report);
// Bring up a parameter input dialog box when needed.
Hashtable htParams = JRClient.getParamValues(null, report);
System.out.println("............Parameters values= " + htParams);
ht.putAll(htParams);
//Send "Run & View" report command with the hashtable of parameter values.
tempResult = JRClient.sendCmdRunAndViewEx(null, report, ht);
|
How to write client code using the Server API
If you want the entire JReport Server to run in the context of your application in the same JVM you can use the Server API. The Server when started this way starts a number of threads and runs exactly the same as when the server is started as a standalone server. The server just starts with the application and stops with the application. All access to the server such as RMI access from other applications is still allowed.
//set report home
System.getProperties().put("reporthome", "C:\\JReport\\Server");
// Creates instance of RptServer ( this does nothing if JReport Server is already running in this JVM)
HttpUtil.initEnv(System.getProperties());
// Get a handle to the server instance
RptServer server = HttpUtil.getHttpRptServer();
|
How to write client code using the Remote Server API
It is exactly the same code as above except you get a remote handle to the RPTServer.
//set report home for logging and location of rmi.auth file for security
System.getProperties().put("reporthome", "C:\\JReport\\Server");
System.getProperties().put("jrs.rmi.auth_file", "C:\\JReport\\Server\\bin\\rmi.auth");
//search the running server using default host and port
String host = "localhost";
String port = "1129";
RptServer server = RemoteReportServerToolkit.getRemoteWrappedRptServer(host, port);
|
