7. Right-click in the source and choose Fix Imports from the pop-up menu. In the Fix Imports
dialog box, verify that java.io.InputStream appears in the Fully Qualified Name field for the InputStream class, and that java.sql.ResultSet appears in the Fully Qualified Name field for the ResultSet class. 8. Click OK.
9. Close and save the file.
Creating the Database Query
In this application, you use the same database query result set for the trip information to display on the web page and in the generated report. You use the Query Editor to design the database query, as shown in the following figure.
Figure 7: Query Editor Settings for the Trip Result Set
1. In the editing area, click the Page1 tab to view the page in the Visual Designer. 2. In the Servers Window, expand Data Sources, expand Travel, and expand Tables.
3. If a red X appears on Travel node's icon and an error appears when you try to expand it, the
database server is not running. To start the database server, right-click the Bundled Database
Server node in the Server Navigator and choose Start Bundled Database from the contextual menu. Then right-click the Travel node and choose Refresh. 4. Drag the TRIP node and drop it on a blank spot on the page.
5. In the Outline window, expand SessionBean1 and double-click tripRowSet to open the Query
Editor. 6. In the TRIP table in the top panel, clear the checkboxes for PERSONID and TRIPTYPEID. 7. Drag the PERSON node from the Servers window and drop it next to the TRIP table in the top
panel. 8. In the PERSON table, clear the checkboxes for PERSONID, JOBTITLE, and FREQUENTFLYER. 9. Drag the TRIPTYPE node from the Servers window and drop it next to the PERSON table in the
top panel. 10. In the TRIPTYPE table, clear the checkboxes for TRIPTYPEID and NAME.
11. In the NAME row for the TRAVEL.PERSON table in the grid panel, choose Ascending from the
drop-down list in the Sort Type column. 12. The IDE sets the Sort Order to 1.
13. In the DEPDATE row for the TRAVEL.TRIP table in the grid panel, choose Ascending from the
drop-down list in the Sort Type column. 14. The IDE sets the Sort Order to 2.
15. Close the Query Editor for the tripRowSet.
Creating the Trip Report Page
In this section you add a table to Page1 to display the trip data, and you add buttons to generate the report in HTML and PDF formats, as shown in the following figure.
Figure 8: Page1
1. Display Page1 in the Visual Designer.
2. Drag the Table component from the Palette and drop it on the page. 3. In the Properties window, set the title property to Trips.
4. Right-click the Table component and choose Table Layout from the pop-up menu.
5. In the Table Layout dialog box, choose tripDataProvider from the Get Data From drop-down list. 6. Select PERSON.NAME in the Selected list and click the Up button four times to move
PERSON.NAME to the top of the list. 7. Click OK to close the dialog box.
8. Drop a Button component above the Table component, type View Report, and press Enter. 9. In the Properties window, set the id property to viewReportBtn.
10. Click the ellipsis (...) button for the action property, choose viewReportBtn_action from the
drop-down list, and click OK. 11. Drop a Button component next to the View Report button, type Create PDF, and press Enter. 12. In the Properties window, set the id property to createPdfBtn.
13. Click the ellipsis (...) button for the action property, choose createPdfBtn_action from the
drop-down list, and click OK. 14. Double-click the View Report button to display the Java source for the viewReportBtn_action
method. 15. Add the following code shown in bold to the body of the viewReportBtn_action method. Code Sample 5: viewReportBtn_action Method
public String viewReportBtn_action() { // Free up the rowset resources tripDataProvider.close();
Map fillParams = new HashMap(); try {
fillParams.put (\
getExternalContext().getResource(\ getApplicationBean1().jasperReport (\
getSessionBean1().getTripRowSet(), fillParams); } catch (Exception e) {
log(\ error(\ }
return null; }
16. Scroll to the createPdfBtn_action method and add the following code shown in bold. Code Sample 6: createPdfBtn_action Method public String pdfButton_action() { // Free up the rowset resources tripDataProvider.close();
Map fillParams = new HashMap(); try {
fillParams.put (\
getExternalContext().getResource(\ getApplicationBean1().jasperReport
(\
getSessionBean1().getTripRowSet(), fillParams); } catch (Exception e) {
log(\ error(\ }
return null; }
10. Right-click in the source and choose Fix Imports from the pop-up menu.
11. In the main toolbar, click Run Main Project to build, deploy, and run the web application. 12. In the web application, click View Report to view the report in HTML format.
13. If the report does not appear, right-click the Deployment Server node in the Servers window,
and choose View Server Log from the pop-up menu. Search the log output for errors. If you see the following error message, verify that you correctly set the codeBase in the entry that you added to the server.policy file. 14. java.security.AccessControlException: access denied 15. (java.lang.RuntimePermission createClassLoader)
16. Click the Back button and click Create PDF to view the report in PDF format.
Summary
The major steps for using the JasperReports framework from the Sun Java Studio Creator IDE are as follows:
1. Download and unzip the JasperReports framework from http://www.jasperforge.org. 2. Use the Library Manager to create a JasperReports library. This makes it easy to add the
necessary JasperReports JAR files to a web application. 3. To add the JasperReports library to a web application, right-click the Library node in the Projects
window, and choose Add Library. 4. Use the Extensions and MIME Types property for XML Objects in the Options dialog box to add
jrxml to the list of XML objects. By doing so, the IDE will open jrxml files in the XML editor. 5. Add a mapping to net.sf.jasperreports.j2ee.servlets.ImageServlet in the web.xml file. 6. Add logic to the application bean for generating the reports.
7. After creating the report, call facesContext.responseComplete( ) to terminate the current
request lifecycle. 8. If you are generating an HTML report, use the IMAGES_URI parameter to pass the URI for the
ImageServlet class. For example, if you set the URI to /image, set the IMAGES_URI parameter to \
9. You can use a cached rowset, such as the cached rowset that the IDE creates when you add a
dataprovider to a page, as a datasource for filling the report. Be sure to call the dataprovider's close method before filling the report.