

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


















Copyright © 2008 The Open University This document is made available under the Creative Commons Attribution - No Derivative Works 3.0 Unported Licence (http://creativecommons.org/licenses/by-nd/3.0/) WEB 00711 2 1.
In this activity you will see how to use Eclipse to create and test a very simple web service. Before I look at the service itself, however, I shall provide some background information about the different approaches that can be used to develop a web service.
Components of a web service
From an implementation point of view, a web service typically has two distinct components:
In addition, in order to actually make use of any web service we implement, we need also to implement a client that constructs our request (based on the XML interface) and displays the result returned by the web service.
This view of a web service should help to explain the different approaches to producing a web service.
Implementation approaches
There are two basic approaches that are used to create web services from scratch.
The first approach is termed 'bottom-up'. This is where the code that implements the operations performed by the service is written first. Then the XML description (WSDL) of the service is produced and published in UDDI. The service code is placed inside a 'container' that provides the required interface for messaging (SOAP or something similar).
The second approach is to produce the XML description of the service before it is implemented. So the service is fully described in terms of what it does, how it can be called and what result(s) it returns, but is not actually implemented. This (WSDL) specification is then used as a guide to writing the code that implements the service. This approach is termed 'top-down'.
These two approaches are illustrated in Figure 1.
Figure 1 Bottom-up and top-down web service development
In short, the steps you will take to produce and test your first web service are as follows:
1 Create a project in the Eclipse workspace of type 'Dynamic Web Project', which will host your web service.
2 Write the Java code (the 'business logic') that implements your web service functionality.
3 Use Eclipse to automatically generate the components (WSDL etc.) that will transform the Java code into a web service, and then ask Eclipse to run that web service for you.
4 Create another project of type 'Dynamic Web Project', which will host the client application that you will use to access and test the web service.
5 Use Eclipse to automatically generate a set of web pages that function as a client interface to call the web service.
6 Use the web page client to send a request to the web service and observe the web service's response.
Application server setup
Before you can create and run an application such as a web service, you will need to set up an application server inside Eclipse. To set up the Tomcat server that the T software installation has provided, you should follow the guide Configuring an Application Server in Eclipse.
Once you have set up the application server, you can start working through the instructions given in the following sections.
Create a project
If Eclipse is not already open, open it by double-clicking on the 'Eclipse for Block 3' shortcut on your desktop. Ensure that you have the Project Explorer open in a Java EE perspective (Figure 3). If you are not currently in this perspective then use Window > Open Perspective and then select 'Java EE', which may be on the menu list or accessible under Other…, depending on your current perspective.
I shall not be going into the details of Java code and the many associated technologies. You might like to know that 'Java EE' is a reference to the Java programming language 'Enterprise Edition'. 'Enterprise' solutions are commonly thought of as reliable and scalable technologies.
The first step is to create a project that will be used to hold all the code and other elements that will make up your web service.
Select File > New > Project…, open up the 'Web' folder and select 'Dynamic Web Project' (Figure 4). Then click 'Next'. This will start the New Dynamic Web Project wizard, which will guide you through the project setup stages.
Figure 3 Eclipse Project Explorer in Java EE perspective
Figure 4 New Project dialogue box
Figure 6 Project Facets choice dialogue box
Figure 7 Web Module configuration dialogue box
At this point, if you have not already selected the Java EE perspective then Eclipse will offer to open this for you (Figure 8). If this happens then click 'Yes'.
Figure 8 Eclipse changes to the appropriate project perspective
Once the wizard has finished you will see that a project called 'Hello' has been created in the Project Explorer view. You can expand the project to view the components (Figure 9).
Figure 9 Dynamic project created
Writing the service logic
Now you will create the 'business logic' or processing code that performs the task behind the web service we want to deliver.
The Java code is very simple. Here it is with each line numbered for referencing purposes:
}
I shall not be asking you to write Java code yourself, but you will need to understand what the code used in this block does, at least in high-level terms. Below is a brief line-by-line description of the code, followed by a diagrammatic view. What is important at this stage is that you become familiar with the terminology used.
Now we need to introduce the code into the 'Hello' project. To do this we could add a package and then add a class to the package. However, a quicker approach is to add a class and specify that it belongs to a package; if the package doesn't exist then Eclipse will create the package so that it can put the class into it.
In the Eclipse Project Explorer, expand the 'Hello' project and right-click on the 'Java Resources: src' component, then select New. The pop-up menu shown in Figure 11 will be displayed.
Figure 11 New pop-up menu items for Java Resources sources
Select Class and you will be presented with the New Java Class dialogue box (Figure 12).
It is important that you enter a package name based on your OUCU. So if your OUCU is 'nks34', you might enter a package name of:
uk.ac.open.t320.nks
This will ensure that you have a unique package name, which may well become important to you later in the course.
Then enter the class name in the 'Name' field and click 'Finish'.
When the new class is created, you will see that the package and Java source file ('Hello.java') have been created in the project and that the Java file is opened for editing (Figure 13).
The 'Hello.java' file that is opened for editing contains the package heading you require and a skeleton for the 'Hello' class definition. Now copy the class code given earlier in this document into the editor and save the code using File > Save.
Figure 12 New Java Class dialogue box
Figure 13 Initial 'Hello' class
Figure 15 Selecting the Web Service wizard
Figure 16 Initial web service configuration
The upper section relates to the web service that you are creating. It names the class you are using and lists the server etc. you are going to use. Notice that the 'Web service type' is given as 'Bottom up Java bean Web Service', which indicates that you are producing a web service from the bottom up based on some Java code (our 'Hello' class). To the left is a slider control, which initially is set near to the maximum level with the text beside it saying 'Start service'. This slider controls what action Eclipse will take in terms of assembling, deploying, starting, etc. the web service.
The middle section allows you to specify that a client should be generated so that you can test the web service. Again this has a slider control, which can be used in the same way as that for the web service. This section is actually the same as you would have encountered had you chosen to create a new 'Web Service Client' in Figure 15.
The lower section contains two check boxes, which allow you to select whether the web service is to be published (to UDDI) or monitored.
As you selected the 'Hello.java' file before starting the web service wizard, it is assumed that this file will be the service implementation. If you didn't select this file then you will see that at the top of the dialogue box there is an error message 'Select a service implementation' (as shown in Figure 16), which will disappear once a service is selected.
If you see this error message then click on the 'Browse' button next to 'Service implementation'. This will bring up the dialogue box in Figure 17. If you start to type the first few characters of the class name 'Hello', you will see that Eclipse lists the classes that match the characters typed.
Figure 17 Selecting the service implementation class
In this case browse to and select the 'Hello' class in the package that you specified earlier. Your package name should include your OUCU as described earlier. Then click 'OK' to return to the Web Service configuration dialogue.
You can now set the slider controls. The slider control for configuring the web service can be left at the 'Start service' setting. Note that the 'Configuration' to the right of the slider specifies the Tomcat server you set up earlier and the 'Hello' project. You will be
If you are running Eclipse for the first time when also starting a Java process then you will usually see the dialogue box in Figure 19 warning you that Eclipse features are being blocked. You should click 'Unblock' and allow Eclipse to run the Java code.
Figure 19 Windows Firewall blocking Eclipse
When Eclipse has finished producing the web service and client, the configuration dialogue box will close and you will see that two new items have been added to the list in the Project Explorer (Figure 20).
Figure 20 Eclipse with generated client
The first is a Java project called 'HelloClient', which Eclipse created to hold the code for the client that has been generated.
The second is a new folder called 'JSR-109 Web Services'. This folder is intended as the location for any files generated that support the Java Specification Request (JSR) 109 (see http://jcp.org/en/jsr/detail?id=109 if you want further details). JSR-109 is a Java standard approach for encoding web services within the Java EE environment, but I shall not be concerned with this approach here as it would require an in-depth examination of Java techniques. The folder is actually empty and will not be used in this activity.
Eclipse will also have opened a web browser with a tab named Web Services Test Client. The web browser displays the client web pages as a frameset of three pages with headings 'Methods', 'Inputs' and 'Result'.
At the bottom of the Eclipse workspace you will also see that the Console tab is displaying several messages. These are messages concerning the Tomcat server and its status. Most of these messages are simply for information ('INFO'). It is important that there should be no errors reported; if there are errors reported then there may be a problem with your Tomcat installation. Warnings are not usually a problem, but if you see any errors then you should discuss these with your tutor.
Testing the web service
Using the client
You can now test the web service. The Methods pane contains a list of the actions that the web service can be asked to perform. The first three listed, 'getEndpoint()', 'setEndpoint(java.lang.String)' and 'getHello()', are actions that Eclipse generates by default so that certain aspects of the web service can be managed using the test client.
A web service endpoint in this case is the location (a URL) where the web service can be accessed on a network. A similar web services acronym, EPR, refers to the 'endpoint reference' of a service. You may also encounter terminology such as 'SOAP endpoint'. Again this refers to the network location where the web service can be accessed, in this case by sending a SOAP message to the web service.
I shall look at these first three methods later on, but for now you can ignore them.
The last method, 'helloHame (java.lang.String)', is the method defined by the Java code of the web service that you created earlier. If you click on this method in the Methods pane then you will see that the Inputs pane changes to display a text box labelled 'name'. All you need do is type a name into this text box, such as 'Nick' (Figure 21), and then click the 'Invoke' button. This will send the text you have entered to the web service via the proxy that Eclipse has generated and started.
Monitoring web service messages
At the bottom of the Eclipse window, you will see that both the request put to the service and the response from the service are displayed in the TCP/IP Monitor. Eclipse displays these messages all on one line, but here is the request laid out more usefully:
Nick
You can see that this is a SOAP envelope with a fairly simple body element. The application's own namespace is constructed using the Java package of the Java class. If a proper unique Java package name was used then this is acceptable as the URI.
The Java method name 'helloName' has been used as a SOAP tag enclosing another tag 'name', which is the name of the variable argument to the 'helloName' method.
These conventions, of course, are restricted to uses of Java but other programming languages can use similar practices.
The SOAP response is very similar, with only a slightly different body element:
Hello there Nick
The web service has used a simple convention of appending 'Response' to the request tag 'helloName' and has appended 'Return' to this to label the method's returned result.
Service descriptions
Web service endpoint
You have now completed the main steps for creating the simple 'Hello' web service. Before you shut down Eclipse, I want to take a quick look at some of the items that have been generated around the 'helloName' method you have invoked.
Earlier I noted that the Methods pane in Eclipse lists three methods other than the 'helloName' method that you created (see Figure 21). The endpoint of the web service is the URL where the service resides and can be accessed. If you click on 'getEndpoint()' and then on the 'Invoke' button, the URL will be displayed in the Result pane as http://localhost:8180/Hello/services/Hello.
Note: the port numbers used, such as 8180 in this case, may not be the same on your machine. Thus Eclipse may show different numbers in this and other cases illustrated in this document.
If you typed this URL into a browser you would be disappointed, because although the service resides at this location, the interface you are using is not available at this address. If you look at the value in the URL pull-down at the top of the Web Services Test Client pane (Figure 23) then you will see the actual HTTP URL used to display the test client. This URL is also shown in the top bar of the Eclipse window.
Figure 23 Display of the test client pages URL
In this case the full URL is:
http://localhost:8180/HelloClient/sampleHelloProxy/TestClient.jsp? endpoint=http://localhost:11368/Hello/services/Hello
The URL is that of a Java Server Page (JSP) called 'TestClient.jsp', which includes the frames you are seeing (Methods, Inputs and Result). A JSP web page is simply a web page with embedded Java code to provide additional functionality over HTML. Here, for example, the JSP code is taking the parameter off the end of the URL and using this to access the web service, passing over the SOAP message to the service. Other languages provide similar functionality.
At the end of the URL is a parameter that is passed to this JSP. The parameter is the URL of the endpoint, which is used by the JSP to actually access the web service when making a request. The URL http://localhost:11368/Hello/services/Hello is the URL for the web service via the proxy (the proxy is a Java class itself).
Copy the URL from the Web Services Test Client to your favourite web browser. Make sure that you leave Eclipse running as it was when you called the web service, so that Eclipse is still running the proxy and web service on Tomcat. You should see the same client pages displayed in the browser and be able to access the web service in the same way (Figure 24).
These client pages are very simple, but it should now be obvious that there is little difference between the service you have implemented and tested within Eclipse and a web service that is generally accessible.
We could take the JSP (web pages) generated for the client as a basic template for our own web pages and, although we have to run a proxy application, we could move the code onto a standalone Tomcat and run our 'Hello' application outside Eclipse. However. we will not do that as the proxy approach is a little clumsy for a web application outside Eclipse, and we can quite simply move the application to run on Tomcat without Eclipse support.