Development And Deployment Guide

  • Uploaded by: Ahmad Safarini
  • 0
  • 0
  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Development And Deployment Guide as PDF for free.

More details

  • Words: 8,166
  • Pages: 49
Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Create a J2EE sample brokerage application using Eclipse WTP tooling Skill Level: Intermediate Manu T. George ([email protected]) Staff Software Engineer IBM

21 Mar 2007 Java™ 2 Platform, Enterprise Edition (J2EE) 1.4 supports two types of Web service endpoints: Plain Old Java Object (POJO) and Enterprise JavaBeans (EJB) endpoints. IBM® WebSphere® Application Server Community Edition (also referred to as Community Edition in this tutorial) is a J2EE 1.4-certified application server that provides support for these two types of Web service endpoints. This tutorial shows you how to use the Eclipse Web Tools Platform (WTP) to create an enterprise application that implements both POJO and EJB Web service endpoints, using IBM DB2® Express-C as the database for the application. You'll use the Community Edition server adapter (formerly known as the Eclipse plug-in) to deploy the application to an Community Edition instance. And finally, you'll develop a client to call the Web services.

Section 1. Before you start About this tutorial This tutorial shows you how to use many of the tools included in the Eclipse Web Tools Platform (WTP) to create a J2EE sample brokerage application that

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 1 of 49

developerWorks®

ibm.com/developerWorks

implements both POJO and EJB Web service endpoints. You'll use IBM DB2® Express-C as the database for the application, and you'll use the Community Edition server adapter to deploy the application to an Community Edition instance. Afterward, you'll use the Eclipse WTP Web service testing functions to test your Web services and write clients to invoke them.

Prerequisites To succeed with this tutorial, you should be familiar with Java development in general and, specifically, server-side Java development. You should understand the general concepts behind relational databases and be familiar with basic J2EE concepts, such as deployment descriptors and WAR archives. You should also be familiar with XML, XML schemas, Eclipse, and the Eclipse WTP. Prior experience with application servers, Web services, and relational databases is also recommended.

System requirements You need to download the following required (no-charge) applications to follow along with this tutorial and work with the sample code included: • IBM DB2 Express-C 9 • IBM WebSphere Application Server Community Edition V1.1 • The WebSphere Application Server Community Edition server adapter (formerly called the Eclipse plugin) • The Eclipse IDE with Web Tools Platform • The Java Web Services Developer Pack (Java WSDP) 2.0 • JDK 1.4.2_08 from Sun Microsystems Your hardware configuration should include the following: • Support for the JDK/JRE listed above with at least 512MB of main memory (1GB recommended) • At least 10MB of additional free disk space to install the software components and examples The instructions in this tutorial are based on a Microsoft® Windows® operating system. All of the tools and techniques covered in this tutorial also work on Linux® and UNIX® operating systems.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 2 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

Section 2. Get started -- an overview of J2EE Web services A Web service is a software component that can be invoked over a network and that uses a standardized XML messaging system for data exchange. It can be accessed over standard network protocols, such as Simple Mail Transfer Protocol (SMTP) and HTTP using SOAP as the messaging system. A Web service is described via the Web Services Description Language (WSDL) specification (see Resources for a link to more WSDL content), and it can be registered in a Universal Description, Discovery, and Integration (UDDI) registry where interested clients can look up the service. The clients of the Web service can be written in any language and on any platform, and they can invoke the service by following the rules in the WSDL file. Two of the important specifications that are used by Web services are: • WSDL -- A language for describing Web services as a set of endpoints operating on messages containing either document-oriented or procedure-oriented information. • SOAP -- An XML-based object invocation protocol (see Resources for a link to more SOAP content). In J2EE 1.4, Web services can be implemented using two types of endpoints: POJOs, or servlet endpoints, and EJB endpoints: • A Web service is called a POJO Web service if the service implementation class is an ordinary java object. In Community Edition, such a Web service is registered as a servlet in the Web application's deployment descriptor. • A Web service is called an EJB Web service if the service implementation class is a stateless session bean, and the methods in the session bean class are exposed for invocation using SOAP calls.

Implement J2EE Web services in Community Edition Community Edition V1.1 supports J2EE Web services by using Apache Axis 1.4 (see Resources for a link to more information on Axis). Axis is a Web service run time from the Apache Software Foundation. It's configured using GBeans and is plugged in to Community Edition. It provides support for both types of J2EE Web

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 3 of 49

developerWorks®

ibm.com/developerWorks

service endpoints described earlier. In the case of a POJO Web service, the request-response flow is shown in Figure 1. Note: This graphic presents a simplified view, and it doesn't show all the details of the integration of Axis with Community Edition. Figure 1. POJO Web service handling in WebSphere Application Server Community Edition

In the flow of events depicted in Figure 1, first the Web service is deployed in the server using custom Community Edition deployers. During the deployment process, all the information in the deployment descriptor is extracted by the deployer and populated into a GBean -- namely, AxisWebServiceContainer. Now, during the startup of the application, all the servlets are registered with Tomcat. During the registration process, a custom servlet called POJOWebServiceServlet is registered for URLs corresponding to POJO Web services. The AxisWebServiceContainer and the POJO representing the Web service are put in the ServletContext, and their corresponding keys are passed to the POJOWebServiceServlet as initialization parameters. Now, whenever the Web service is invoked using its configured URL, the POJOWebServiceServlet is invoked, and it looks up the AxisWebServiceContainer and POJO and passes them to Axis along with the request and response objects. Axis then invokes the required method on the POJO and returns the response. In the case of an EJB Web service, the EJB container -OpenEJB -- also comes as a layer between the request and response. A simplified representation of the request-response flow in the case of an EJB Web service is shown in Figure 2. Figure 2. EJB Web service handling in WebSphere Application Server Community Edition

Note that the enterprise bean is hosted in the EJB container. It's invoked by the EJBContainerProvider class, which is the pivot handler that's registered in Axis. Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 4 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

So, the pivot handler, which is a custom one packaged with Community Edition, takes care of invoking the session bean deployed in the OpenEJB container. Also, a valve is registered -- EJBWebServiceValve -- which lets the Web service requests be processed in a custom manner. This valve takes care of invoking the methods on the AxisWebServiceContainer class.

Section 3. Application overview The application included with this tutorial (see the Download section) represents an online brokerage tool that's used to buy and sell stocks. The application has the following five pages: • Login • Registration • User Details • Available Stocks • User Portfolio The application provides the following two Web services: • StockService • UserStockService Figure 3 shows the application flow. Figure 3. Online brokerage application flow

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 5 of 49

developerWorks®

ibm.com/developerWorks

The user is first shown the Login page. From there, the user enters his or her user name and password and submits the page. After validation of the user's credentials, the Available Stocks page is displayed, where the user can view all the stocks that are for sale on the exchange at that time. The user can choose to buy any stock (provided there's enough money in his or her account) by clicking the Buy button. After the transaction completes successfully, the user is brought back to the Available Stocks page. Clicking the View Your Portfolio link takes the user to his or her portfolio, which shows all the stocks the user owns. The user can sell whichever stock he or she owns from this page. This page also shows the user's free cash in the user cash field. If the user is new, he or she can go to the Registration page by clicking the Register button on the Login page. The user can then enter all the fields and click Submit to register. He or she can also view the available stocks and his or her portfolio in a stand-alone client by invoking two Web services: StockService and UserStockService, respectively.

Section 4. Set up the database Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 6 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

Create the database using the DB2 Express-C Control Center In the following section, you'll create a database and tables for users and groups in the DB2 Express-C Control Center by using the following steps: 1.

Open the Control Center.

2.

Right-click All Databases, and select Create Database > Standard.

3.

Enter TRADEDB as the database name.

4.

Click Finish.

A new database called TRADEDB is created and listed under All Databases (see Figure 4). Figure 4. New database listing

Create tables using the Control Center

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 7 of 49

developerWorks®

ibm.com/developerWorks

In the previous section, you created a database called TRADEDB. Now you'll create tables for users (USERS), stocks (STOCKS), and user accounts (TRADINGACCOUNT) within that database. The fields within the different tables are shown in Table 1. Table 1. USERS Field name Data type

Key

userid

VARCHAR(15) PRIMARY KEY NOT NULL

name

VARCHAR(50)

password

VARCHAR(24)

address

VARCHAR(96)

cash

DECIMAL

Table 2. STOCKS Field name Data type

Key

ID

VARCHAR(8)PRIMARY KEY NOT NULL

name

VARCHAR(100)

Price

DECIMAL

Table 3. TRADINGACCOUNT Field name Data type

Key

userid

VARCHAR(15) FOREIGN KEY NOT NULL

stockid

VARCHAR(18) FOREIGN KEY NOT NULL

quantity

INT

To create the tables, follow this procedure: 1.

Click Tables under TRADEDB, then right-click Create.

2.

Enter USERS as the table name, and click Next.

3.

Add a column to the table, then click Add and enter the column name.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 8 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

4.

Select Data type (nullable or not), and click Apply.

5.

Repeat steps 3 and 4 to add and name all columns.

6.

Click Next > Next > Next to navigate to the screen titled Define keys on new table. Click Add Primary.

7.

From the available columns, select the column for primary key, and click Finish.

8.

Follow steps 1-7 to create all three tables.

9.

Right-click the TRADINGACCOUNT table, then click Select alter.

10. In the dialog box shown, select the Keys tab. 11. Click Add Foreign. 12. In the PARENT table (or nickname section), select the schema as the schema where you have created these tables. 13. Select STOCKS as the name of the table. This shows the primary key STOCKS in the primary key list. 14. In the Available columns section, select STOCKID, and click the > button. 15. Click OK. 16. Repeat this procedure, and map the USERID field of the USERS table to USERID of TRADINGACCOUNT. After you complete the previous steps, the tables are created under the ADMINISTRATOR schema. (Note: Schema assignment depends on the Windows login that you're using. If you log in as db2admin, the tables are created under the DB2ADMIN schema.) After creation, the tables can be viewed in the DB2 Express-C Control Center, as shown in Figure 5. Figure 5. Tables in DB2 Express-C Control Center

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 9 of 49

developerWorks®

ibm.com/developerWorks

Put values into tables Now follow these instructions to populate the tables with values (see Figure 6): 1.

Select the USERS table from the TRADEDB Tables area.

2.

Right-click USERS, then click Open > Add row.

3.

Enter the values, and select Commit.

4.

After you've entered all values, click Close.

5.

Follow the same steps for all other tables. Note that you can use the values in the db2_brokerage.sql file, which is in the sql directory.

Figure 6. Putting values into tables

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 10 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

Create a table using scripts Alternatively, you can also create the database, tables, and data using an SQL script by performing the following steps. The sample in this tutorial has a script file called db2_brokerage.sql; execute this script from a DB2 command line to create the database and tables as follows: 1.

Create a DB2 command window from the Windows command prompt by typing db2cw. This opens another command window for entering DB2 commands.

2.

Execute the script file from the DB2 command window (db2 -tvf db2_brokerage.sql). This path should be given if db2_brokerage.sql is in the same directory where the command is executed. Otherwise, you must give the full path instead of db2_brokerage.sql.

If there are no errors, you should see all SQL commands executed successfully. The script creates the database and all the related tables, as shown in Figure 7. Figure 7. Database and related tables

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 11 of 49

developerWorks®

ibm.com/developerWorks

Section 5. Create the Web application This section gives you detailed steps on using the Eclipse WTP to create a Web application.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 12 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

Install the server run time Create a new Community Edition V1.1 server run time by following these steps: 1.

Run Eclipse WTP by double-clicking eclipse.exe from the location where you extracted the WTP platform. You can also run eclipse.exe from the command line.

2.

Click Window > Open Perspective > Other > J2EE to open the J2EE perspective.

3.

Select the Servers tab at the bottom right of the screen.

4.

Right-click New > Server.

5.

Select IBM > WebSphere Application Server Community Edition v1.1 Server, then click Next.

6.

In the next screen, select the appropriate JRE, and fill in the Application Server Installation Directory field with the installation directory (C:\Program Files\IBM\WebSphere\AppServerCommunityEdition), then click Finish. An Community Edition V1.1 instance is created in the Servers view.

Develop the application In this section, you'll create a WAR file that contains pages for the application, and you'll add it to an EAR file. Create a Web application Create a Web application by following these steps: 1.

Run Eclipse WTP by double-clicking eclipse.exe from the location where you extracted the WTP platform. You can also run eclipse.exe from the command line.

2.

Select File > New Project > Web > Dynamic Web project.

3.

Enter BrokerageWebApp for the project name, and enter Websphere Community Edition Server 1.1 as the target run time.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 13 of 49

developerWorks®

ibm.com/developerWorks

4.

Click Next > Next.

5.

Enter the context root /brokerage.

6.

Click Next.

7.

Configure the Geronimo Deployment Plan as follows: • Under Group ID, enter wasce-samples. • Under Artifact ID, enter brokerage-web. • Under Version, enter 1.0. • Under Artifact Type, enter war.

8.

Click Finish.

Create an enterprise application Create an enterprise application by following these steps (see Figure 8): 1.

Run Eclipse WTP by double-clicking eclipse.exe from where you extracted the WTP platform. You can also run eclipse.exe from the command line.

2.

Select File > New Project > J2EE > Enterprise application project.

3.

Enter BrokerageApp for the project name, and enter Websphere Community Edition Server 1.1 as the target run time.

4.

Click Next > Next.

5.

Check both BrokerageEjb and BrokerageWebApp in the J2EE Modules to add to the EAR page.

6.

Click Next.

7.

Configure the Geronimo Deployment Plan as follows: • Under Group ID, enter wasce-samples. • Under Artifact ID, enter brokerage. • Under Version, enter 1.0. • Under Artifact Type, enter car.

8.

Click Finish.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 14 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

Figure 8. Create the applications within Eclipse WTP

Import the source files into Eclipse WTP 1.

Select and expand the BrokerageWebApp project.

2.

Right-click the BrokerageWebApp project, and select Import > Import.

3.

From the Import dialog box, select General > File System, and click Next.

4.

From the File System dialog box, browse to where you extracted the compressed file provided with the article, and in From Directory, select /web-src/.

5.

Expand the web-src directory in the left pane, and select com.

6.

Select Into Folder as the BrokerageWebApp/src, and click Finish.

7.

Similarly, import the source files in /ejb-src/ to BrokerageEjb/ejbModule.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 15 of 49

developerWorks®

ibm.com/developerWorks

8.

Right-click BrokerageWebApp, and select Build Path > Configure Build Path.

9.

In the Java Build Path dialog box, select the Projects tab.

10. Click Add, select BrokerageEjb in the Required Project Selection dialog box, then select OK. 11. Return to the Java Build Path dialog box, and select OK. 12. Clean the projects. You now have all the classes imported. To import the JavaServer Pages (JSP) files, perform the following steps. The JSP files are also imported into Eclipse: 1.

Right-click BrokerageWebApp, and select Import > Import.

2.

In the Import dialog box, select General > File System, then click Next.

3.

From the File System dialog box, browse to the JSPs directory in the brokerage.zip file, which contains all the JSPs, and click OK. The JSPs are listed in the right pane.

4.

Select all the JSPs, change the Into folder to point to BrokerageWebApp/WebContent, and click Finish. This imports all the JSP files to the WebContent directory.

Section 6. Use XDoclet to generate J2EE artifacts in Eclipse WTP You can choose to develop classes manually or use XDoclet. To create an enterprise bean using XDoclet, use the following steps: 1.

Right-click the package you want your EJB class to belong to.

2.

Select New > Other.

3.

In the New dialog box, select JB/XDoclet Enterprise JavaBean, and click Next. (Note: If you get an error stating that the annotation provider is

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 16 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

not set correctly, go to Window > Preferences > Xdoclet, and click the XDoclet. Set the XDoclet Home tag to point to the home directory of XDoclet in your system. Select the right version, and click OK.) 4.

Select Session Bean, then click Next.

5.

In the Enterprise JavaBean dialog box, enter the relevant information, and click Next.

6.

Continue entering the remaining information, and then click Next > Finish.

XDoclet makes it convenient to generate much of your boilerplate code, because it allows you to create many other necessary J2EE artifacts. Because you've already imported all the EJB components needed for this application, it's not necessary to go through this step.

Section 7. Generate the WSDL files and mapping files for Web services As mentioned earlier in this tutorial, this application contains two Web services: One is exposed as a servlet endpoint, and the other is exposed as an EJB endpoint. Community Edition doesn't provide a mechanism for generating the Java API for XML-based RPC (JAX-RPC) mapping files. You can use the Apache Axis WSDL2Java and Java2WSDL tools to generate the WSDL/Stubs, but you need to use the wscompile utility of the Java WSDP to generate the JAX-RPC mapping files. You can also use the Java WSDP to generate the WSDL. In the Java WSDP directory of the sample application archive provided with this article, you'll find the configuration files that you can use to generate the WSDL and JAX-RPC mapping files for the two Web services -- config_s for StockService and config_us for UserStockService. The commands for running wscompile to generate the mapping files are given in Listings 1 and 2. (Note: The lines in these two listings were broken due to space constraints.) Here, <dest-dir> equals the absolute path of the directory to put the generated

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 17 of 49

developerWorks®

ibm.com/developerWorks

classes, WSDL, and mapping files. <workspace> equals the absolute path of the Eclipse workspace directory. And <wasce_home> equals the Community Edition installation directory. Listing 1. Commands for running wscompile to generate mapping files wscompile -gen:server -d <dest-dir> -mapping <dest-dir>\mapping.xml -cp "<wasce_home>\ repository\org\apache\geronimo\specs\geronimo-j2ee_1.4_spec\ 1.1\geronimo-j2ee_1.4_spec-1.1.jar;<workspace>\BrokerageEjb\build\classes; <workspace>\BrokerageWebApp\build\classes;" C:\wscompile\config_s.xml

Listing 2. Commands for running wscompile to generate mapping files, continued wscompile -gen:server -d C:\wscompile -mapping C:\wscompile\ mapping.xml -cp "<wasce_home>\repository\org\apache\geronimo\specs\ geronimo-j2ee_1.4_spec\1.1\geronimo-j2ee_1.4_spec-1.1.jar;<workspace>\ BrokerageEjb\build\classes;" C:\wscompile\config_us.xml

Note: After installing the Java WSDP, you can add <JWSDP_HOME>\jaxrpc\bin to the PATH environment variable so that you can access wscompile from the command prompt. The default value of JWSDP_HOME is C:\Sun\jwsdp-2.0. After generating the WSDL and mapping files for each Web service, put them in the appropriate locations in the Eclipse projects: • For the stateless session bean endpoint (such as UserStockService), put the mapping.xml file directly inside the META-INF directory in ejbModule. • Inside the same directory, create a subdirectory called wsdl, and copy the WSDL file to that directory. • Similarly, for the servlet endpoint, copy the mapping.xml file to the WEB-INF directory and the WSDL file to WEB-INF/wsdl. (Note: If you're using the same destination directory, generate the files for one Web service first, and after copying the files, clean the output directory before generating the files for the next Web service. You can also access the generated files from the generated files directory in the brokerage directory.)

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 18 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

Section 8. Write the deployment descriptors In this section, you learn about writing the J2EE- and Geronimo-specific deployment descriptors.

Write the J2EE-specific deployment descriptors Because you have EJB servlets and Web services together in this application, you need to write five J2EE deployment descriptors -- one each for the Web module, the EJB module, and the EAR file, and two for the two Web services. All the descriptors are present in the j2ee-descriptors directory. (Note: Going through the individual deployment descriptors and what their elements mean is beyond the scope of this tutorial; you can refer to the J2EE specification for this information.) Because the Eclipse WTP provides schema-based editors for each of the plans, you don't need to manually write these descriptors. To create plans using the editor, follow these steps, and refer to Figure 9: 1.

Within Eclipse, select the BrokerageWebApp project, and expand it to show its contents.

2.

Click Deployment Descriptor to open the web.xml file in an editor.

3.

Select the Design tab of the editor to open a new editor showing a tree structure.

4.

Right-click the web-app to display all the possible operations you can do using the editor. The editor shows all the children you can add and remove.

5.

Refer to the deployment descriptors in the j2ee-descriptors directory of the brokerage.zip file, and create the descriptors. Alternatively, you can just import the descriptors to the META-INF and WEB-INF directories.

Figure 9. Create the applications within Eclipse WTP

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 19 of 49

developerWorks®

ibm.com/developerWorks

Write the Geronimo-specific deployment descriptors Next, you need to write the Geronimo-specific deployment descriptors for the Web (geronimo-web.xml) and EJB (openejb-jar.xml) modules and the entire enterprise application (geronimo-application.xml). The descriptors are shown below and are also present in the Geronimo-descriptors directory. Deployment descriptor #1: geronimo-web.xml The geronimo-web.xml file is created in the WEB-INF directory of the web-app: 1.

Double-click the geronimo-web.xml file, which opens the Community Edition Deployment Plan Editor, as shown in Figure 10. Figure 10. Deployment Plan Editor

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 20 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

2.

Click the Naming tab of the Community Edition Deployment Plan Editor.

3.

In the Resource References section, click the Add button. This brings up the Resource Reference Details screen.

4.

Enter jdbc/TradeDB as the Reference Name, and enter TradeDS as the Resource Link.

5.

Click Finish. Figure 11. Resource Reference Details

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 21 of 49

developerWorks®

ibm.com/developerWorks

6.

Click the Deployment tab in the Community Edition Deployment Plan Editor to open the Deployment Configuration screen.

7.

Click Add, and add two dependencies with the following details: • Group ID: geronimo • Artifact ID: axis • Version: 1.1 • Artifact type: car • Group ID: console.dbpool • Artifact ID: TradeDS • Version: 1.0 • Artifact type: rar

8.

This creates the deployment plan for the Web application. When you click the Source tab, the contents of the plan should appear as shown in Listing 3. Alternatively, you can enter the contents of Listing 3 directly into the Source editor. Listing 3. Entry into the Source editor

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 22 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

<web-app xmlns="http://geronimo.apache.org/xml/ns/j2ee/web-1.1" xmlns:nam= "http://geronimo.apache.org/xml/ns/naming-1.1" xmlns:sec= "http://geronimo.apache.org/xml/ns/security-1.1" xmlns:sys= "http://geronimo.apache.org/xml/ns/deployment-1.1"> <sys:environment> <sys:moduleId> <sys:groupId>wasce-samples <sys:artifactId>brokerage-web <sys:version>1.0 <sys:type>war <sys:dependencies> <sys:dependency> <sys:groupId>geronimo <sys:artifactId>axis <sys:version>1.1 <sys:type>car <sys:dependency> <sys:groupId>console.dbpool <sys:artifactId>TradeDS <sys:version>1.0 <sys:type>rar /brokerage jdbc/TradeDB TradeDS

(Note: Lines two through four in Listing 3 were broken due to space constraints.) Listing 3 shows the geronimo-web.xml file. Here, the environment tag represents the Web application environment. It defines the module ID of the Web module as well as the dependencies -- for example, the modules on which this module depends. The context-root tag gives the context root of the Web application to be /brokerage. The other important tag is the resource-ref tag, which is used to map the name jdbc/TradeDB to the database pool TradeDS, which you'll deploy in the following sections. This results in the corresponding data source being bound to java: comp/env/jdbc/TradeDB in Java Naming and Directory Interface (JNDI). Deployment descriptor #2: openejb-jar.xml The openejb-jar.xml file is created in the META-INF directory of the EJB module, as shown in Listing 4. Listing 4. openejb-jar.xml

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 23 of 49

developerWorks®

ibm.com/developerWorks

<sys:environment> <sys:moduleId> <sys:groupId>wasce-samples <sys:artifactId>BrokerageEJB <sys:version>1.0 <sys:type>car <sys:dependencies> <sys:dependency> <sys:groupId>geronimo <sys:artifactId>tomcat <sys:version>1.1 <sys:type>car <sys:dependency> <sys:groupId>console.dbpool <sys:artifactId>TradeDS <sys:version>1.0 <sys:type>rar <enterprise-beans> <session> <ejb-name>UserStock <jndi-name>ejb/UserStockBean jdbc/TradeDB TradeDS

(Note: Lines two through five in Listing 4 were broken due to space constraints.) The openejb-jar.xml file also contains the module ID and dependency declarations. It has a session element that's used to map the name of the bean to a jndi-name in this case. The resource-ref element is also present. The Community Edition Deployment Plan Editor for openejb-jar.xml is shown in Figure 12. Figure 12. Deployment plan editor

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 24 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

Deployment descriptor #3: geronimo-application.xml The geronimo-application.xml file is created in the META-INF directory of the EAR -for example, in the BrokerageApp project in EarContent/META-INF (see Listing 5). Listing 5. geronimo-application.xml <sys:environment> <sys:moduleId> <sys:groupId>wasce-samples <sys:artifactId>brokerage <sys:version>1.1 <sys:type>car

(Note: The second and third lines in Listing 5 were broken due to space constraints.)

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 25 of 49

developerWorks®

ibm.com/developerWorks

You don't need to make any changes to this file for this application. The WTP also provides schema-based XML editors for writing the plans. The schema-based editor for ejb-plan is shown in Figure 13. Figure 13. Schema-based editor

If double-clicking the Community Edition-specific deployment plan doesn't open it in the Plan Editor, right-click the Community Edition-specific deployment plan file, and select Open with Community Edition Deployment Plan Editor. This opens the editor.

Section 9. Import the project into Eclipse WTP The sample application bundled with this tutorial contains a source compressed file for the EAR file, which can be imported into the Eclipse WTP following the steps below:

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 26 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

1.

Select File > Import > J2EE, select EAR File, and click Next.

2.

Click Browse, select the file to import -- for example, BrokerageApp.ear -- and click Next.

3.

Click Next after selecting both the Web and EJB modules.

4.

Click Finish.

5.

Click I agree in the license dialog. The EAR file is imported, and the respective Eclipse projects are created.

6.

Right-click on the BrokerageApp project, and select the Targeted Runtimes option.

7.

Select Websphere Community Edition Server v1.1 as the target run time, and click OK.

8.

Rebuild all the projects. Four errors and 16 warnings remain, but ignore these for the time being.

Define a server To deploy an application, you first need to create a server in the Eclipse WTP by following the steps below: 1.

Right-click within the Servers Tab window, and select New Server (see Figure 14). Figure 14. Define a new server

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 27 of 49

developerWorks®

ibm.com/developerWorks

2.

Select Websphere Application Server Community 1.1 Server, then click Next.

3.

In the New Server dialog box, fill in the Application Server Installation Directory field so that it points to the server home directory, and click Next.

4.

Enter the server details as shown in Figure 15. Figure 15. Populate new server fields

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 28 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

5.

developerWorks®

Click Finish.

Start the server, and log in to the console To start the server and log in to the console, follow these steps: 1.

In the Servers tab, right-click Community Edition 1.1, and select Start. This starts the server inside Eclipse.

2.

When the server status becomes started, right-click the server icon again, and select Launch Community Edition Console.

3.

Now enter the user name as system and the password as manager, and click the Login button. This takes you to the Welcome page of the administration console.

Deploy the database pool A database pool is necessary to connect to DB2. You can create this by deploying a database pool plan using the wizard that's available in the Community Edition Console. Do the following: 1.

Log on to the Community Edition Console.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 29 of 49

developerWorks®

ibm.com/developerWorks

2.

Under Services, select Database Pools.

3.

Select Using the Geronimo Database Pools Wizard.

4.

Enter TradeDS in the field Name for Database Pools, and click Next.

5.

Enter the details shown in Figure 16, and click Next. Figure 16. Populate the Create Database Pool screen

6.

You can test the connection to check for any errors and then deploy the data source.

7.

You may see a license-related error. To work around this error, go back and click the Skip Test and Show Plan button.

8.

Copy the deployment plan to a file, and save it as db2-datasource-plan.xml.

9.

Add a dependency to the DB2 license .jar file in the repository, as shown in Listing 6. Listing 6. Add dependency to DB2 license .jar file

<dep:dependency>

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 30 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

<dep:groupId>com.ibm.db2 <dep:artifactId>db2jcc_license_cu <dep:version>8.2 <dep: type>jar

10. Now you can deploy the pool by deploying this plan. Select the Deploy New option from the console. 11. Select tranql-connector-db2-xa-1.1.rar for the archive and db2-datasource-plan.xml for the plan. 12. Click Install. 13. Once you deploy the pool, it's started and running. You can also use the plan file, provided in the plans directory of brokerage.zip, after editing the properties to reflect your database configuration. Remember that the user you configure in the plan should have access to the schema where you created the tables. Figure 17. Installed J2EE connectors

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 31 of 49

developerWorks®

ibm.com/developerWorks

Section 10. Deploy the application This section describes how to deploy the application using the Admin console. To get started, follow these steps: 1.

Right-click the server icon, and select Add and remove projects.

2.

In the Add and Remove Projects dialog box, select BrokerageApp, and click Add to add this project to the Configured projects list.

3.

Click Finish.

4.

Right-click the server icon, and select Publish to deploy the application.

Note: If deployment fails with the exception java.lang.IllegalArgumentException: targetPath is absolute, follow these steps: 1.

Shut down Eclipse, restart it, and then try again. The problem may stop occurring.

2.

If step 1 doesn't solve the problem, export the .ear file to a temporary location, open it with an extraction tool, and remove the http: directory from the WAR file inside the EAR file.

Now you need to deploy this application using the Admin console (see Figures 18 and 19). Figure 18. Deployment using the Admin console

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 32 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

Figure 19. Deployment using the Admin console

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 33 of 49

developerWorks®

ibm.com/developerWorks

Section 11. Test the application Now that you've successfully deployed the application, it's time to begin the testing process. In this section, you'll learn how to use the Web Services Explorer tool provided within the WTP.

Access the application Perform the following steps to access the application: 1.

Within the Navigator area, select Web Application (BrokerageWebApp).

2.

Right-click and select Run As, and then select Run on Server.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 34 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

3.

Choose an existing server. Select Community Edition 1.1, the server you defined earlier.

4.

If the project is already added, select Next and Finish.

5.

The application is available at http://localhost:8080/brokerage/. Figure 20. Access the application

Using the application: Application flow Now you're ready to try out a few tasks with the application! To get started, follow these steps: 1.

Log in to the application by entering the user name j2ee and password password. You're taken to the Available Stocks page.

2.

Under one of the available stocks, enter the quantity 2, and select the Buy radio button.

3.

Click the Buy Stock button.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 35 of 49

developerWorks®

4.

ibm.com/developerWorks

After the request is submitted, click the View Your Portfolio link, which takes you to the Portfolio screen. You can sell stocks similarly here. Figure 21. Available Stocks page

Figure 22. Portfolio page

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 36 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

5.

developerWorks®

Click the User Info button to access the page showing user details. Figure 23. User details

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 37 of 49

developerWorks®

6.

ibm.com/developerWorks

If you want to register a new user, click the Register button on the Login page to access the registration page where you can enter user details.

Invoke and test the Web services The next step is to test the Web services using the WTP tool. Web Services Explorer The WTP provides a tool called Web Services Explorer that lets you test the Web services you've deployed. The following steps describe how to use the Web Services Explorer to invoke the Web services that you deployed for testing: 1.

After the brokerage application is deployed, the Project Explorer pane of Eclipse shows a node called JSR - 109 Web Services.

2.

Expand this node to reveal two subnodes called Clients and Services.

3.

Expand the Services node to reveal two subnodes representing the two Web services that you deployed. Figure 24. Using Web Services Explorer

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 38 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

4.

Start the server if it's not already started.

5.

Right-click StockService, and select Test with Web Services Explorer. This brings up the screen shown in Figure 25. Figure 25. Test with Web Services Explorer

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 39 of 49

developerWorks®

ibm.com/developerWorks

6.

Click the getStocks link under Operations to bring up the Actions screen.

7.

Click Go to invoke the Web service. The output is shown in the Status pane, as shown in Figure 26. Figure 26. The Status pane

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 40 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

Generate the Web services client The Eclipse WTP also provides the functionality to generate Web services clients using the application server's Web service run time. In this application, the run time is Apache Axis (see Resources for a link to more information on Axis). The following steps illustrate the procedure to generate a Web service client using Eclipse: 1.

Select File > New > Project > Web > Dynamic Web Project, then click Next.

2.

Give the project name as ServiceClient, and click Finish to create the project.

3.

Once the brokerage application is deployed, the Project Explorer pane of Eclipse shows a node called JSR - 109 Web Services.

4.

Expand this node to reveal two subnodes called Clients and Services.

5.

Expand the Services node to reveal two subnodes representing the two Web services that you deployed.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 41 of 49

developerWorks®

ibm.com/developerWorks

6.

Start the server if it's not already started.

7.

Right-click UserStockService, and select Generate Client. This brings up the screen shown in Figure 27. Figure 27. Web services client

8.

Click the BrokerageWebApp Client project link, select Service Client as the Client project, and click OK.

9.

Click Finish. This generates the following classes inside the ServiceClient project: • UserStockIF • UserStockIFBindingStub • UserStockIFProxy • UserStockService • UserStockServiceLocator

Now you can write a client that uses these classes and invoke the Web service in the Client class:

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 42 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

1.

Import the contents in the client directory from the extracted file (provided with this article) to the respective directories (for example, import web.xml and geronimo-web.xml into WEB-INF; import source files to src; and import .jsp files to the root of the WebContent directory).

2.

In the Client class, create a UserStockServiceLocator instance call in the getUserStockIFPort method to get a handle to the UserStockIFProxy object. Invoke the getUserStocks method on the proxy to get the result.

Listing 7 shows the code for the client. Listing 7. Code to invoke the Web services in Client class package com.dev.trade.client; import java.io.IOException; import java.rmi.RemoteException; import import import import import

javax.servlet.ServletException; javax.servlet.http.HttpServlet; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; javax.xml.rpc.ServiceException;

import import import import

com.dev.trade.service.UserStockIF; com.dev.trade.service.UserStockService; com.dev.trade.service.UserStockServiceLocator; com.dev.trade.service.types.Stock;

public class ClientServlet extends HttpServlet { protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { listUserStocks(arg0, arg1); } protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { // TODO Auto-generated method stub listUserStocks(arg0, arg1); } public void listUserStocks(HttpServletRequest request, HttpServletResponse response) throws IOException { String name = (String) request.getParameter("name"); String password = (String) request.getParameter("password"); UserStockService userStockService = (UserStockService) new UserStockServiceLocator(); UserStockIF userStockIF = null; try { userStockIF = userStockService.getUserStockIFPort(); } catch (ServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); }

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 43 of 49

developerWorks®

ibm.com/developerWorks

Stock[] stocks = null; try { stocks = userStockIF.getUserStocks(name, password); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } response.getWriter().print(""); for (int i = 0; i < stocks.length; i++) { response.getWriter().print(""); } } }

Run the Web service client The following steps are necessary to run the Web service client: 1.

Compile the ServiceClient project.

2.

Right-click the server icon in the Servers pane, and select Add Remove Projects.

3.

Select ServiceClient, add it to Configure Projects, and click Finish. The ServiceClient client module is now deployed.

4.

Right-click ServiceClient, then select Run on Server > Finish. This brings up the User Stocks client JSP.

5.

Enter the user ID J2EE and the password password, then click Submit Query to get a listing of the user's stocks. Figure 28. User's stocks

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 44 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

Section 12. Summary Hopefully this tutorial has helped you learn how to develop Web services applications using Eclipse WTP and the WebSphere Application Server Community Edition server adapter. Using many of the tools included with the Eclipse WTP and the Community Edition server adapter, you created a J2EE sample brokerage application. You learned how XDoclet can be configured and used with the WTP to generate the boilerplate code required in J2EE applications. Along the way, you saw some of the wizards and editors provided by WTP, as well as specific editors provided by the Community Edition server adapter. You learned how to create a data source to connect to DB2 Express-C in Community Edition and how to deploy the application to Community Edition from Eclipse using the server adapter. You generated the mapping and WSDL files required to deploy Web services using the wscompile tool packaged with Sun Microsystem's Java WSDP tool. Finally, you used the Web service testing functions provided by the Eclipse WTP to test your Web services, and you wrote clients to invoke them. I welcome your feedback on this tutorial posted to the developerWorks IBM WebSphere Application Server Community Edition and Apache Geronimo forum.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 45 of 49

developerWorks®

ibm.com/developerWorks

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 46 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

Downloads Description

Name

Size

Download method

Tutorial source code

brokerage.zip

65KB

HTTP

Information about download methods

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 47 of 49

developerWorks®

ibm.com/developerWorks

Resources Learn • See "Using the Eclipse Plug-in for WebSphere Application Server Community Edition" (developerWorks, April 2006) for more information on the plug-in. • Visit the WebSphere Application Server Community Edition content area here on developerWorks for a comprehensive set of tools and resources. • Get product documentation for WebSphere Application Server Community Edition. • "Get started with WebSphere Application Server Community Edition" (April 2006, developerWorks) today using this helpful guide. • Read the developerWorks article "Using the Web console in WebSphere Application Server Community Edition" to get started using the Web console. • Learn more about "Using the Eclipse Plug-in for WebSphere Application Server Community Edition" (developerWorks, April 2006). • Explore the WebSphere Application Server Community Edition technical support offerings available from IBM. • Get more information on WSDL. • Get more information on the SOAP specification. • Check out the developerWorks Apache Geronimo project area for articles, tutorials, and other resources to help you get started developing with Geronimo today. • Visit the Apache Geronimo Web site. • Check out the IBM Support for Apache Geronimo offering, which lets you develop Geronimo applications backed by world-class IBM support. • Visit the developerWorks Open source zone for extensive how-to information, tools, and project updates to help you develop with open source technologies and use them with IBM's products. • Stay current with developerWorks technical events and webcasts. • Browse for books on these and other technical topics at the Safari bookstore. Get products and technologies • Download the WebSphere Application Server Community Edition server adapter (formerly known as the Eclipse plug-in). • Download the Eclipse Web Tools Platform from eclipse.org.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition Page 48 of 49 © Copyright IBM Corporation 1994, 2008. All rights reserved.

ibm.com/developerWorks

developerWorks®

• Download Apache Axis, a Java platform for creating and deploying Web services applications. • Download your free copy of WebSphere Application Server Community Edition -- a lightweight J2EE application server built on Apache Geronimo open source technology that is designed to help you accelerate your development and deployment efforts. • Download free software for rapid results, and kick-start your Java application development. • Innovate your next open source development project with IBM trial software, available for download or on DVD. Discuss • Participate in the discussion forum for this content. • Get involved in the developerWorks community by participating in developerWorks blogs.

About the author Manu T. George Manu T. George is a Staff Software Engineer for IBM in the India Software Labs, Bangalore. He is a part of the IBM WebSphere Application Server Community Edition Level 3 Support Team. He received a bachelor's degree in applied electronics from the College of Engineering at Trivandrum.

Trademarks IBM, the IBM logo, DB2, and WebSphere are registered trademarks of IBM in the United States, other countries, or both. Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Microsoft and Windows are trademarks of Microsoft Corporation in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries. Linux is a registered trademark of Linus Torvalds in the United States, other countries, or both.

Develop and deploy J2EE Web services using WebSphere Application Server Community Edition © Copyright IBM Corporation 1994, 2008. All rights reserved.

Page 49 of 49

Related Documents


More Documents from "Access Communications"

" + name + "'s Stocks
Id Name Price Quantity
" + stocks[i].getId() + "" + stocks[i].getName() + "" + stocks[i].getPrice() + "" + stocks[i].getQuantity() + "