0% found this document useful (0 votes)
9 views56 pages

Slips

The document provides a series of Java web service implementations, including factorial calculation, currency conversion, greeting users based on server time, temperature conversion, weight conversion, email validation, geometry calculations, and staff details retrieval from a database. Each service includes a server-side implementation and a corresponding client application to demonstrate usage. The examples illustrate various functionalities such as mathematical operations, string manipulations, and database interactions using Java's JAX-WS framework.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views56 pages

Slips

The document provides a series of Java web service implementations, including factorial calculation, currency conversion, greeting users based on server time, temperature conversion, weight conversion, email validation, geometry calculations, and staff details retrieval from a database. Each service includes a server-side implementation and a corresponding client application to demonstrate usage. The examples illustrate various functionalities such as mathematical operations, string manipulations, and database interactions using Java's JAX-WS framework.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 56

Slip 1 - CS-563-MJP Web Services Practical

Q1: Factorial Web Service


Web Service (Server Side) - FactorialService.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class FactorialService {

@WebMethod
public int getFactorial(int num) {
if (num < 0) return -1;
int result = 1;
for (int i = 2; i <= num; i++) {
result *= i;
}
return result;
}
}

Client Application - FactorialClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.FactorialService;

public class FactorialClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/FactorialWebService/FactorialService?wsdl");
QName qname = new QName("http://ws.com/", "FactorialServiceService");
Service service = Service.create(url, qname);
FactorialService fs = service.getPort(FactorialService.class);
int result = fs.getFactorial(5);
System.out.println("Factorial: " + result);
}
}
Q2: Currency Converter Web Service
Web Service (Server Side) - CurrencyConverter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class CurrencyConverter {

@WebMethod
public double convert(String type, double rupees) {
switch (type.toLowerCase()) {
case "dollar": return rupees / 83.0;
case "euro": return rupees / 90.0;
case "pound": return rupees / 103.0;
default: return -1.0;
}
}
}

Client Application - CurrencyClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.CurrencyConverter;

public class CurrencyClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/CurrencyConverterWebService/CurrencyConverter?wsdl");
QName qname = new QName("http://ws.com/", "CurrencyConverterService");
Service service = Service.create(url, qname);
CurrencyConverter cc = service.getPort(CurrencyConverter.class);
double result = cc.convert("dollar", 1000);
System.out.println("Converted amount: " + result);
}
}
Slip 2 - CS-563-MJP Web Services Practical
Q1: Greet User According to Server Time
Web Service (Server Side) - GreetingService.java

package com.ws;

import java.time.LocalTime;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class GreetingService {

@WebMethod
public String greetUser(String name) {
LocalTime time = LocalTime.now();
int hour = time.getHour();

String greeting;
if (hour < 12) {
greeting = "Good Morning";
} else if (hour < 18) {
greeting = "Good Afternoon";
} else {
greeting = "Good Evening";
}

return greeting + ", " + name + "!";


}
}

Client Application - GreetingClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.GreetingService;

public class GreetingClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/GreetingWebService/GreetingService?wsdl");
QName qname = new QName("http://ws.com/", "GreetingServiceService");
Service service = Service.create(url, qname);
GreetingService gs = service.getPort(GreetingService.class);
String message = gs.greetUser("Alice");
System.out.println("Response: " + message);
}
}

Q2: Convert Celsius to Fahrenheit


Web Service (Server Side) - TemperatureConverter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class TemperatureConverter {

@WebMethod
public double convertCtoF(double celsius) {
return (celsius * 9 / 5) + 32;
}
}

Client Application - TemperatureClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.TemperatureConverter;

public class TemperatureClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/TemperatureWebService/TemperatureConverter?wsdl");
QName qname = new QName("http://ws.com/", "TemperatureConverterService");
Service service = Service.create(url, qname);
TemperatureConverter tc = service.getPort(TemperatureConverter.class);
double result = tc.convertCtoF(25);
System.out.println("Fahrenheit: " + result);
}
}
Slip 3 - CS-563-MJP Web Services Practical
Q1: Convert Kilograms to Grams
Web Service (Server Side) - WeightConverter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class WeightConverter {

@WebMethod
public double kgToGram(double kg) {
return kg * 1000;
}
}

Client Application - WeightClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.WeightConverter;

public class WeightClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/WeightConverterWebService/WeightConverter?wsdl");
QName qname = new QName("http://ws.com/", "WeightConverterService");
Service service = Service.create(url, qname);
WeightConverter wc = service.getPort(WeightConverter.class);
double result = wc.kgToGram(5);
System.out.println("Grams: " + result);
}
}

Q2: Validate Email Address using Regex


Web Service (Server Side) - EmailValidator.java

package com.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class EmailValidator {

@WebMethod
public boolean validateEmail(String email) {
return email.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$");
}
}

Client Application - EmailClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.EmailValidator;

public class EmailClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/EmailValidatorWebService/EmailValidator?wsdl");
QName qname = new QName("http://ws.com/", "EmailValidatorService");
Service service = Service.create(url, qname);
EmailValidator ev = service.getPort(EmailValidator.class);
boolean isValid = ev.validateEmail("[email protected]");
System.out.println("Valid Email: " + isValid);
}
}
Slip 4 - CS-563-MJP Web Services Practical
Q1: Find Area and Volume of a Rectangle
Web Service (Server Side) - GeometryService.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class GeometryService {

@WebMethod
public double calculateArea(double length, double width) {
return length * width;
}

@WebMethod
public double calculateVolume(double length, double width, double height) {
return length * width * height;
}
}

Client Application - GeometryClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.GeometryService;

public class GeometryClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/GeometryWebService/GeometryService?wsdl");
QName qname = new QName("http://ws.com/", "GeometryServiceService");
Service service = Service.create(url, qname);
GeometryService gs = service.getPort(GeometryService.class);

double area = gs.calculateArea(5, 4);


double volume = gs.calculateVolume(5, 4, 3);

System.out.println("Area: " + area);


System.out.println("Volume: " + volume);
}
}

Q2: Select Staff Details from Database


Web Service (Server Side) - StaffService.java

package com.ws;

import java.sql.*;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class StaffService {

@WebMethod
public String getStaffDetails(String name) {
String output = "Staff Not Found";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/college", "root",
"password");
PreparedStatement ps = con.prepareStatement("SELECT * FROM staff WHERE sname = ?");
ps.setString(1, name);
ResultSet rs = ps.executeQuery();

if (rs.next()) {
output = "Staff No: " + rs.getInt("sno") +
", Name: " + rs.getString("sname") +
", Designation: " + rs.getString("designation") +
", Salary: " + rs.getDouble("salary");
}

con.close();
} catch (Exception e) {
output = "Error: " + e.getMessage();
}
return output;
}
}

Client Application - StaffClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.StaffService;

public class StaffClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/StaffDatabaseWebService/StaffService?wsdl");
QName qname = new QName("http://ws.com/", "StaffServiceService");
Service service = Service.create(url, qname);
StaffService ss = service.getPort(StaffService.class);

String details = ss.getStaffDetails("John");


System.out.println("Staff Details: " + details);
}
}
Slip 5 - CS-563-MJP Web Services Practical
Q1: Factorial of a Given Number
Web Service (Server Side) - FactorialService.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class FactorialService {

@WebMethod
public int getFactorial(int num) {
if (num < 0) return -1;
int result = 1;
for (int i = 2; i <= num; i++) {
result *= i;
}
return result;
}
}

Client Application - FactorialClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.FactorialService;

public class FactorialClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/FactorialWebService/FactorialService?wsdl");
QName qname = new QName("http://ws.com/", "FactorialServiceService");
Service service = Service.create(url, qname);
FactorialService fs = service.getPort(FactorialService.class);
int result = fs.getFactorial(6);
System.out.println("Factorial: " + result);
}
}
Q2: Return Price of a Stationary Item
Web Service (Server Side) - StationaryService.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;

@WebService
public class StationaryService {

private static final Map<String, Double> itemPrices = new HashMap<>();

static {
itemPrices.put("pen", 10.0);
itemPrices.put("pencil", 5.0);
itemPrices.put("notebook", 30.0);
itemPrices.put("eraser", 3.0);
itemPrices.put("marker", 20.0);
}

@WebMethod
public double getItemPrice(String itemName) {
return itemPrices.getOrDefault(itemName.toLowerCase(), -1.0);
}
}

Client Application - StationaryClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.StationaryService;

public class StationaryClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/StationaryWebService/StationaryService?wsdl");
QName qname = new QName("http://ws.com/", "StationaryServiceService");
Service service = Service.create(url, qname);
StationaryService ss = service.getPort(StationaryService.class);
double price = ss.getItemPrice("Pen");
System.out.println("Item Price: " + price);
}
}
Slip 6 - CS-563-MJP Web Services Practical
Q1: Count Vowels in a String
Web Service (Server Side) - VowelCounter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class VowelCounter {

@WebMethod
public int countVowels(String input) {
int count = 0;
input = input.toLowerCase();
for (char c : input.toCharArray()) {
if ("aeiou".indexOf(c) != -1) {
count++;
}
}
return count;
}
}

Client Application - VowelClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.VowelCounter;

public class VowelClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/VowelWebService/VowelCounter?wsdl");
QName qname = new QName("http://ws.com/", "VowelCounterService");
Service service = Service.create(url, qname);
VowelCounter vc = service.getPort(VowelCounter.class);
int count = vc.countVowels("Hello World");
System.out.println("Vowel Count: " + count);
}
}
Q2: Validate Username and Password
Web Service (Server Side) - LoginValidator.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;

@WebService
public class LoginValidator {

private static final Map<String, String> users = new HashMap<>();

static {
users.put("admin", "admin123");
users.put("user", "user123");
}

@WebMethod
public boolean validate(String username, String password) {
return users.containsKey(username) && users.get(username).equals(password);
}
}

Client Application - LoginClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.LoginValidator;

public class LoginClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/LoginWebService/LoginValidator?wsdl");
QName qname = new QName("http://ws.com/", "LoginValidatorService");
Service service = Service.create(url, qname);
LoginValidator lv = service.getPort(LoginValidator.class);
boolean valid = lv.validate("admin", "admin123");
System.out.println("Login Successful: " + valid);
}
}
Slip 7 - CS-563-MJP Web Services Practical
Q1: Convert Kilograms to Grams
Web Service (Server Side) - WeightConverter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class WeightConverter {

@WebMethod
public double kgToGram(double kg) {
return kg * 1000;
}
}

Client Application - WeightClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.WeightConverter;

public class WeightClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/WeightConverterWebService/WeightConverter?wsdl");
QName qname = new QName("http://ws.com/", "WeightConverterService");
Service service = Service.create(url, qname);
WeightConverter wc = service.getPort(WeightConverter.class);
double result = wc.kgToGram(7.5);
System.out.println("Grams: " + result);
}
}

Q2: Select Staff Details from Database


Web Service (Server Side) - StaffService.java

package com.ws;
import java.sql.*;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class StaffService {

@WebMethod
public String getStaffDetails(String name) {
String output = "Staff Not Found";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/college", "root",
"password");
PreparedStatement ps = con.prepareStatement("SELECT * FROM staff WHERE sname = ?");
ps.setString(1, name);
ResultSet rs = ps.executeQuery();

if (rs.next()) {
output = "Staff No: " + rs.getInt("sno") +
", Name: " + rs.getString("sname") +
", Designation: " + rs.getString("designation") +
", Salary: " + rs.getDouble("salary");
}

con.close();
} catch (Exception e) {
output = "Error: " + e.getMessage();
}
return output;
}
}

Client Application - StaffClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.StaffService;

public class StaffClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/StaffDatabaseWebService/StaffService?wsdl");
QName qname = new QName("http://ws.com/", "StaffServiceService");
Service service = Service.create(url, qname);
StaffService ss = service.getPort(StaffService.class);
String details = ss.getStaffDetails("Jane");
System.out.println("Staff Details: " + details);
}
}
Slip 8 - CS-563-MJP Web Services Practical
Q1: Find Area and Volume of a Rectangle
Web Service (Server Side) - GeometryService.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class GeometryService {

@WebMethod
public double calculateArea(double length, double width) {
return length * width;
}

@WebMethod
public double calculateVolume(double length, double width, double height) {
return length * width * height;
}
}

Client Application - GeometryClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.GeometryService;

public class GeometryClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/GeometryWebService/GeometryService?wsdl");
QName qname = new QName("http://ws.com/", "GeometryServiceService");
Service service = Service.create(url, qname);
GeometryService gs = service.getPort(GeometryService.class);

double area = gs.calculateArea(6, 5);


double volume = gs.calculateVolume(6, 5, 4);

System.out.println("Area: " + area);


System.out.println("Volume: " + volume);
}
}

Q2: Return Percentage of a Student


Web Service (Server Side) - PercentageCalculator.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class PercentageCalculator {

@WebMethod
public double calculatePercentage(int m1, int m2, int m3, int m4, int m5) {
int total = m1 + m2 + m3 + m4 + m5;
return (total / 500.0) * 100;
}
}

Client Application - PercentageClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.PercentageCalculator;

public class PercentageClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/PercentageWebService/PercentageCalculator?wsdl");
QName qname = new QName("http://ws.com/", "PercentageCalculatorService");
Service service = Service.create(url, qname);
PercentageCalculator pc = service.getPort(PercentageCalculator.class);

double percentage = pc.calculatePercentage(85, 90, 78, 88, 92);


System.out.println("Percentage: " + percentage);
}
}
Slip 9 - CS-563-MJP Web Services Practical
Q1: Greet User According to Server Time
Web Service (Server Side) - GreetingService.java

package com.ws;

import java.time.LocalTime;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class GreetingService {

@WebMethod
public String greetUser(String name) {
LocalTime time = LocalTime.now();
int hour = time.getHour();

String greeting;
if (hour < 12) {
greeting = "Good Morning";
} else if (hour < 18) {
greeting = "Good Afternoon";
} else {
greeting = "Good Evening";
}

return greeting + ", " + name + "!";


}
}

Client Application - GreetingClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.GreetingService;

public class GreetingClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/GreetingWebService/GreetingService?wsdl");
QName qname = new QName("http://ws.com/", "GreetingServiceService");
Service service = Service.create(url, qname);
GreetingService gs = service.getPort(GreetingService.class);
String message = gs.greetUser("Amit");
System.out.println("Response: " + message);
}
}

Q2: Validate Mobile Number Using Regex


Web Service (Server Side) - MobileValidator.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class MobileValidator {

@WebMethod
public boolean validateMobile(String mobile) {
return mobile.matches("^[0-9]{10}$");
}
}

Client Application - MobileClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.MobileValidator;

public class MobileClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/MobileWebService/MobileValidator?wsdl");
QName qname = new QName("http://ws.com/", "MobileValidatorService");
Service service = Service.create(url, qname);
MobileValidator mv = service.getPort(MobileValidator.class);
boolean isValid = mv.validateMobile("9876543210");
System.out.println("Valid Mobile Number: " + isValid);
}
}
Slip 10 - CS-563-MJP Web Services Practical
Q1: Convert Kilograms to Grams
Web Service (Server Side) - WeightConverter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class WeightConverter {

@WebMethod
public double kgToGram(double kg) {
return kg * 1000;
}
}

Client Application - WeightClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.WeightConverter;

public class WeightClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/WeightConverterWebService/WeightConverter?wsdl");
QName qname = new QName("http://ws.com/", "WeightConverterService");
Service service = Service.create(url, qname);
WeightConverter wc = service.getPort(WeightConverter.class);
double result = wc.kgToGram(10.0);
System.out.println("Grams: " + result);
}
}

Q2: Convert Rupees to Dollar, Pound, Euro


Web Service (Server Side) - CurrencyConverter.java

package com.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class CurrencyConverter {

@WebMethod
public double convert(String type, double rupees) {
switch (type.toLowerCase()) {
case "dollar": return rupees / 83.0;
case "euro": return rupees / 90.0;
case "pound": return rupees / 103.0;
default: return -1.0;
}
}
}

Client Application - CurrencyClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.CurrencyConverter;

public class CurrencyClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/CurrencyConverterWebService/CurrencyConverter?wsdl");
QName qname = new QName("http://ws.com/", "CurrencyConverterService");
Service service = Service.create(url, qname);
CurrencyConverter cc = service.getPort(CurrencyConverter.class);
double result = cc.convert("euro", 1000);
System.out.println("Converted amount in Euro: " + result);
}
}
Slip 11 - CS-563-MJP Web Services Practical
Q1: Count Vowels in a String
Web Service (Server Side) - VowelCounter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class VowelCounter {

@WebMethod
public int countVowels(String input) {
int count = 0;
input = input.toLowerCase();
for (char c : input.toCharArray()) {
if ("aeiou".indexOf(c) != -1) {
count++;
}
}
return count;
}
}

Client Application - VowelClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.VowelCounter;

public class VowelClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/VowelWebService/VowelCounter?wsdl");
QName qname = new QName("http://ws.com/", "VowelCounterService");
Service service = Service.create(url, qname);
VowelCounter vc = service.getPort(VowelCounter.class);
int count = vc.countVowels("Web Services Practical");
System.out.println("Vowel Count: " + count);
}
}
Q2: Decimal to Binary/Octal/Hex Converter
Web Service (Server Side) - NumberConverter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class NumberConverter {

@WebMethod
public String convert(int number, String type) {
switch (type.toLowerCase()) {
case "binary": return Integer.toBinaryString(number);
case "octal": return Integer.toOctalString(number);
case "hex": return Integer.toHexString(number).toUpperCase();
default: return "Invalid type";
}
}
}

Client Application - NumberClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.NumberConverter;

public class NumberClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/NumberConverterWebService/NumberConverter?wsdl");
QName qname = new QName("http://ws.com/", "NumberConverterService");
Service service = Service.create(url, qname);
NumberConverter nc = service.getPort(NumberConverter.class);
String binary = nc.convert(45, "binary");
System.out.println("Binary: " + binary);
}
}
Slip 12 - CS-563-MJP Web Services Practical
Q1: Find Area and Volume of a Rectangle
Web Service (Server Side) - GeometryService.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class GeometryService {

@WebMethod
public double calculateArea(double length, double width) {
return length * width;
}

@WebMethod
public double calculateVolume(double length, double width, double height) {
return length * width * height;
}
}

Client Application - GeometryClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.GeometryService;

public class GeometryClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/GeometryWebService/GeometryService?wsdl");
QName qname = new QName("http://ws.com/", "GeometryServiceService");
Service service = Service.create(url, qname);
GeometryService gs = service.getPort(GeometryService.class);

double area = gs.calculateArea(8, 6);


double volume = gs.calculateVolume(8, 6, 5);

System.out.println("Area: " + area);


System.out.println("Volume: " + volume);
}
}

Q2: Check Login Success or Fail (Using Database)


Web Service (Server Side) - LoginService.java

package com.ws;

import java.sql.*;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class LoginService {

@WebMethod
public boolean validateLogin(String username, String password) {
boolean isValid = false;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/college", "root",
"password");
PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE username=? AND
password=?");
ps.setString(1, username);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
isValid = rs.next();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
return isValid;
}
}

Client Application - LoginClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.LoginService;

public class LoginClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/LoginWebService/LoginService?wsdl");
QName qname = new QName("http://ws.com/", "LoginServiceService");
Service service = Service.create(url, qname);
LoginService ls = service.getPort(LoginService.class);

boolean success = ls.validateLogin("admin", "admin123");


System.out.println("Login Success: " + success);
}
}
Slip 13 - CS-563-MJP Web Services Practical
Q1: Count Vowels in a String
Web Service (Server Side) - VowelCounter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class VowelCounter {

@WebMethod
public int countVowels(String input) {
int count = 0;
input = input.toLowerCase();
for (char c : input.toCharArray()) {
if ("aeiou".indexOf(c) != -1) {
count++;
}
}
return count;
}
}

Client Application - VowelClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.VowelCounter;

public class VowelClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/VowelWebService/VowelCounter?wsdl");
QName qname = new QName("http://ws.com/", "VowelCounterService");
Service service = Service.create(url, qname);
VowelCounter vc = service.getPort(VowelCounter.class);
int count = vc.countVowels("Web Services Example");
System.out.println("Vowel Count: " + count);
}
}
Q2: Convert Rupees to Dollar, Pound, Euro
Web Service (Server Side) - CurrencyConverter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class CurrencyConverter {

@WebMethod
public double convert(String type, double rupees) {
switch (type.toLowerCase()) {
case "dollar": return rupees / 83.0;
case "euro": return rupees / 90.0;
case "pound": return rupees / 103.0;
default: return -1.0;
}
}
}

Client Application - CurrencyClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.CurrencyConverter;

public class CurrencyClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/CurrencyConverterWebService/CurrencyConverter?wsdl");
QName qname = new QName("http://ws.com/", "CurrencyConverterService");
Service service = Service.create(url, qname);
CurrencyConverter cc = service.getPort(CurrencyConverter.class);
double result = cc.convert("pound", 1500);
System.out.println("Converted amount in Pound: " + result);
}
}
Slip 14 - CS-563-MJP Web Services Practical
Q1: Factorial of a Given Number
Web Service (Server Side) - FactorialService.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class FactorialService {

@WebMethod
public int getFactorial(int num) {
if (num < 0) return -1;
int result = 1;
for (int i = 2; i <= num; i++) {
result *= i;
}
return result;
}
}

Client Application - FactorialClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.FactorialService;

public class FactorialClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/FactorialWebService/FactorialService?wsdl");
QName qname = new QName("http://ws.com/", "FactorialServiceService");
Service service = Service.create(url, qname);
FactorialService fs = service.getPort(FactorialService.class);
int result = fs.getFactorial(7);
System.out.println("Factorial: " + result);
}
}
Q2: Convert Celsius to Fahrenheit
Web Service (Server Side) - TemperatureConverter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class TemperatureConverter {

@WebMethod
public double convertCtoF(double celsius) {
return (celsius * 9 / 5) + 32;
}
}

Client Application - TemperatureClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.TemperatureConverter;

public class TemperatureClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/TemperatureWebService/TemperatureConverter?wsdl");
QName qname = new QName("http://ws.com/", "TemperatureConverterService");
Service service = Service.create(url, qname);
TemperatureConverter tc = service.getPort(TemperatureConverter.class);
double result = tc.convertCtoF(30);
System.out.println("Fahrenheit: " + result);
}
}
Slip 15 - CS-563-MJP Web Services Practical
Q1: Greet User According to Server Time
Web Service (Server Side) - GreetingService.java

package com.ws;

import java.time.LocalTime;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class GreetingService {

@WebMethod
public String greetUser(String name) {
LocalTime time = LocalTime.now();
int hour = time.getHour();

String greeting;
if (hour < 12) {
greeting = "Good Morning";
} else if (hour < 18) {
greeting = "Good Afternoon";
} else {
greeting = "Good Evening";
}

return greeting + ", " + name + "!";


}
}

Client Application - GreetingClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.GreetingService;

public class GreetingClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/GreetingWebService/GreetingService?wsdl");
QName qname = new QName("http://ws.com/", "GreetingServiceService");
Service service = Service.create(url, qname);
GreetingService gs = service.getPort(GreetingService.class);
String message = gs.greetUser("Neha");
System.out.println("Response: " + message);
}
}

Q2: Validate Email Address using Regex


Web Service (Server Side) - EmailValidator.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class EmailValidator {

@WebMethod
public boolean validateEmail(String email) {
return email.matches("^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+$");
}
}

Client Application - EmailClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.EmailValidator;

public class EmailClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/EmailValidatorWebService/EmailValidator?wsdl");
QName qname = new QName("http://ws.com/", "EmailValidatorService");
Service service = Service.create(url, qname);
EmailValidator ev = service.getPort(EmailValidator.class);
boolean isValid = ev.validateEmail("[email protected]");
System.out.println("Valid Email: " + isValid);
}
}
Slip 16 - CS-563-MJP Web Services Practical
Q1: Convert Kilograms to Grams
Web Service (Server Side) - WeightConverter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class WeightConverter {

@WebMethod
public double kgToGram(double kg) {
return kg * 1000;
}
}

Client Application - WeightClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.WeightConverter;

public class WeightClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/WeightConverterWebService/WeightConverter?wsdl");
QName qname = new QName("http://ws.com/", "WeightConverterService");
Service service = Service.create(url, qname);
WeightConverter wc = service.getPort(WeightConverter.class);
double result = wc.kgToGram(15.5);
System.out.println("Grams: " + result);
}
}

Q2: Return Price of a Stationary Item


Web Service (Server Side) - StationaryService.java

package com.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;

@WebService
public class StationaryService {

private static final Map<String, Double> itemPrices = new HashMap<>();

static {
itemPrices.put("pen", 10.0);
itemPrices.put("pencil", 5.0);
itemPrices.put("notebook", 30.0);
itemPrices.put("eraser", 3.0);
itemPrices.put("sharpener", 7.0);
}

@WebMethod
public double getItemPrice(String itemName) {
return itemPrices.getOrDefault(itemName.toLowerCase(), -1.0);
}
}

Client Application - StationaryClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.StationaryService;

public class StationaryClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/StationaryWebService/StationaryService?wsdl");
QName qname = new QName("http://ws.com/", "StationaryServiceService");
Service service = Service.create(url, qname);
StationaryService ss = service.getPort(StationaryService.class);
double price = ss.getItemPrice("Notebook");
System.out.println("Item Price: " + price);
}
}
Slip 17 - CS-563-MJP Web Services Practical
Q1: Find Area and Volume of a Rectangle
Web Service (Server Side) - GeometryService.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class GeometryService {

@WebMethod
public double calculateArea(double length, double width) {
return length * width;
}

@WebMethod
public double calculateVolume(double length, double width, double height) {
return length * width * height;
}
}

Client Application - GeometryClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.GeometryService;

public class GeometryClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/GeometryWebService/GeometryService?wsdl");
QName qname = new QName("http://ws.com/", "GeometryServiceService");
Service service = Service.create(url, qname);
GeometryService gs = service.getPort(GeometryService.class);

double area = gs.calculateArea(4, 3);


double volume = gs.calculateVolume(4, 3, 2);
System.out.println("Area: " + area);
System.out.println("Volume: " + volume);
}
}

Q2: Validate Mobile Number Using Regex


Web Service (Server Side) - MobileValidator.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class MobileValidator {

@WebMethod
public boolean validateMobile(String mobile) {
return mobile.matches("^[0-9]{10}$");
}
}

Client Application - MobileClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.MobileValidator;

public class MobileClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/MobileWebService/MobileValidator?wsdl");
QName qname = new QName("http://ws.com/", "MobileValidatorService");
Service service = Service.create(url, qname);
MobileValidator mv = service.getPort(MobileValidator.class);
boolean isValid = mv.validateMobile("9876543210");
System.out.println("Valid Mobile Number: " + isValid);
}
}
Slip 18 - CS-563-MJP Web Services Practical
Q1: Count Vowels in a String
Web Service (Server Side) - VowelCounter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class VowelCounter {

@WebMethod
public int countVowels(String input) {
int count = 0;
input = input.toLowerCase();
for (char c : input.toCharArray()) {
if ("aeiou".indexOf(c) != -1) {
count++;
}
}
return count;
}
}

Client Application - VowelClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.VowelCounter;

public class VowelClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/VowelWebService/VowelCounter?wsdl");
QName qname = new QName("http://ws.com/", "VowelCounterService");
Service service = Service.create(url, qname);
VowelCounter vc = service.getPort(VowelCounter.class);
int count = vc.countVowels("Web Services Project");
System.out.println("Vowel Count: " + count);
}
}
Q2: Return Percentage of a Student
Web Service (Server Side) - PercentageCalculator.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class PercentageCalculator {

@WebMethod
public double calculatePercentage(int m1, int m2, int m3, int m4, int m5) {
int total = m1 + m2 + m3 + m4 + m5;
return (total / 500.0) * 100;
}
}

Client Application - PercentageClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.PercentageCalculator;

public class PercentageClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/PercentageWebService/PercentageCalculator?wsdl");
QName qname = new QName("http://ws.com/", "PercentageCalculatorService");
Service service = Service.create(url, qname);
PercentageCalculator pc = service.getPort(PercentageCalculator.class);
double percentage = pc.calculatePercentage(80, 85, 78, 90, 88);
System.out.println("Percentage: " + percentage);
}
}
Slip 19 - CS-563-MJP Web Services Practical
Q1: Greet User According to Server Time
Web Service (Server Side) - GreetingService.java

package com.ws;

import java.time.LocalTime;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class GreetingService {

@WebMethod
public String greetUser(String name) {
LocalTime time = LocalTime.now();
int hour = time.getHour();

String greeting;
if (hour < 12) {
greeting = "Good Morning";
} else if (hour < 18) {
greeting = "Good Afternoon";
} else {
greeting = "Good Evening";
}

return greeting + ", " + name + "!";


}
}

Client Application - GreetingClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.GreetingService;

public class GreetingClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/GreetingWebService/GreetingService?wsdl");
QName qname = new QName("http://ws.com/", "GreetingServiceService");
Service service = Service.create(url, qname);
GreetingService gs = service.getPort(GreetingService.class);
String message = gs.greetUser("Ravi");
System.out.println("Response: " + message);
}
}

Q2: Decimal to Binary/Octal/Hex Converter


Web Service (Server Side) - NumberConverter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class NumberConverter {

@WebMethod
public String convert(int number, String type) {
switch (type.toLowerCase()) {
case "binary": return Integer.toBinaryString(number);
case "octal": return Integer.toOctalString(number);
case "hex": return Integer.toHexString(number).toUpperCase();
default: return "Invalid type";
}
}
}

Client Application - NumberClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.NumberConverter;

public class NumberClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/NumberConverterWebService/NumberConverter?wsdl");
QName qname = new QName("http://ws.com/", "NumberConverterService");
Service service = Service.create(url, qname);
NumberConverter nc = service.getPort(NumberConverter.class);

String binary = nc.convert(45, "binary");


String octal = nc.convert(45, "octal");
String hex = nc.convert(45, "hex");

System.out.println("Binary: " + binary);


System.out.println("Octal: " + octal);
System.out.println("Hexadecimal: " + hex);
}
}
Slip 20 - CS-563-MJP Web Services Practical
Q1: Factorial of a Given Number
Web Service (Server Side) - FactorialService.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class FactorialService {

@WebMethod
public int getFactorial(int num) {
if (num < 0) return -1;
int result = 1;
for (int i = 2; i <= num; i++) {
result *= i;
}
return result;
}
}

Client Application - FactorialClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.FactorialService;

public class FactorialClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/FactorialWebService/FactorialService?wsdl");
QName qname = new QName("http://ws.com/", "FactorialServiceService");
Service service = Service.create(url, qname);
FactorialService fs = service.getPort(FactorialService.class);
int result = fs.getFactorial(8);
System.out.println("Factorial: " + result);
}
}
Q2: Validate Mobile Number Using Regex
Web Service (Server Side) - MobileValidator.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class MobileValidator {

@WebMethod
public boolean validateMobile(String mobile) {
return mobile.matches("^[0-9]{10}$");
}
}

Client Application - MobileClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.MobileValidator;

public class MobileClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/MobileWebService/MobileValidator?wsdl");
QName qname = new QName("http://ws.com/", "MobileValidatorService");
Service service = Service.create(url, qname);
MobileValidator mv = service.getPort(MobileValidator.class);
boolean isValid = mv.validateMobile("9876543210");
System.out.println("Valid Mobile Number: " + isValid);
}
}
Slip 21 - CS-563-MJP Web Services Practical
Q1: Convert Kilograms to Grams
Web Service (Server Side) - WeightConverter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class WeightConverter {

@WebMethod
public double kgToGram(double kg) {
return kg * 1000;
}
}

Client Application - WeightClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.WeightConverter;

public class WeightClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/WeightConverterWebService/WeightConverter?wsdl");
QName qname = new QName("http://ws.com/", "WeightConverterService");
Service service = Service.create(url, qname);
WeightConverter wc = service.getPort(WeightConverter.class);
double result = wc.kgToGram(12.5);
System.out.println("Grams: " + result);
}
}

Q2: Convert Celsius to Fahrenheit


Web Service (Server Side) - TemperatureConverter.java

package com.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class TemperatureConverter {

@WebMethod
public double convertCtoF(double celsius) {
return (celsius * 9 / 5) + 32;
}
}

Client Application - TemperatureClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.TemperatureConverter;

public class TemperatureClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/TemperatureWebService/TemperatureConverter?wsdl");
QName qname = new QName("http://ws.com/", "TemperatureConverterService");
Service service = Service.create(url, qname);
TemperatureConverter tc = service.getPort(TemperatureConverter.class);
double result = tc.convertCtoF(37);
System.out.println("Fahrenheit: " + result);
}
}
Slip 22 - CS-563-MJP Web Services Practical
Q1: Count Vowels in a String
Web Service (Server Side) - VowelCounter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class VowelCounter {

@WebMethod
public int countVowels(String input) {
int count = 0;
input = input.toLowerCase();
for (char c : input.toCharArray()) {
if ("aeiou".indexOf(c) != -1) {
count++;
}
}
return count;
}
}

Client Application - VowelClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.VowelCounter;

public class VowelClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/VowelWebService/VowelCounter?wsdl");
QName qname = new QName("http://ws.com/", "VowelCounterService");
Service service = Service.create(url, qname);
VowelCounter vc = service.getPort(VowelCounter.class);
int count = vc.countVowels("Web Services Practical Slip");
System.out.println("Vowel Count: " + count);
}
}
Q2: Validate Mobile Number Using Regex
Web Service (Server Side) - MobileValidator.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class MobileValidator {

@WebMethod
public boolean validateMobile(String mobile) {
return mobile.matches("^[0-9]{10}$");
}
}

Client Application - MobileClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.MobileValidator;

public class MobileClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/MobileWebService/MobileValidator?wsdl");
QName qname = new QName("http://ws.com/", "MobileValidatorService");
Service service = Service.create(url, qname);
MobileValidator mv = service.getPort(MobileValidator.class);
boolean isValid = mv.validateMobile("9876543210");
System.out.println("Valid Mobile Number: " + isValid);
}
}
Slip 23 - CS-563-MJP Web Services Practical
Q1: Greet User According to Server Time
Web Service (Server Side) - GreetingService.java

package com.ws;

import java.time.LocalTime;
import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class GreetingService {

@WebMethod
public String greetUser(String name) {
LocalTime time = LocalTime.now();
int hour = time.getHour();

String greeting;
if (hour < 12) {
greeting = "Good Morning";
} else if (hour < 18) {
greeting = "Good Afternoon";
} else {
greeting = "Good Evening";
}

return greeting + ", " + name + "!";


}
}

Client Application - GreetingClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.GreetingService;

public class GreetingClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/GreetingWebService/GreetingService?wsdl");
QName qname = new QName("http://ws.com/", "GreetingServiceService");
Service service = Service.create(url, qname);
GreetingService gs = service.getPort(GreetingService.class);
String message = gs.greetUser("Sita");
System.out.println("Response: " + message);
}
}

Q2: Return Price of a Stationary Item


Web Service (Server Side) - StationaryService.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;
import java.util.HashMap;
import java.util.Map;

@WebService
public class StationaryService {

private static final Map<String, Double> prices = new HashMap<>();

static {
prices.put("pen", 10.0);
prices.put("pencil", 5.0);
prices.put("notebook", 30.0);
}

@WebMethod
public double getItemPrice(String item) {
return prices.getOrDefault(item.toLowerCase(), -1.0);
}
}

Client Application - StationaryClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.StationaryService;

public class StationaryClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/StationaryWebService/StationaryService?wsdl");
QName qname = new QName("http://ws.com/", "StationaryServiceService");
Service service = Service.create(url, qname);
StationaryService ss = service.getPort(StationaryService.class);
double price = ss.getItemPrice("Notebook");
System.out.println("Item Price: " + price);
}
}
Slip 24 - CS-563-MJP Web Services Practical
Q1: Factorial of a Given Number
Web Service (Server Side) - FactorialService.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class FactorialService {

@WebMethod
public int getFactorial(int num) {
if (num < 0) return -1;
int result = 1;
for (int i = 2; i <= num; i++) {
result *= i;
}
return result;
}
}

Client Application - FactorialClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.FactorialService;

public class FactorialClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/FactorialWebService/FactorialService?wsdl");
QName qname = new QName("http://ws.com/", "FactorialServiceService");
Service service = Service.create(url, qname);
FactorialService fs = service.getPort(FactorialService.class);
int result = fs.getFactorial(6);
System.out.println("Factorial: " + result);
}
}
Q2: Convert Celsius to Fahrenheit
Web Service (Server Side) - TemperatureConverter.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class TemperatureConverter {

@WebMethod
public double convertCtoF(double celsius) {
return (celsius * 9 / 5) + 32;
}
}

Client Application - TemperatureClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.TemperatureConverter;

public class TemperatureClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/TemperatureWebService/TemperatureConverter?wsdl");
QName qname = new QName("http://ws.com/", "TemperatureConverterService");
Service service = Service.create(url, qname);
TemperatureConverter tc = service.getPort(TemperatureConverter.class);
double result = tc.convertCtoF(32);
System.out.println("Fahrenheit: " + result);
}
}
Slip 25 - CS-563-MJP Web Services Practical
Q1: Area and Volume of a Rectangle
Web Service (Server Side) - GeometryService.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class GeometryService {

@WebMethod
public double calculateArea(double length, double width) {
return length * width;
}

@WebMethod
public double calculateVolume(double length, double width, double height) {
return length * width * height;
}
}

Client Application - GeometryClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.GeometryService;

public class GeometryClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/GeometryWebService/GeometryService?wsdl");
QName qname = new QName("http://ws.com/", "GeometryServiceService");
Service service = Service.create(url, qname);
GeometryService gs = service.getPort(GeometryService.class);
double area = gs.calculateArea(6, 4);
double volume = gs.calculateVolume(6, 4, 3);
System.out.println("Area: " + area);
System.out.println("Volume: " + volume);
}
}
Q2: Return Percentage of a Student
Web Service (Server Side) - PercentageCalculator.java

package com.ws;

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService
public class PercentageCalculator {

@WebMethod
public double calculatePercentage(int m1, int m2, int m3, int m4, int m5) {
int total = m1 + m2 + m3 + m4 + m5;
return (total / 500.0) * 100;
}
}

Client Application - PercentageClient.java

package com.client;

import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.ws.PercentageCalculator;

public class PercentageClient {


public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:8080/PercentageWebService/PercentageCalculator?wsdl");
QName qname = new QName("http://ws.com/", "PercentageCalculatorService");
Service service = Service.create(url, qname);
PercentageCalculator pc = service.getPort(PercentageCalculator.class);
double percentage = pc.calculatePercentage(85, 90, 80, 88, 95);
System.out.println("Percentage: " + percentage);
}
}

You might also like