464 WebSphere Studio Application Developer Version 5 Programming Guide Creating a Web service from a session bean Creating a Web service from an EJB session bean is very similar to the process for a JavaBean. There are only a few differences. Let us go through the process in abbreviated fashion: In the J2EE Hierarchy view, expand the ItsoProGuideEJB project. Select the BankEJB session bean and New -> Other -> Web Services -> Web Service . The type of Web service is now preselected as EJB Web service . Select Generate a proxy and Test the generated proxy . Select the ItsoProGuideWebServ project as the Web project. Leave all the defaults on the EJB configuration. Leave all the defaults on the identity. The URI is: http://tempuri.org/itso.ejb.model.facade.BankEJB Leave all the methods selected. Leave the proxy class to proxy.soap.BankEJBProxy. Leave the sample test application folder as sample/BankEJB. Make sure the client project is ItsoProGuideWebServClient. Click Finish . The code is generated and the test client opens (Figure 13-18). Figure 13-18 Web service sample for EJB service Chapter 13. Developing Web services 465 Implementing a real client application We provide a very simple client consisting of one JSP to execute the deposit and withdraw methods of the Banking service: Import the TestBankingWebService.jsp into the ItsoProGuideWebServClient project (Web Content folder). The main logic is shown in Figure 13-19: – A proxy bean (proxy.soap.ClientBankingProxy) to invoke the Web service is allocated using <useBean>. – The parameters from the form are retrieved and tested. – Depending on the Submit button, either the deposit or withdraw method of the Web service is invoked. – After the banking operation, the getAccount method is invoked so that we can display the account information. – The rest is HTML code with JSP tags to display the form with input fields and push buttons, and to display the resulting account information. – Note that the proxy and the Account classes are imported. The client proxy makes coding of a real client very simple. Notes: If you encounter runtime errors, restart the server. Methods that return collections are not supported in the test client, because an instance cannot be created. The methods of our session bean return arrays of objects; these are very well supported by SOAP encoding. 466 WebSphere Studio Application Developer Version 5 Programming Guide Figure 13-19 Client JSP to test the Web service (extract) A sample run of the client is shown in Figure 13-20. <jsp:useBean id="proxy" class="proxy.soap.ClientBankingProxy" scope="session"></jsp:useBean> <%! String accid = null; String amount = null; String deposit = null; String withdraw = null; java.math.BigDecimal amountD = new java.math.BigDecimal(0.00); Account account = new Account(); %> <% accid = request.getParameter("accountid"); amount = request.getParameter("amount"); deposit = request.getParameter("deposit"); withdraw = request.getParameter("withdraw"); if ( accid != null && amount != null && !accid.trim().equals("") && !amount.trim().equals("") ) { amountD = new java.math.BigDecimal(amount); if ( deposit != null ) proxy.deposit(accid, amountD); if ( withdraw != null ) proxy.withdraw(accid, amountD); account = proxy.getAccount(accid); } %> <h1>Banking Web Service Test</h1> <FORM action="/ItsoProGuideWebServClient/TestBankingWebService.jsp" method="post"> <TABLE> <tr><td>Enter an account number:</td> <td><INPUT type="text" name="accountid" size="20" value="<%= accid %>"></td></tr> <tr><td>Enter an amount:</td> <td><INPUT type="text" name="amount" size="20"></td></tr> <tr><td> </td> <td><INPUT type="submit" name="deposit" value="Deposit"> <INPUT type="submit" name="withdraw" value="Withdraw"></td></tr> </TABLE> <h2>Account information</h2> <TABLE border="1"> <tr><td>Account number: </td><td><%= account.getId() %> </td></tr> <tr><td>Account type: </td><td><%= account.getType() %> </td></tr> <tr><td>Account balance: </td><td><%= account.getBalance() %></td></tr> </TABLE> </FORM> Chapter 13. Developing Web services 467 Figure 13-20 Client JSP invoking a Web service Summary In this chapter, we introduced Web services. We then showed an example of using Application Developer to generate a Web service from an existing JavaBean. Finally, we showed how easy it was to create a Web service client using the built-in wizard. More information The 2003 IBM Redbook WebSphere Version 5 Web Services Handbook , SG24-6891, goes into thorough detail about the concept of SOA as well as Web Services for WebSphere Version 5. It also contains examples using Application Developer Version 5. Additional information about using and creating Web Services is available from the online help included in Application Developer. 468 WebSphere Studio Application Developer Version 5 Programming Guide © Copyright IBM Corp. 2003. All rights reserved. 469 Chapter 14. Developing GUI applications Application Developer 5 introduces the Visual Editor for Java (hereafter called Visual Editor) that lets developers build graphical user interfaces (GUIs) based on the JavaBeans component model. In this chapter we introduce you to the Visual Editor and develop a sample GUI, which lists the content of a table in a DB2 database and has an action that writes the selected value back to a text field. This GUI is runnable as a JavaBean and as a Java application. This chapter provides information on the following topics: Introduction to the Visual Editor for Java Sample GUI Setting up your sample project Launching the Visual Editor Visual Editor look and feel Customizing the appearance of the Visual Editor Changing the default Java Editor Working with the Visual Editor Adding data to the JavaBean Adding additional methods to the sample GUI Writing event handling code Running and testing JavaBeans Running the sample outside of Application Developer 14 470 WebSphere Studio Application Developer Version 5 Programming Guide Introduction to the Visual Editor for Java The Visual Editor for Java is a code-centric editor that helps you design applications containing a graphical user interface (GUI). It is based on the JavaBeans component model and supports visual construction using either the Abstract Windows Toolkit (AWT) or Swing. For more information concerning Swing and AWT, see the either Application Developer’s help manual or Sun’s Web site: http://java.sun.com/products/jfc/index.html The Visual Editor allows you to compose class files visually. Using the Visual Editor, you can drag beans from different palettes, manipulate them in the Design view, and edit their properties in the Properties view. The Visual Editor also includes a Source view where you can both see and modify the generated Java code. You can make changes in either the Source view or in the Design view. The new Visual Editor provides similar function as the earlier VisualAge for Java Visual Composition Editor. Unlike VisualAge for Java, the Visual Editor is a code centric editor, so you have to use its embedded Java editor to write event handling logic. Sample GUI The sample GUI we develop in this chapter is shown in Figure 14-1. The sample GUI displays a list with the last names of customers that are stored in the sample EJBBANK database. The GUI also provides a push button action that retrieves the corresponding first name of the customer and writes the first name to a text field in the GUI. Note: JavaBeans is basically a portable, platform independent, reusable component model. When talking about a JavaBean in this chapter, we mean a reusable software component that can be visually manipulated in builder tools. Chapter 14. Developing GUI applications 471 Figure 14-1 Sample GUI By creating the sample GUI, you should learn how to work with the new Visual Editor and how to compose and add visual components, change their properties, add event handling code, and run the GUI. Setting up your sample project To demonstrate the capabilities of the Visual Editor, we set up a new project. Therefore we create a new Java project and name it ItsoProGuideGui. See “Creating a Java project” on page 94 for a description of how to create a new Java project. Once this is done, we create a Java package named itso.gui. “Creating Java packages” on page 99 provides information regarding this issue. Our new project skeleton is shown in Figure 14-2. Figure 14-2 Project skeleton You also have to update the Java build path for this project. You do this by selecting the ItsoProGuideGUI project and Properties from the context menu. Then you select the Java Build Path entry and the Libraries tab (Figure 14-3). Click Add Variable , select DB2JAVA and confirm both dialogs with OK . JList bean JButton bean JTextField bean JLabel bean 472 WebSphere Studio Application Developer Version 5 Programming Guide See “Creating and working with a Java project” on page 94 for more information about the Java build path. Figure 14-3 Libraries settings for the sample project After having created the project, we create a new class and launch the Visual Editor. Launching the Visual Editor The Visual Editor allows you to create and modify application GUIs by manipulating beans in a WYSIWYG (what you see is what you get) editor. There are two ways to launch the Visual Editor: Create a visual class, which is automatically assigned and opened with the Visual Editor. Create a Java class from scratch and open the class with the Visual Editor. In this example we create a visual class as described in the first step above (“Creating Java classes” on page 100 provides more information about how to create a new Java class). Note: The .java file that you open in the Visual Editor must be stored in a Java project. Chapter 14. Developing GUI applications 473 Create a visual class To create a visual class, select File -> New -> Other -> Java -> Visual Class or, even easier, select the itso.gui package and New -> Visual Class from the context menu. Enter CustomerGUI in the Name field, make sure the package is set to itso.gui, select Panel from the extension list, select Swing —our sample class will inherit from the javax.swing.JPanel class—and select to have a main method created (Figure 14-4). Figure 14-4 Create a visual Java class We have a choice of using Swing or AWT as GUI framework. Table 14-1 shows a brief description of the user interface classes that can be selected in the New Java Class dialog. . testing JavaBeans Running the sample outside of Application Developer 14 470 WebSphere Studio Application Developer Version 5 Programming Guide Introduction to the Visual Editor for Java The. Application Developer Version 5. Additional information about using and creating Web Services is available from the online help included in Application Developer. 468 WebSphere Studio Application Developer. 464 WebSphere Studio Application Developer Version 5 Programming Guide Creating a Web service from a session bean Creating a Web service