Multi-web services deployment on a Tomcat server
In this labs, we will create a service converting cm to inch and another service calculating the ideal weight (in kg) based on a height (in cm). These services contain two operations and are deployed on the same server. The response messages of the second service include two parameters: men's and women's weight.
Deploying the Converter WS
The deployment is somehow similar to the previous lab.
File → New → Dynamic Web Project. Name it as
MultiWS. Select
Web service module version as 2.5 and click on
modify in the
configuration tab to include
Axis2 web services.
Create a Java Package named tps.multi.ws under the Java Resources\src folder.
Create a new Java Class, named ConverterWS inside the tps.multi.ws package.
Copy and paste the following codes to the
ConverterWS.java file.
package tps.multi.ws;
public class ConverterWS {
public static double inch_to_cm (double inch){
return 2.54*inch;
}
public static double cm_to_inch (double cm){
return 0.3937*cm;
}
}
Save and right-click on the
ConverterWS, select
Web Services → Create Web Service. Select
Web service runtime: Apache axis and choose
Apache axis2. Click
Next→Next.
Start the Tomcat Server and Finish.
Right click on MultiWS project. Select Runs as→Run on server. Select Apache Tomcat v7.0. Click Next.
Make sure that MutliWS is in the Configured list and click Finish.
Select on services in the Welcome page. Select the ConverterWS to access the WSDL file.
To invoke the
cm_to_inch operation with the input
163, enter the following url
http://localhost:8080/MultiWS/services/ConverterWS/cm_to_inch?cm=163. You will get the following response:
Done.
Deploying the Ideal Weight Calculator WS
By the same way, under the package
tps.multi.ws, create a Java class named
IdealWeight and copy/paste the following codes into it.
package tps.multi.ws;
public class IdealWeight {
public double men_weight_in_kg;
public double women_weight_in_kg;
public double getMenWeight(){return men_weight_in_kg;}
public double getWomenWeight(){return women_weight_in_kg;}
}
Create another class named
IdealWeightWS to compute the ideal weight for men and women based on Devine's Formula and Miller Formula contains the codes (remember that the function names always begin with a small letter):
package tps.multi.ws;
public class IdealWeightWS {
public IdealWeight devineFormula(double height_in_cm){
IdealWeight iw = new IdealWeight();
iw.men_weight_in_kg = 50 + 2.3*(ConverterWS.cm_to_inch(height_in_cm)-60);
iw.women_weight_in_kg = 45.5 + 2.3*(ConverterWS.cm_to_inch(height_in_cm)-60);
return iw;
}
public IdealWeight millerFormula(double height_in_cm){
IdealWeight iw = new IdealWeight();
iw.men_weight_in_kg = 56.2 + 1.41*(ConverterWS.cm_to_inch(height_in_cm)-60);
iw.women_weight_in_kg = 53.1 + 1.36*(ConverterWS.cm_to_inch(height_in_cm)-60);
return iw;
}
}
Save and right click on the IdealWeightWS class, select Web Services → Create Web Service. Selecy Apache axis2 as Web service runtime. Click Finish.
Now, right click on MultiWS. Select Run as→Run on a server. Select services. You will see that there are two services ConverterWS and IdealWeightWS.
Click on
IdealWeightWS to access the WSDL file. Then test the
devineFormula operation with the input
162 by entering the
URL http://localhost:8080/MultiWS/services/IdealWeightWS/devineFormula?height_in_cm=162.
Deploying two created service on a Tomcat server
Export the
MultiWS project to a
WAR file, suppose
MultiWS.war.
Copy this WAR file to [your Tomcat]\webapps folder.
Restart the Tomcat. (if the parameter deployable in your Tomcat is set to “true”, you do need to do this step)
Now two service are running at: http://[Tomcat's address]:[port]/MultiWS/services/ConverterWS?wsdl and http://[Tomcat's address]:[port]/MultiWS/services/IdealWeightWS?wsdl. You can open these addresses by your web browser to verify them or using the Eclipse's Web Service Explorer open the created WSDL files.
Done.
Back to top