#### Deploying to WebSphere 5.1 on Windows/Linux

This demo shows how JReport Engine Bean can be deployed as an EJB to WebSphere 5.1 for both windows 2000 professional and Linux. The steps below are based on Windows. The procedure for Linux is similar, where you only need to change the path information.

In this demo, an EJB client is HttpServerlet which calls EJB module deployed in the same WebSphere 5.1. In the EJB module, the session bean calls JReport Engine Bean to run and export a report to the HTML format. The below image illustrates the working structure of this demo.

JReport Engine Bean is the module running report templates against a data source and exporting report results to the specified format such as HTML, PDF, RTF and so on (see Exporting report results to different file formats for methods about exporting report result to other formats).

The steps for this demo are as below:

1. Preparation
2. Developing the EJB session bean and the servlet that calls the EJB
3. Deploying the EAR file and starting the servlet

##### Preparation

To prepare for this demo, you will need to install the following:

1. Install WebSphere 5.1 to `C:\websphere5.1`.
2. Install WebSphere Studio Application Developer 5.1 (abbreviated as WSAD) to `C:\IBM`. In this demo, we will use WSAD to:
   - develop EJB session beans
   - develop EJB client servlet
   - generate deployment descriptors
   - generate the ear file for the later deployment
3. Install JReport Designer to `C:\JReport\Designer`. You will then have the following JReport Engine Bean and related libraries in `C:\JReport\Designer\lib`:
   - JREngine.jar - It is required.
   - log4j-1.2.8.jar - Required. It is called by JReport Engine to generate log information at runtime.
   - sac.jar - Required. It is used to parse xml files.
   - hsqldb.jar - It is necessary for this demo. This demo runs a report against HSQL database, and hsqldb.jar is the hsqldb driver.

   This demo will export the report result to the HTML format. If you want to export the report result to other formats, add the corresponding class files or jars. They are:

   - Mail: mail.jar and activation.jar
   - Excel: poiHSSF_151.jar
   - XML: xml-apis.jar and xercesImpl.jar
   - PDF: Itext.zip

Note: JReport Engine Bean in JReport Designer is of single thread. That is, you can only have it run one report at a time. To build your production EJB which calls JReport Engine Bean for concurrent multiple reports, contact Jinfonet Support about the JReport Server Engine product.

##### Developing the EJB session bean and the servlet that calls the EJB

In WSAD, first create a project that contains the framework of the EJB and the servlet which calls the EJB. Then, go to their editors to modify the code for the EJB and servlet. Follow the steps below:

1. Start WebSphere Studio 5.1. Create a new Java EE project named JReportEJBDemo.

   To create the new project JReportEJBDemo,

   1. Select Project from the File menu. The New Project dialog will then appear.
   2. Highlight Java EE on the left panel in the New Project dialog, and then choose Enterprise Application Project from the right panel. Click the Next button in this dialog. You will then be taken to the New Enterprise Application Project dialog. Then, click Next in this dialog directly.
   3. Type JReportEJBDemo into the Project name text box, and click Next.
   4. Click New Module and on the New Module Project dialog, select EJB Project and Web Project. Then, click the Finish button.
   5. You will now see that JReportEJBDemoEJB and JReportEJBDemoWeb have been generated. Click Finish.
   6. Add source files for the EAR.
      1. Right-click Utility JARs in JReportEJBDemo, and click Import from the shortcut menu. The Import dialog appears. Select File System from the Select an import source panel, and then click the Next button.
      2. Click the Browse button and select the directory `C:\JReport\Designer\lib`. Select the files JREngine.jar, log4j-1.2.8.jar, sac.jar and hsqldb.jar. For the Into folder text box, click the Browse button and select JReportEJBDemo. Then, click Finish in the Import dialog.
2. Create an EJB session bean named JReport for JReportEJBDemoEJB.
   1. From the Java EE Hierarchy tree, expand EJB Modules, right-click Session Beans in JReportEJBDemoEJB, and on the shortcut menu, click Session Bean from the sub-menu of New.
   2. In the New Enterprise Bean dialog, click Next.
   3. This dialog helps you "Create a 2.0 Enterprise Bean". Type JReport in the Bean name text box. Type demo in the Default Package text box. Click Next to go to the next dialog which shows the Enterprise Bean Details (perform no actions in this dialog except for clicking Next). Then, click Finish.
   4. Now, in the Java EE Hierarchy tree, three EJB classes have been generated in the Session Beans node. They will automatically have been named JReportHome, JReport, and JReportBean. Note that JReportHome is the EJB home class, JReport is the EJB object class and JReportBean is the EJB bean class.
3. Edit the three EJB classes.
   - JReportHome: Insert a method which is used to create the EJB object.
   - JReport: Insert methods which have been defined to call JReport Engine Bean.
   - JReportBean: Implement the methods in the JReport class (the EJB object class).
4. Check the JNDI name.

   Double-click the session bean JReport. In the right panel, find out the JNDI Name, which will be used in the servlet lookup later.

5. Put external classes/jars to the WSAD compiler.
   1. Right-click JReportEJBDemoEJB, and choose Properties from the shortcut menu.
   2. Select Java Build Path on the left panel.
   3. In the Libraries tab. Then, click the Add External JARs button to add JREngine.jar (go to `C:\JReport\Designer\lib`, rename the file JREngine.jar to JREngine.jar).
   4. In the Order and Export tab, check JREngine.jar, and move it to the top. Then, click OK.
6. Add JREngine.jar, log4j-1.2.8.jar, and hsqldb.jar to the class path which is used to run the EJB.
   1. On the Window menu, click Other from the sub-menu of Open Perspective.
   2. Select Plug-in Development in the Select Perspective dialog.
   3. In the Package Explorer, expand JREportEJBDemoEJB in META-INF of ejbModule. Then, double-click MANIFEST.MF to open it.
   4. In the Dependencies panel of the JAR Dependency Editor, select all the jar files in the JAR or Module column.
7. Use WSAD to build the container classes and deployment descriptors for the EJB.
   1. Right-click JReportEJBDemoEJB in EJB Modules, and click Deployment and RMIC Code from the Generate sub-menu. Check JReport.
   2. Click Finish in the Generate Deployment and RMIC Code dialog and the changes will be saved at the same time.
8. Export this JReportEJBDemoEJB to a jar.
   1. Right-click JReportEJBDemoEJB and select Export EJB JAR File from the Export sub-menu.
   2. In the Export dialog, specify a directory in the Destination text box. Use the default name JReportEJBDemoEJB. The JReportEJBDemoEJB.jar will be generated in the specified path.
9. The EJB, which is JReportEJBDemoEJB, is now ready. Next, you should finish the servlet code in the created web module, that is JReportEJBDemoWeb, to call this EJB via RMI. To generate the servlet code,
   1. Double-click JReportEJBDemoWeb. In the Web Deployment Descriptor, click Details of Servlets and JSPs panel.
   2. Click New to bring out the New Servlet dialog. Type JRServer in the Class name text box, and click Finish. The JRServer.java is generated.
   3. Add the following code into the JRServer.java.

          InitialContext ic = new InitialContext();
          Object objRef = ic.lookup("java:comp/env/jreport");
          JReportHome home =(JReportHome)PortableRemoteObject.narrow(objRef,
          JReportHome.class);
          this.jreport = home.create();

      Note: The above code `Object objRef = ic.lookup("java:comp/env/jreport")` calls AliasName – jreport. Therefore, we will create an alias name for the EJB in the web module.

      1. Double-click JReportEJBDemoWeb in the References panel. Then click Details.
      2. In the EJB References panel, click Add, and rename New EJB Ref to jreport.
      3. In the Details panel, click Browse to the right of the Lin text box. Then, select JReportEJBDemoEJB in the Location panel in the EJB Selection dialog, and click OK.
      4. In the WebSphere Bindings panel, check the JNDI Name, to make sure that it is the same as the name mentioned in step four.
   4. Double-click JReportEJBDemoWeb in the Pages panel, and click Details. In the Login panel, select Basic from the Authentication method drop-down list. Type index.jsp in the Realm name text box.
   5. Create index.jsp.
      1. Switch to Plug-in Development Perspective, in the Package Explorer, in JReportEJBDemoWeb, right-click WebContent, and select File from the New sub-menu.
      2. In the New File dialog, type index.jsp in the File name text box, and then click Finish.
      3. Write your JSP page code into the index.jsp file.
10. Now, both the EJB and the servlet are ready in the same application. The next step is to export an EAR file which includes the EJB and web project.
    1. In the Java EE Hierarchy tree, select JReportEJBDemo, right-click it and click Export EAR File.
    2. In the Export dialog, specify a directory in the Destination text box, using the default name JReportEJBDemo. JReportEJBDemo.ear will be generated in the specified directory.

##### Deploying the EAR file and starting the servlet

To deploy the EAR file:

1. Open the WebSphere Administration page.
2. Select Install New Application in Applications in the left panel of the page.
3. For the Preparing for the application installation section, click the Browse button of the Local path. Then, go to the directory where JReportEJBDemo.ear exists, and select it.
4. Click Next for all the following steps until finish, and save the task.

To start the servlet:

1. Select Enterprise Application in Applications in the left panel of the administration page.
2. In the Enterprise Application panel, select JReportEJBDemo and then click the Start button.
3. Use a web browser to load the JSP page. For example, the URL may be like this: `http://192.168.129.53:9080/JRServer/index.jsp`.

Note: When deploying JReport Engine Bean as an EJB to WebSphere5.1, JReport Engine Bean is wrapped into a session bean. For more information, refer to the demo program in ejbWebsphere51.zip in `C:\JReport\Designer\help\designer\samples`.