This tutorial walks you through the necessary steps to create and run an EJB Web service in IBM WebSphere® Studio Application Developer; it also discusses how to invoke the Web service from some basic application clients created using the Microsoft® .NET®- Framework SDK. I will demonstrate how to generate Web Service Description Language (WSDL) proxies for consumption by these Microsoft .NET clients in both the C# and JScript programming languages, which are both currently supported by the Microsoft .NET Framework. Note that C#, pronounced "C sharp," is a new Microsoft programming language similar to Java™ and C/C++. I will even show you how to combine proxies created in both languages into single executables. After you develop and test the EJB and Web service in Application Developer, I will show you how to create and run a simple console-based Microsoft .NET application as well as a graphical-based application written in C#.
This tutorial does not cover the specifics of developing Java, Web services, or J2EE applications. Instead, the tutorial's goal is simply to demonstrate that interoperability is possible. I will not go into great detail about the Microsoft .NET Framework because you can easily read about this yourself at the Microsoft MSDN Web site. Please see my two earlier tutorials that walk you through the details of creating and unit-testing J2EE applications in Application Developer. The first one, Developing and Testing a Complete "Hello World" J2EE Application with WebSphere Studio Application Developer, shows you how to create and test EJBs. The second article, Developing and Testing a Complete J2EE Application with WebSphere Studio Application Developer -- Part 2: Running on WebSphere Application Server, shows you how to deploy and test a J2EE application on WebSphere Application Server. Please do not interpret my sample code as some kind of blueprint or prescription for writing Web services; my simple sample code is not optimized to minimize network traffic, which is something a genuine Web service would need to do. My focus is simply to demonstrate a basic interoperability involving J2EE with WebSphere Application Server and Microsoft .NET.
You need the WebSphere Studio Application Developer 4.0x (even a trial version will do) and the Microsoft .NET Framework SDK. If you have Microsoft Visual Studio® .NET, you can perform the same steps, but everything I demonstrate can be done with Application Developer 4.0x and the Microsoft .NET Framework SDK, which is available from Microsoft at no charge.
My goal here is to show that industry support for open Web-based protocols (and the efforts behind them to make them standard) such as HTTP, SOAP, WSDL, and J2EE is essentially what makes it possible for a client running in the Microsoft .NET Framework to execute against a Web service/J2EE application running in the WebSphere test server in Application Developer. The EJB, Web service, and clients that you create are very simple, and the object types flowing back and forth are basic, simple types, but the exercise should serve to prove that this cross-framework interoperability is not only possible but necessary in heterogeneous environments. I hope that this will be a springboard for more complex interactions.
Listings for all source code, scripts, and commands are available
in all listings.txt in the download file provided with this article.
In this tutorial, you will perform the following tasks.
- Write a stateless session EJB
- Create a Web project
- Create an EJB Web service for the session EJB
- Test the EJB Web service (and thereby the EJB) using the generated sample
- Generate the Microsoft .NET WSDL proxies for the EJB Web service
- Compile the Microsoft .NET WSDL proxies for the EJB Web service
- Create, compile, and execute a simple console-based Microsoft .NET client
- Create, compile, and execute a graphical-based Microsoft .NET client
There are essentially eight development steps to developing the EJB, Web service, and Microsoft .NET client applications. The steps describe what you need to get started writing Microsoft .NET application clients that invoke Web services running in WebSphere Studio Application Developer's unit test server. I will be concentrating more on the client side for this interoperability scenario rather than on the server side, which I have discussed elsewhere. For introductory tutorials on developing and testing simple end-to-end J2EE applications, refer to my earlier two tutorials (see Resources).
A "bonus" section demonstrates how to create an ASP.NET Web service that communicates with the J2EE Web service you developed. This further demonstrates the vision of "cooperating applications" because the two Web services are in essence collaborating to accomplish a specific task.
The EAR module containing the J2EE EJB and the Web service is available in the download file so you can import the existing sample if you prefer.
Screen captures will be liberally used throughout this tutorial to show you exactly what the process entails and to make sure you know what to select to get the desired results. All of the application components were developed and tested on Windows® 2000 and Windows XP. Windows XP is used throughout for the screen captures.
Step 1. Write a stateless session EJB
In the first step, you will create a simple session EJB, which will be used to wrap an EJB Web service. Since currently only stateless session EJBs can be made into Web services with Application Developer, I will demonstrate how to create and use a simple stateless session bean.
- From the J2EE perspective, create an EJB project in the default EAR module called HelloWorldEJB using the EJB Project Creation wizard. Accept the rest of the default values and click Finish.
- Select the HelloWorldEJB project (already selected by default) and
then create a new session EJB (New => EnterpriseBean) called HelloWorld
in a package name of your choosing.
Figure 1. Creating a simple HelloWorld session bean
- Add the following three "business" methods to the HelloWorldBean
and then promote them to the remote interface. Feel free to change the implementation
return string to one that appeals to you (in case you object to using my name).
See Listing 1 for this snippet.
Listing 1. Adding methods
public String sayHelloWorld1() { return "Hello World from Sheldon"; } public String sayHelloWorld2(String name) { return "Hello World to " + name + " from Sheldon"; } public String sayHelloWorld3(String name, int age) { return "Hello World " + name + " from Sheldon. You're " + age + " years old!"; }
- Right-click the EJB project and select Generate deploy and RMIC code from the pop-up menu. Select the HelloWorld check box when prompted.
In this step, you will create the Web project, which will contain the EJB Web service that you create in the next development step.
- From the J2EE perspective, select New => Web Project, and use the Create Web Project wizard to create a Web project in the default EAR module called HelloWorldWeb.
- Set the context root for the Web application to
HelloWorld. - Click Finish.
Step 3. Create an EJB Web service
In this step, you will page through an easy-to-use Web service creation wizard that does all of the under-the-covers work for you to create the EJB-wrapped Web service and a sample test client application.
- From the Web perspective, select New => Web Service, and use the Web Service wizard to create a new EJB Web service, which you will use to generate a proxy and a sample application, and create all of the necessary folders for you.
- Select or clear the check boxes according to the figure below. You can select
the Launch the sample check box if you want to, but I haven't done
so here so that you know how to launch it on subsequent occasions.
Figure 2. Creating an EJB Web service
- Click Next, and then click the Browse EJB Beans button to browse
the EJB beans. Select the HelloWorld bean. The entries should appear similar
to Figure 3 below.
Figure 3. Configuring the EJB as a Web service
- Proceed through the pages of the wizard and accept all of the defaults.
Do not select the use secure SOAP check box.
Figure 4. Accepting defaults --leaving "use secure SOAP" unchecked
- Accept the defaults of all three of the EJB remote methods to expose and
deploy. Accept all other defaults to use the SOAP encoding.
Figure 5. Specifying all exposed EJB methods for EJB Web service methods
- Accept the defaults on the binding proxy generation page as well. Again,
do not select the use secure SOAP (WebSphere only) check box.
It is beyond the scope of this tutorial to use SOAP security and doing so
would detract from my goal to keep things as simple as possible.
Figure 6. Using the preselected SOAP binding
- Click Finish.
Step 4. Test the EJB Web service
In the previous step, when you created the EJB Web service, you selected the Generate a sample check box in the wizard. This generates a sample JSPTM page, which is in effect a Web service test client because it allows you to invoke methods in your Web service, supply parameters, and observe the result values sent back.
- Select the provided Web services
TestClient.jspapplication, and select Run on Server. The WebSphere server should already be automatically configured and started for you, and the embedded Web browser should automatically launch set to the correct Web address. You should be set up as illustrated in Figure 7 in order to do this from the Navigator view.
Figure 7. Performing a "Run on Server" for the Web service test client sample
Note that if the Launch the Sample check box had been selected in the wizard above (see Figure 2 above), then steps 2 and 3 listed here could be combined into a single step. If users need to repeat the steps through the wizard, they need to select the Overwrite files without warning check box since the code needs to be regenerated.
- Try out all three of the EJB methods that were exposed -- all three versions
of the
sayHelloWorld()method. If you are able to invoke them successfully and get the expected return values, then this means that both the EJB and the Web service you created are working properly. Figure 8 below illustrates invoking thesayHelloWorld2()method, which takes a single string argument. I passed the string "Jane" as the parameter to use, and then clicked Invoke.
Figure 8. Invoking methods in the generated JSP sample
Step 5. Generate the Microsoft .NET WSDL proxies for the EJB Web service
- Use the Microsoft Web Services Description Language Utility (
wsdl.exe) from the command line to generate proxies for both the C# and JScript languages. Ensure that the WebSphere server that runs the Web service and the EJB is actually running. The following describes both C# and JScript for the generated proxies, but you don't need both (unless you want to try it out). I demonstrate both to show that it is reasonable to assume that any .NET client using proxies generated in any of the supported .NET languages should work here, although the process was only tested with C# and JScript. - For JScript proxies, issue the following commands from a directory you want
to work in:
wsdl /nologo /language:JS /out:HelloWorld-binding.jscript /namespace:SBW.J2EE.HelloWorld http://localhost:8080/HelloWorld/wsdl/HelloWorld-binding.wsdl wsdl /nologo /language:JS /out:HelloWorld-service.jscript /namespace:SBW.J2EE.HelloWorld http://localhost:8080/HelloWorld/wsdl/HelloWorld-service.wsdl
- For C# proxies, issue the following commands from a directory you want to
work in:
wsdl /nologo /language:CS /out:HelloWorld-binding.cs /namespace:SBW.J2EE.HelloWorld http://localhost:8080/HelloWorld/wsdl/HelloWorld-binding.wsdl wsdl /nologo /language:CS /out:HelloWorld-service.cs /namespace:SBW.J2EE.HelloWorld http://localhost:8080/HelloWorld/wsdl/HelloWorld-service.wsdl
Once you have completed step 2 and/or step 3 above, you will
have generated a service and a binding source file as the out parameter
that was specified.
Step 6. Compile the Microsoft .NET WSDL proxies for the EJB Web service
- For JScript proxies, issue the following commands that use the JScript.NET
compiler from the directory that you generated the proxies in:
jsc /nologo /out:HelloWorld-binding-jscript.dll /r:Microsoft.JScript.dll /target:library HelloWorld-binding.jscript jsc /nologo /out:HelloWorld-service-jscript.dll /r:Microsoft.JScript.dll /target:library HelloWorld-service.jscript
- For C# proxies, issue the following commands that use the C# .NET compiler
from the directory you generated the proxies in:
csc /nologo /out:HelloWorld-binding-csharp.dll /target:library HelloWorld-binding.cs CSC /nologo /out:HelloWorld-service-csharp.dll /target:library HelloWorld-service.cs
- Once steps 1 and 2 above have been completed, you should find the C# or
JScript file created and compiled as follows:
HelloWorld-binding-csharp.dllHelloWorld-binding-jscript.dllHelloWorld-binding.csHelloWorld-binding.jscriptHelloWorld-service-csharp.dllHelloWorld-service-jscript.dllHelloWorld-service.csHelloWorld-service.jscript
These are the proxies generated for a particular .NET language and then compiled to dynamic link libraries using the appropriate .NET compiler for the language.
Step 7. Create, compile, and execute a simple console-based Microsoft .NET client
Create a simple no-frills console-style application by typing the following code and then compiling and running it.
1. For a simple client, use any text editor to create a new
C# source file called, SimpleWebServicesClient.cs, that contains
the following lines:
using System; using SBW.J2EE.HelloWorld;
public class SimpleWebServicesClient {
public static void Main() {
HelloWorldService proxy = new HelloWorldService();
Console.WriteLine("Invoking EJB methods through the proxy.");
string message = null;
try {
message = proxy.sayHelloWorld1();
WriteMessage(message);
message = proxy.sayHelloWorld2("John");
WriteMessage(message);
message = proxy.sayHelloWorld3("Jane", 29);
WriteMessage(message);
}
catch (Exception e)
{Console.WriteLine("Threw general exception: {0}", e);}
}
private static void WriteMessage(string message) {
Console.WriteLine("Server returns: {0}", message);
}
} |
See Listing 6 for the complete source code.
2. To compile and link, type the following at the command line using the JScript libraries that we built:
CSC /nologo /out:SimpleWebServicesClient.exe /r:Microsoft.JScript.dll /r:HelloWorld-binding-jscript.dll /r:HelloWorld-service-jscript.dll SimpleWebServicesClient.cs |
To build a similar executable using the C# libraries, type:
CSC /nologo /out:SimpleWebServicesClient.exe /r:HelloWorld-binding-csharp.dll /r:HelloWorld-service-csharp.dll SimpleWebServicesClient.cs |
3. After building the SimpleWebServices.exe executable
(using either the JScript or the C# library), run it from the command line in
the directory with the libraries, after ensuring that the WebSphere server instance
in Application Developer is running. If you have done everything correctly up
to this point, you should see the following output displayed in the console:
C:\tutorial>SimpleWebServicesClient Invoking EJB methods through the proxy. Server returns: Hello World from Sheldon! Server returns: Hello World to John from Sheldon! Server returns: Hello World Jane from Sheldon. You're 29 years old! |
If you prefer, you can run the supplied build-run-csharp.bat
or build-run-jscript.bat files. Doing so will create and compile
the proxies, build the client application, and execute it in one step.
Step 8. Create, compile, and execute a graphical-based Microsoft .NET client
To develop a more complex, graphical-based Windows application client for the Microsoft .NET platform, follow the steps below. You will construct a simple form window with some simple controls and event handlers that make it possible to enter a name and age, and then invoke the service. We add a bit of logic to call the first EJB method if no name or age is submitted (or age but not name), the second EJB method if the name only is submitted, and the third if both a name and an age are submitted. This will complete the round of showing a Microsoft .NET client-side front-end to the J2EE server-side back-end that we developed.
- Create a graphical-based Microsoft .NET application client by typing the
following code and compiling it. See Listing 11 for the complete listing,
which is not reproduced here.
... public class WindowsWebServicesClient : Form { ... // Event handler implementation private void button_Click(object sender, EventArgs evArgs) { System.Console.WriteLine("Invoking the HelloWorld EJB Web service..."); string message = null; int age = 0; if(firstNameTextBox.Text == "" && ageTextBox.Text == "") message = proxy.sayHelloWorld1(); else if(firstNameTextBox.Text != "" && ageTextBox.Text == "") { message = proxy.sayHelloWorld2(firstNameTextBox.Text); } else if(firstNameTextBox.Text != "" && ageTextBox.Text != "") { // no type checking in this simple example. // Only enter integers. age = Convert.ToInt32(ageTextBox.Text); message = proxy.sayHelloWorld3(firstNameTextBox.Text, age); } else message = "If you enter an age, you must enter a name. Try again."; // display back the results messageTextBox.Text = message; } } - Compile it as you did above, as follows:
JScript:
CSC /nologo /out:WindowsWebServicesClient.exe /r:Microsoft.JScript.dll /r:HelloWorld-binding-jscript.dll /r:HelloWorld-service-jscript.dll WindowsWebServicesClient.cs
C#:
CSC /nologo /out:WindowsWebServicesClient.exe /r:HelloWorld-binding-csharp.dll /r:HelloWorld-service-csharp.dll WindowsWebServicesClient.cs
- Run the application. Based on the simple logic above, which governs which
of the three EJB methods to call, you should see a response similar to Figure
9 below.
Figure 9. Invoking the Web service with no parameters
Figure 10. Invoking the Web service passing a parameter for "name"
Figure 11. Invoking the Web service passing a parameter for "name" and "age"
Try these steps if you want to take the idea to the next logical step, which is Web service-to-Web service, machine-to-machine. These steps demonstrate how to set up a simple ASP.NET Web service that is written in C# and deployed to the .NET Framework, which talks to the EJB Web service running in the Application Developer test server. Although both sets of Web services are running on one physical machine, we can still consider this a machine-to-machine case for all intents and purposes. Both are running in very different programming and component models but are able to talk to each other relatively seamlessly.
Step 1. Write an ASP.NET Web service in C#
- Using any plain text editor, create a new file in the directory in which
you have been generating and compiling the proxies and application clients,
and name it:
HelloWorld.NETWebService.asmx. - This Web service will expose three methods, which in turn invoke the three
methods on the Web service running in Application Developer's WebSphere Application
Server unit test environment. After you complete the text, save your
ASP.NETfile.<%@ WebService Language="C#" Class="HelloWorldNETWebService" %> using System.Web.Services; using SBW.J2EE.HelloWorld; public class HelloWorldNETWebService : WebService { HelloWorldService proxy = new HelloWorldService(); string message = null; [WebMethod] public string SayHelloWorld1() { message = proxy.sayHelloWorld1(); return message; } [WebMethod] public string SayHelloWorld2(string firstName) { message = proxy.sayHelloWorld2(firstName); return message; } [WebMethod] public string SayHelloWorld3(string firstName, int age) { message = proxy.sayHelloWorld3(firstName, age); return message; } }
See Listing 12 for the complete listing.
Step 2. Set up a shared folder for a virtual directory for Internet Information Server
It is relatively straightforward to use the physical directory
where you create and edit the ASP.NET file as
a virtual directory that the Microsoft Internet Information Server (IIS) can
use without moving any of your source files around. In my case, I did all of
my development in c:\tutorial, so this is the directory that I
enabled as Web Sharing. Any directory will do, however.
1. Share the folder for IIS by right-clicking the directory, going to Properties, and clicking the Web Sharing tab.
Figure 12. Sharing the working directory as a virtual directory

2. Create a subdirectory called \Bin below the
directory that this virtual directory is mapped to, and place the C# versions
of the DLLs there. See Step 6 above. In other words, build:
Bin\HelloWorld-binding-csharp.dll Bin\HelloWorld-service-csharp.dll |
This important step enables the compiled ASP.NET to locate and load the classes that it depends on.
Step 3. Run the ASP.NET Web service
1. Make sure the IIS service is started (and the WebSphere server in Application Developer is also running), and then open a browser to: http://localhost/HelloWorld/HelloWorldNETWebService.asmx
2. Launching this URL gives you a generated test client running in IIS for the Web service. It also lets you view the Service Description as a WSDL.
Figure 13. Viewing two of the available three operations supported

This is a clear demonstration of a simple case of "collaborative applications" that work together as one application although they are developed on two entirely different platforms -- Microsoft .NET and J2EE -- using similar, standardized Web service technology. Click the SayHelloWorld2 operation and enter a name parameter string value.
Figure 14. Testing the ASP.NET Web service to J2EE Web service

There is very little logic on the ASP.NET side and it is merely proxy to proxy. In a real application though, it may be better to incorporate the data or transaction from the other (WebSphere) Web service into its own.
Figure 15 below shows the return value of the ASP.NET Web service talking to the WebSphere plus J2EE Web service in Application Developer. It is Web service to Web service, Microsoft .NET to WebSphere plus J2EE to Microsoft .NET.
Figure 15. Viewing the SOAP return value for the invoked methods

This tutorial has walked you through all of the essential steps for preparing a simple J2EE application and Microsoft .NET application client that was developed on very different programming models and platforms but connected by using open Internet-based standards. It is these open standards that make it possible to invoke them from a variety of platforms and operating systems -- even from the .NET platform to the J2EE platform. The goal was not to show you how to develop Web services on either J2EE or on the Microsoft .NET platform, but rather to demonstrate that they can interoperate, and to lend credibility to the hopes and dreams that many of us have for the future of Web services.
| Name | Size | Download method |
|---|---|---|
| HelloWorldWebServices.zip | 105 KB | FTP |
Information about download methods
- Sheldon Wosnick, Developing and Testing a Complete
"Hello World" J2EE Application with WebSphere Studio Application
Developer, IBM WebSphere Developer Technical Journal, October 2001.
- Sheldon Wosnick, Developing and Testing a Complete
J2EE Application with WebSphere Studio Application Developer -- Part 2: Running
on WebSphere Application Server, IBM WebSphere Developer Technical Journal,
November/December 2001.
- Microsoft Developer Network (MSDN)
Note: The following code is sample code not created by IBM Corporation. This sample code is not part of any standard IBM product and is provided to you solely for the purpose of assisting you in the development of your applications. The code is provided "as is," without warranty of any kind. IBM shall not be liable for any damages arising out of your use of the sample code, even if they have been advised of the possibility of such damages.

Sheldon Wosnick is a software developer on the IBM WebSphere Studio Application Developer, Server Tools team at the IBM Toronto Lab. With his teammates, he is currently responsible for the entire server run time and unit test environment for Application Developer. Previously, he was a member of the VisualAge® for Java WebSphere Tools team. Sometimes fondly known as the "run time guy," he designed and integrated the WebSphere Test Environment and the Apache Tomcat Test Environment for VisualAge for Java, two very popular features in VisualAge for Java. You can reach Sheldon at swosnick@ca.ibm.com.





