## Best practices

You can do several things to improve the performance of web applications that make use of the viewer to display reports. These techniques range from optimization of the reports themselves to coding practices that improve the efficiency of your applications.

Click the appropriate link to jump to that section:

- Report design
- Stateless viewing
- Caching report sources
- Viewer only pages
- Using the setOwnForm method
- Specifying the correct character set

### Report design

You can view potential performance bottlenecks in your report design by going to the Report menu in the Crystal Reports Designer and clicking Performance Information. The Crystal Reports Online Help contains a chapter with a detailed overview on how to create efficient reports titled "Designing Optimized Web Reports." Following the recommendations listed there helps you improve your report viewing efficiency considerably.

### Stateless viewing

Beginning with version 9, the viewer can work effectively without being stored in a session variable on the web application server, providing stateless viewing. Stateless viewing only works, however, for reports that are read-only. If the user needs to modify the report using a Report Application Server or refresh the report's data source, stateless viewing cannot be used, and the viewer and report source instances must be stored in a session variable.

### Caching report sources

Caching a report source in the session variable allows it to be used multiple times efficiently. When a report source is not cached, the process of creating a new report source multiple times becomes fairly expensive. Furthermore, caching a report source allows reports with or without saved data to be refreshed.

The following example shows how to cache a report source in the session variable:

`String report = "/reports/sample.rpt";`

`JPEReportSource reportSource;`

`reportSource = new JPEReportSource(report);`

`session.putValue("reportSource", reportSource);`

Note: If you are using a cached report source, the dispose methods for the viewer or report source should not be called until the report source is no longer being used.

### Viewer only pages

If your JSP page contains only the viewer and nothing else, several things can be done ways to simplify the report viewing implementation.

#### Setting the setOwnPage property

The viewer is capable of generating complete HTML pages and can set the appropriate page properties depending on the viewing context. Setting the setOwnPage property to true provides several benefits, by allowing the viewer to completely handle the surrounding HTML content. Allowing the viewer to handle the surrounding HTML content reduces the amount of code you need to add to your JSP page and allows the viewer to automatically determine certain settings:

- It allows the viewer to choose which page start and end tags to use based on what device is being used to view the page.

  For example, it writes out the `<html>` start tag for web browsers and `<wml>` start tag for mobile devices.

- It correctly sets the content-type and charset information for the page. This ensures that pages containing international characters is displayed correctly.

If setOwnPage is false, the surrounding HTML tags and content-type and charset directives need to be manually set.

#### Using the processHttpRequest method

When setOwnPage is set to true, you must use the processHttpRequest method to display the report instead of getHtmlContent. The processHttpRequest method must be used because using getHtmlContent has the same effect as setting setOwnPage to false, negating any of the benefits gained from setting setOwnPage to true.

### Using the setOwnForm method

If your JSP page does not contain any controls that require post back, you should set the setOwnForm method to true. Doing so, allows the viewer to handle the view state information automatically. The view state is used to perform client-side caching of information about the current state of the report. If you have other controls on the page, you must setOwnForm is set to false and handle the view state information manually.

The following example shows how to set the view state information manually:

`viewer.setOwnForm(false);`

`viewer.setViewState((String) session.getValue("viewState"));`

`viewer.processHttpRequest(request, response, getServletContext(), pageContext.getOut());`

`session.putValue("viewState", viewer.getViewState());`

### Specifying the correct character set

To send characters from a JSP file to a web browser, you must use the correct encoding. Always specify the correct content-type and character set for all of your JSP pages.

If your JSP page returns content to a standard HTML browser, ensure that the correct character set is defined:

`<%@ page contentType="text/html; charset=utf-8" %>`

The contentType and charset directives let the browser know how the returned HTML page is encoded. UTF-8 is the recommended standard character set if it is available for your target client browser. For more information, consult the Release Notes or the vendor of your target client browser.

Crystal Decisions http://www.crystaldecisions.com/ Support services http://support.crystaldecisions.com/