java 分布式代写 EIE4108 Distributed Systems and Cloud Computing

Department of Electronic & Information Engineering

The Hong Kong Polytechnic University

 

EIE4108 Distributed Systems and Cloud Computing

Lab: SOAP Web Services Based on JAX-RPC and JAX-WS

 

Objectives and Outcomes:

 

To enable students

  • to develop and consume JAX-PRC and JAX-WS web services using Eclipse as the development environment;
  • to deploy Web services on Windows OS and consume the web services using Java applications.

 

Submission:

 

Screen captures showing the output of your JAX-RPC and JAX-WS clients. The outputs should contain your name. See the example output in the procedure below. Put the screen capture in a Microsoft Word file and convert it to PDF. Submit the PDF file to Blackboard. Explanations of codes are NOT required.

 

Procedures:

 

The following procedures assume that you do not have admin right but you can write to C:\Temp. Remember to compress the folder C:\Temp\EIE4108 and copy the compressed file to your flash memory before leaving the lab. The procedure assumes that you are using Windows 8 in CF105 or CF503.

 

  1. Downloading and Installing Software

 

  1. Create the folder C:\Temp\EIE4108

 

  1. Start a browser and visit http://158.132.148.85:8080/download/EIE4108/SOAP

 

  1. Download Apache Tomcat 7 (apache-tomcat-7.0.85-windows-x64.zip) in the above webpage. Extract the .zip file to C:\Temp\EIE4108.

 

  1. Skip this step if you work in CF105/CF503. Otherwise, download Eclipse IDE for Java EE Developer (eclipse-jee-neon-3-win32-x86_64.zip). Extract the .zip file to C:\Temp\EIE4108.

 

 

  1. Setting UP Eclipse

 

  1. Start Eclipse by clicking exe in C:\eclipse. If it is the first time you start eclipse, it will prompt you to select a workspace. You may select C:\Temp\EIE4108\Workspace as your workspace. When you see Eclipse’s welcome page, click “Workbench” in the top-right corner.

 

  1. You need to tell Eclipse the location of JDK or JRE. Select Window à Preferences. On the left panel of the Preferences Window, expand Java and select “Installed JRE”. You should see something like the following:

 

Press “OK”. If not, you may add a JRE by clicking “Add” and follow the instructions.

 

  1. Setup Web Application Server. Select Window à Preferences. In the left panel, expand “Server” then select “Run Time Environment”. Press Add and follow the instruction to select
    Apache Tomcat 7.0. Select C:\Temp\EIE4108\apache-tomcat-7.0.85 as the installation directory of Tomcat.

 

  1. Create JAX-RPC Web Services

 

  1. Create a new Dynamic Web project called “ConverterWS” using File à New à Project à Web à Dynamic Web Project.

 

  1. Create a class called Converter by right-clicking the ConverterWS project and select New à Class. You should be able to see java under the Java Resources (default package) in the Project Explorer. Put the following code in “Converter.java”.

 

public class Converter {

public float celsiusToFarenheit ( float celsius ) {

return (celsius * 9 / 5) + 32;

}

public float farenheitToCelsius ( float farenheit ) {

return (farenheit – 32) * 5 / 9;

}

}

 

  1. Right-click java in the Project Explorer. Select New à Other à Web Services à Web Service. Then, click Next. Move the Service slider to the “Start Service” position and the Client slider to the “Test Client” position. Select “Monitor the Web service” and “Publish the Web service”.

 

 

 

  1. Click Finish. The wizard will assemble the Web service and Web Service Client Web Projects (ConvertWSClient), start Apache Tomcat, and deploy the web service to Tomcat. You should see the following in the Server Window of Eclipse.

 

 

  1. Once the service has been deployed and the test client has started up in Eclipse, you may select celsiusToFarenheit(float) and put “0” into the Celsius edit box. Press “Invoke”. You should see the following result.

 

 

  1. To redeploy the service, right-click “java” and select Web Services à Create Web Service. Then, slide the scroll bar to “Start service” and then click “Finish”. You may check whether the service has been deployed by accessing http://localhost:8080/ConverterWS/servlet/AxisServlet.

 

 

  1. To retest the service, expand ConverterWSClient à WebContent à sampleConverterProxy in the Project Explorer Window. Right click “jsp” and select Run As à Run on Server. Then, press “Finish”.

 

  1. Because you have selected the “Monitor Web Service” checkbox, a TCP/IP port is automatically created on the Tomcat server. When you invoke the service, the TCP/IP Monitor View will display the request and response SOAP messages. Click the TCP/IP Monitor View and select XML view in the drop-down combo to see the request and response messages. Click “Restore” icon to return.

 

  1. In the Project Explorer, expand ConverterWS à WebContent à wsdl. Double click “wsdl”. This file is automatically created by Eclipse based on Converter.java. Click “source” in the bottom-left corner and study the contents.
  2. Create a new class called “ConverterSoapClient” in the project “ConverterWSClient”. Add a main() method in java so that it becomes a Java application. In the main() method, create an object of ConverterProxy and invoke the method celisiusToFarenheit(double temp) and farenheitToCelsius(double temp) via the ConverterProxy object. The ConverterProxy class simplifies Web service invocation by providing methods that resembles the service interface. You may get the following screen snapshot when you run ConverterSoapClient.class. Capture your window and paste it to your report. Note that the screen snapshot should contain your name.

 

 

 

Deploy JAX-RPC Web Service to Tomcat and Run Tomcat Outside Eclipse

 

  1. To deploy the web applications or web services, you need to create a Web Archive (.war) file that contains all the files related to your web applications. To create a .war file, right click your Web Service project and select Export. Then, select Web à WAR file. Then, browse to C:\Temp\EIE4108\apache-tomcat-7.0.85\webapps, then press Finish.

 

  1. Open a Command Window and setup JAVA_HOME environment variable so that Tomcat will use the correct version of Java:

 

set JAVA_HOME=c:\Program Files\Java\jre1.8.0_141

 

  1. Start Tomcat by typing

 

cd C:\Temp\EIE4108\apache-tomcat-7.0.85\bin

startup.bat

 

Tomcat will automatically decompress the .war file and deploy your web service.

 

  1. Read the xml file under

 

C:\Temp\EIE4108\apache-tomcat-7.0.85\webapps\<YourWSdir>\WEB-INF

 

to find the URL pattern to access the WSDL file of your web service.

http://localhost:8080/<YourWSdir>/servlet/AxisServlet

From the WSDL file, find the access point of your web service.

 

  1. You may also access your web services via the Java client as follows:

 

cd C:\Temp\EIE4108\Workspace\<YourWSClientDir>\build\classes

java -cp  .;”..\..\WebContent\WEB-INF\lib\*” ConverterSoapClient

 

Screen capture for submission

 

Note that -cp is an option telling the JVM the locations for finding the class files required by the Java program. In this example, the classpath includes the current directory “.” And all .jar files in “..\..\WebContent\WEB-INF\lib”. These .jar files are automatically copied by Eclipse to the lib\ folder of your Java client project.

 

Capture the screen and put it into your report.

 

 

  1. Create JAX-WS Web Services

 

  1. Download “jaxws-ri-2.3.0.zip” from http://158.132.148.85:8080/download/EIE4108/SOAP. Decompress the file and copy all of the .jar files in the folder jaxws-ri\lib to C:\Temp\EIE4108\apache-tomcat-7.0.85\lib.

 

  1. Create a Dynamic Web Project called “ExampleJAX-WS”.

 

  1. Create a service endpoint interface and an implementation of that interface.

 

// calculator.CalculatorInterface.java

package calculator;

 

import javax.jws.WebMethod;

import javax.jws.WebService;

import javax.jws.soap.SOAPBinding;

import javax.jws.soap.SOAPBinding.Style;

 

@WebService

@SOAPBinding(style = Style.RPC)

public interface CalculatorInterface {

@WebMethod

int add(int i, int j);

}

 

// calculator.CalculatorImpl.java

package calculator;

import javax.jws.WebService;

 

@WebService(endpointInterface =

“calculator.CalculatorInterface”)

public class CalculatorImpl implements CalculatorInterface{

@Override

public int add(int i, int j) {

return i+j;

}

}

 

  1. Create web.xml and sun-jaxws.xml and put them under the WebContent/WEB-INF folder of your project. You may find these two files in http://158.132.148.85:8080/download/EIE4108/SOAP.

 

web.xml

 

sun-jaxws.xml

 

 

  1. Export “ExampleJAX-WS.war” to $TOMCAT_HOME/webapps and start Tomcat

 

  1. Check if the web service is working by accessing http://localhost:8080/ExampleJAX-WS/calculate

 

  1. Start a Command Window and set the path as follows so that you could use programs in the JDK.

 

set PATH=”c:\Program Files\Java\jdk1.8.0_131\bin”;%PATH

 

  1. Create a client proxy by executing the following command in the command window:

 

wsimport -keep -p client http://localhost:8080/ExampleJAX-WS/calculate?wsdl

 

  1. Create a JAX-WS Java client to produce the following result (refer to lecture notes for an example):

 

 

 

  1. Optional. Repeat the same procedure (Steps 1–31) on a Linux machine and access the web service from a Windows machine.

 

Remember to compress the folder C:\Temp\EIE4108 and copy the compressed file to your flash memory before leaving the lab.

 

 

 

 

— END —