# Using JSPs and Servlets to render Charts and set Preferences

- Configuring Eclipse to launch a Tomcat server
- Creating a Dynamic Web Project
- Developing your own chart pages via JSPs and Servlets

### Configuring Eclipse to launch a Tomcat server

The following components are required to run Tomcat from within Eclipse

Component | Location in the local environment
BIRT 2.2, Report Designer Full Eclipse Install (includes Eclipse 3.3, BIRT 2.2 and the required dependent plugins) | C:\Eclipse_3.3
Sun Java JDK1.5.0_06 | C:\jdk1.5.0_06
Tomcat 5.5.x | C:\Tomcat

Configuring Tomcat in the Eclipse Servers view

1. Launch Eclipse.
2. Open the J2EE Perspective, then the Servers view.
3. Right-click from within the Servers view to bring up the context menu, then select New > Server.
4. Select the application server you have installed, in this example, Apache Tomcat v5.5 Server, then click Next.
5. Browse to the directory where you installed the Apache Tomcat v5.5 Server, and make sure the Installed JRE is at least Java version 5 (Tomcat 5.5.x requires this.). Then click Finish.

Starting Tomcat from Eclipse

1. Go to the Servers view and bring up the context menu from the server you just created.
2. Select Start.
3. The status in the Servers view should now say Started. Refer to the Console view if any problems occur.

TOP

### Creating a Dynamic Web Project

1. Select the context menu from within the Project Explorer view and select New > Project. Select Web > Dynamic Web Project, then click Next.

2. The Dynamic Web Project wizard appears. Give the project a name, for example, BirtChartExample, and make sure the target runtime is the Tomcat server you just created. Click Next.

3. On the Project Facets page, select the BIRT Reporting Runtime Component checkbox. This adds the BIRT runtime libraries to the dynamic web application. Also make sure the Dynamic Web Module, and the Java project facet is checked. Click Next. On the Web Module page click Finish.

4. Now the JSPs, servlet, Java files and some additional BIRT libraries need to be imported into the project.
   These additional libraries need to be added to the web application and are available from the BIRT All-in-One Report Designer download or the standalone Chart Engine download:

> - org.eclipse.birt.chart.device.extension_2.2.0vXXXXXXXX-XXXX.jar
> - org.eclipse.birt.chart.device.svg_2.2.0vXXXXXXXX-XXXX.jar
> - org.eclipse.birt.chart.engine.extension_2.2.0vXXXXXXXX-XXXX.jar

5. To add these libraries, expand the WebContent folder under the web application just created, and select the lib folder under the WEB-INF folder. Right-click the lib folder and select Import..., then General > File System. Click Next.

   Browse to the eclipse/plugins directory where the jar files listed above have been unpacked to. Select these jar files and import them into the web application's WebContent/WEB-INF/lib folder. Click Finish. (Note: A bug in the Project Explorer view prevents the files in the lib folder from being displayed. To double-check you imported them, switch to the Navigator view.)

Importing the JSPs, servlet and Java files

1. From the Project Explorer view, select the WebContent folder at the top level of the web application and right-click it to import the JSPs. Import the JSPs to the WebContent folder from the file system as you did above. Alternately, you can copy the JSPs, chartImage.jsp, control.jsp, and UserPreference.jsp, to the WebContent folder.

2. From the Project Explorer view select the Java Resources: src folder, bring up the context menu and select New > Package. Create a package called org.eclipse.birt.chart.examples.api.preference. Now import or copy into this package these Java source files;

   - PreferenceServlet.java
   - ChartModels.java
   - LabelStyleProcessor.java

   from the org.eclipse.birt.chart.examples/src/org/eclipse/birt/chart/examples/api/preference folder.

Edit the web.xml

Edit the web.xml file under the WebContent/WEB-INF folder to add the servlet mapping. This text should be added after the <-welcome-file-list> and before the closing <web-app> tag.

> <servlet>
> <servlet-name>PreferenceServlet</servlet-name>
> <servlet-class>org.eclipse.birt.chart.examples.api.preference.PreferenceServlet</servlet-class>
> </servlet>
> <servlet-mapping>
> <servlet-name>PreferenceServlet</servlet-name>
> <url-pattern>/PreferenceServlet</url-pattern>
> </servlet-mapping>

Running the JSPs

1. From the Project Explorer view select the control.jsp file, and select Run As > Run On Server from the context menu.
2. In the Define a New Server page, select Choose an existing server, ensuring that the Tomcat server you set up earlier is selected. Check the Set server as project default checkbox, then click Next.
3. In the Add and Remove Projects page, the Web project you are working with should be in the Configured Projects side. If it is not, add it to this side. Click Next. Do not check the Update context root for your Web module and click Finish. The page should appear, allowing for selection of font, size, style and color of the bar graph which appears when the Submit button is pushed.

TOP

### Developing your own chart pages via JSPs and Servlets

To generate a dynamic, revisable chart in your web pages, the required components include a JSP page, a Servlet and a chart model.

JSPs

The JSP pages allow the user to set the characteristics of the rendered chart by passing parameters to the PreferenceServlet.

1. Set the action for the form to call the Servlet.

> <form action="PreferenceServlet" method="POST">

2. Assign values to the selection or input components

> <option value="value">XXX</option>

Servlet

1. Set the rendering device in the constructor

> PluginSettings ps = PluginSettings.instance( );
> IDeviceRenderer idr = ps.getDevice("dv.SWING" );

2. The doGet() method

- Create a chart model
- Modify the chart model according to the values from JSP
- Set the output content type, in this case to a JPEG
- Output the chart image

3. Chart image rendering

- Create a buffered image
- Build, then render a chart in the buffered image
- Output the chart image

Chart Model

The ChartModel.java class is the same class used in the other chart examples plugins.

TOP