Cloud Computing

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 35

Cloud Computing

Practical No-1
Write a program for implementing client & server communication model using TCP.
1) Write a client server based program using TCP to display date and time from server.

Server program for date & time


package prac1;

import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateTimeServer {

public static void main(String[] args) {

try{

ServerSocket ss=new ServerSocket(8004);


System.out.println("server started");
Socket s=ss.accept();
Calendar c=new GregorianCalendar();
PrintWriter out=new PrintWriter(s.getOutputStream());
out.println(new Date());
out.println(c.get(Calendar.DATE));
out.flush();
s.close();

}catch(Exception e){

System.out.println("Error...."+ e);

Output:
Client program for date & time
package prac1;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;

public class DateTimeClient {

public static void main(String[] args) {

try{
Socket cs=new Socket(InetAddress.getLocalHost(),8004);
BufferedReader br=new BufferedReader(new InputStreamReader(cs.getInputStream()));
String userinput;
while((userinput=br.readLine())!=null) {
System.out.println(userinput);
}
cs.close();
}
catch(Exception e){
System.out.println("error............"+e);
}
}
}

Output:

2) Write a client server based program using TCP to find if the number enter is prime or
not.
Server Program:
package prac1;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class PrimeServer {

public static void main(String[] arg){

try{

ServerSocket ss = new ServerSocket(8001);


System.out.println("Server Started.........");
Socket s = ss.accept();

DataInputStream in = new DataInputStream(s.getInputStream());


int x = in.readInt();

DataOutputStream otc = new DataOutputStream(s.getOutputStream());


int y = x/2;

if(x==1 || x==2 || x==3){


otc.writeUTF(x+" is prime number");
System.exit(0);
}

for(int i=2; i<=y; i++){


if(x%2==0){
otc.writeUTF(x+ " is not prime number");
}
else{
otc.writeUTF(x+ " is prime number");
}
}

}catch(Exception e){
System.out.println("Error....."+ e);
}
}
}

Output:
Client Program:

package prac1;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class PrimeClient {

public static void main(String[] arg){

try{
Socket cs=new Socket("localhost",8001) ;
BufferedReader inf=new BufferedReader(new InputStreamReader(System.in) );
System.out.println("enter a number");

int a=Integer.parseInt(inf.readLine());

DataOutputStream out=new DataOutputStream(cs.getOutputStream());


out.writeInt(a);

DataInputStream in=new DataInputStream(cs.getInputStream());


System.out.println(in.readUTF());
cs.close();

}catch(Exception e){

System.out.println("Error......."+ e);
}
}
}

Output:

3) Write a client server based program using TCP to display chatting application between
client and server.
Server program for chat:
package prac1;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServer {

public static void main(String[] args) {

try{
ServerSocket ss=new ServerSocket(8003);
System.out.println("server started");
Socket s=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream out=new DataOutputStream(s.getOutputStream());
DataInputStream in=new DataInputStream(s.getInputStream());
String send,receive;
while((receive=in.readLine())!=null) {
if(receive.equals("STOP"))
break;

System.out.println("client says "+receive);


// System.out.println("Server says");
send=br.readLine();
out.writeBytes(send+"\n");
}
br.close();
in.close();
out.close();
s.close();

}catch(Exception e){
System.out.println("Error......."+ e);
}
}
}

Output:
Client program for chat:

package prac1;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;

public class ChatClient {

public static void main(String args[]){


try {
Socket cs=new Socket("localhost",8003);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
DataOutputStream out=new DataOutputStream(cs.getOutputStream());
DataInputStream in=new DataInputStream(cs.getInputStream());
System.out.println("to stop chatting type STOP");
// System.out.println("client says");
String msg;
while((msg=br.readLine())!=null){
out.writeBytes(msg+"\n");
if(msg.equals("STOP"))
break;
System.out.println("Server says"+in.readLine());
System.out.println("client says");
}
br.close();
in.close();
out.close();
cs.close();
}
catch(Exception e){
System.out.println("error............"+e);
}
}
}

Output:
Practical No-2
Write a program for implementing client server communication model using UDP.
1) A client server based program using UDP to find whether the entered number is even or odd .
SERVER PROGRAM TO CHECK ODD OR EVEN NUMBER
package prac2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPServer {


public static void main(String[] args) {
try {
DatagramSocket ds=new DatagramSocket(2000);
byte b[]=new byte[1024];
DatagramPacket dp=new DatagramPacket(b,b.length);
ds.receive(dp);
System.out.println("server started");
String str=new String(dp.getData(),0,dp.getLength());
System.out.println(str);
int a=Integer.parseInt((str));
String s=new String();
if(a%2==0)
s="NUMBER IS EVEN";
else
s="NUMBER IS ODD";

byte b1[]=new byte[1024];


b1=s.getBytes();
DatagramPacket dp1=new DatagramPacket
(b1,b1.length,InetAddress.getLocalHost(),1000);
ds.send(dp1);
}
catch(Exception e){
System.out.println("error!!!!!"+e);
}
}
}

Output:

CLIENT PROGRAM TO CHECK ODD OR EVEN NUMBER


package prac2;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPClient {


public static void main(String[] args) {
try {
DatagramSocket ds=new DatagramSocket(1000);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ENTER A NUMBER");
String num=br.readLine();
byte b[]=new byte[1024];
b=num.getBytes();
DatagramPacket dp=new DatagramPacket(b,b.length,InetAddress.getLocalHost(),2000);
ds.send(dp);
byte b1[]=new byte[1024];
DatagramPacket dp1=new DatagramPacket(b1,b1.length);
ds.receive(dp1);
String str=new String(dp1.getData(),0,dp1.getLength()) ;
System.out.println(str);
}
catch(Exception e){
System.out.println("error at client side ..."+e);
}
}
}

Output:
2) A program to implement simple calculator operations like as addition, subtraction,
multiplication & division.

SERVER PROGRAM FOR CALCULATOR


package prac2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.StringTokenizer;

public class CalculatorServer {

DatagramSocket ds;
DatagramPacket dp;
String str, methodName, result;
int val1, val2;

public CalculatorServer() {
try {
ds = new DatagramSocket(1200);
byte b[] = new byte[4096];
while (true) {
dp = new DatagramPacket(b, b.length);
ds.receive(dp);
str = new String(dp.getData(), 0, dp.getLength());
if (str.equalsIgnoreCase("q")) {
System.exit(1);
} else {
StringTokenizer st = new StringTokenizer(str, "
");
int i = 0;
while (st.hasMoreTokens()) {
String token = st.nextToken();
methodName = token;
val1 = Integer.parseInt(st.nextToken());
val2 = Integer.parseInt(st.nextToken());
}
}

System.out.println(str);
InetAddress ia = InetAddress.getLocalHost();
if (methodName.equalsIgnoreCase("add")) {
result = "" + add(val1, val2);
} else if (methodName.equalsIgnoreCase("sub")) {
result = "" + sub(val1, val2);
} else if (methodName.equalsIgnoreCase("mul")) {
result = "" + mult(val1, val2);
} else if (methodName.equalsIgnoreCase("div")) {
result = "" + div(val1, val2);
}
byte b1[] = result.getBytes();
DatagramSocket ds1 = new DatagramSocket();
DatagramPacket dp1 = new DatagramPacket(b1, b1.length,
InetAddress.getLocalHost(), 1300);
System.out.println("result : " + result + "\n");
ds1.send(dp1);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public int add(int val1, int val2) {


return val1 + val2;
}

public int sub(int val3, int val4) {


return val3 - val4;
}

public int mult(int val3, int val4) {


return val3 * val4;
}

public int div(int val3, int val4) {


return val3 / val4;
}

public static void main(String[] args) {

new CalculatorServer();
}

CLIENT PROGRAM FOR CALCULATOR

package prac2;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class CalculatorClient {

public CalculatorClient() {
try {
InetAddress ia = InetAddress.getLocalHost();
DatagramSocket ds = new DatagramSocket();
DatagramSocket ds1 = new DatagramSocket(1300);
System.out.println("\nRPC Client\n");
System.out.println("Enter method name and parameter like add
34\n");
while (true) {
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String str = br.readLine();
byte b[] = str.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length, ia,
1200);
ds.send(dp);
dp = new DatagramPacket(b, b.length);
ds1.receive(dp);
String s = new String(dp.getData(), 0, dp.getLength());
System.out.println("\nResult = " + s + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {


new CalculatorClient();
}
}

Output:
Server Output:

Client Output:
3) A program that finds the square, square root, cube and cube root of the entered
number.

Server Program:

package prac2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.StringTokenizer;

public class NumberServer {

DatagramSocket ds;
DatagramPacket dp;
String str, methodName, result;
int val;

public NumberServer() {
try {
ds = new DatagramSocket(1200);
byte b[] = new byte[4096];
while (true) {
dp = new DatagramPacket(b, b.length);
ds.receive(dp);
str = new String(dp.getData(), 0, dp.getLength());
if (str.equalsIgnoreCase("q")) {
System.exit(1);
} else {
StringTokenizer st = new StringTokenizer(str, "
");
int i = 0;
while (st.hasMoreTokens()) {
String token = st.nextToken();
methodName = token;
val = Integer.parseInt(st.nextToken());
}
}
System.out.println(str);
InetAddress ia = InetAddress.getLocalHost();
if (methodName.equalsIgnoreCase("square")) {
result = "" + square(val);
} else if (methodName.equalsIgnoreCase("squareroot")) {
result = "" + squareroot(val);
} else if (methodName.equalsIgnoreCase("cube")) {
result = "" + cube(val);
} else if (methodName.equalsIgnoreCase("cuberoot")) {
result = "" + cuberoot(val);
}
byte b1[] = result.getBytes();
DatagramSocket ds1 = new DatagramSocket();
DatagramPacket dp1 = new DatagramPacket(b1, b1.length,
InetAddress.getLocalHost(), 1300);
System.out.println("result : " + result + "\n");
ds1.send(dp1);
}
} catch (Exception e) {
e.printStackTrace();
}
}

public double square(int a) throws Exception {


double ans;
ans = a * a;
return ans;
}

public double squareroot(int a) throws Exception {


double ans;
ans = Math.sqrt(a);
return ans;
}

public double cube(int a) throws Exception {


double ans;
ans = a * a * a;
return ans;
}

public double cuberoot(int a) throws Exception {


double ans;
ans = Math.cbrt(a);
return ans;
}

public static void main(String[] args) {


new NumberServer();
}
}

Client Program:

package prac2;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class NumberClient {

public NumberClient() {
try
{
InetAddress ia = InetAddress.getLocalHost();
DatagramSocket ds = new DatagramSocket();
DatagramSocket ds1 = new DatagramSocket(1300);
System.out.println("\nRPC Client\n");
System.out.println("1. Square of the number - square\n2. Square root
of the number - squareroot\n3. Cube of the number - cube\n4. Cube root of the
number -cuberoot");
System.out.println("Enter method name and the number\n");
while (true)
{
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
byte b[] = str.getBytes();
DatagramPacket dp = new
DatagramPacket(b,b.length,ia,1200);
ds.send(dp);
dp = new DatagramPacket(b,b.length);
ds1.receive(dp);
String s = new String(dp.getData(),0,dp.getLength());
System.out.println("\nResult = " + s + "\n");
} }
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
new NumberClient();
}

}
Server Output:
Client Output:

4) A client server based program using UDP to find the factorial of the entered number.

Server Program:

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class udpServerFact {

public static void main(String[] args) {


try {
DatagramSocket ds = new DatagramSocket(2000);
byte b[] = new byte[1024];
DatagramPacket dp = new DatagramPacket(b, b.length);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(str);
int a = Integer.parseInt(str);
int f = 1, i;
String s = new String();
for (i = 1; i <= a; i++) {
f = f * i;
}
s = Integer.toString(f);
String str1 = "The Factorial of " + str + " is : " + f;
byte b1[] = new byte[1024];
b1 = str1.getBytes();
DatagramPacket dp1 = new DatagramPacket(b1, b1.length,
InetAddress.getLocalHost(), 1000);
ds.send(dp1);
} catch (Exception e) {
e.printStackTrace();
}
}

Client Program:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class udpClientFact {

public static void main(String args[]) {


try {
DatagramSocket ds = new DatagramSocket(1000);
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a number : ");
String num = br.readLine();
byte b[] = new byte[1024];
b = num.getBytes();
DatagramPacket dp = new DatagramPacket(b, b.length,
InetAddress.getLocalHost(), 2000);
ds.send(dp);
byte b1[] = new byte[1024];
DatagramPacket dp1 = new DatagramPacket(b1, b1.length);
ds.receive(dp1);
String str = new String(dp1.getData(), 0, dp1.getLength());
System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Server Output:

Client Output:

Practical No-3
Multicast Socket example
Server Program:
package prac3;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;

public class BroadcastServer {

public static final int PORT = 1234;

public static void main(String[] args) {


MulticastSocket socket;
DatagramPacket packet;
InetAddress address;

// set the multicast address to your local subnet


try {
address = InetAddress.getByName("239.1.2.3");
socket = new MulticastSocket();

// join a Multicast group and send the group messages


socket.joinGroup(address);
byte[] data = null;

for (;;) {
Thread.sleep(10000);
System.out.println("Sending ");
String str = ("This is Dimple Calling ");
data = str.getBytes();
packet = new DatagramPacket(data, str.length(),
address, PORT);
// Sends the packet
socket.send(packet);
} // end for
} catch (Exception e) {

System.out.println("Exception : " + e);


}

Client Program:
package prac3;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

public class BroadcastClient {

public static final int PORT = 1234;

public static void main(String[] args) throws Exception {


MulticastSocket socket;
DatagramPacket packet;
InetAddress address;
// set the mulitcast address to your local subnet
address = InetAddress.getByName("239.1.2.3");
socket = new MulticastSocket(PORT);
// join a Multicast group and wait for a message
socket.joinGroup(address);
byte[] data = new byte[100];
packet = new DatagramPacket(data, data.length);
for (;;) {
// receive the packets
socket.receive(packet);
String str = new String(packet.getData());
System.out.println("Message received from " +
packet.getAddress() + "Message is : " + str);
}
}

}
Server Output:

Client Output:

Practical No-4
Write a program to show the object communication using RMI.
A) A RMI based application program to display current date and time.
1. InterDate.java

package prac4;

import java.rmi.Remote;

public interface InterDate extends Remote{

public String display() throws Exception;


}

2. ServerDate.java
package prac4;

import java.rmi.*;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
public class ServerDate extends UnicastRemoteObject implements InterDate {

protected ServerDate() throws RemoteException {

@Override
public String display() throws Exception {
String str = "";
Date d = new Date();
str = d.toString();
return str;
}
public static void main(String args[]) throws Exception {
ServerDate s1 = new ServerDate();
Naming.bind("DS", s1);
System.out.println("Object registered.....");
}
}

3. ClientDate.java
package prac4;

import java.rmi.Naming;

public class ClientDate {

public static void main(String[] args) throws Exception {


String s1;
InterDate h1 = (InterDate)Naming.lookup("DS");
s1 = h1.display();
System.out.println(s1);
}

Output:
B) A RMI based application program that converts digits to words, e.g. 123 will be
converted to one two three.
1. InterConvert.java

import java.rmi.*;
public interface InterConvert extends Remote
{
public String convertDigit(String no) throws Exception;
}
2. ServerConvert.java

import java.rmi.*;
import java.rmi.server.*;
public class ServerConvert extends UnicastRemoteObject implements
InterConvert {
public ServerConvert() throws Exception
{
}
public String convertDigit(String no) throws Exception
{
String str = "";
for(int i = 0; i < no.length(); i++) {
int p = no.charAt(i);
if( p == 48)
{
str += "zero ";
}
if( p == 49)
{
str += "one ";
}
if( p == 50)
{
str += "two ";
}
if( p == 51)
{
str += "three ";
}
if( p == 52)
{
str += "four ";
}
if( p == 53)
{
str += "five ";
}
if( p == 54)
{
str += "six ";
}
if( p == 55)
{
str += "seven ";
}
if( p == 56)
{
str += "eight ";
}
if( p == 57)
{
str += "nine ";
}
}
return str;
}

public static void main(String args[]) throws Exception {


ServerConvert s1 = new ServerConvert();
Naming.bind("Wrd",s1);
System.out.println("Object registered....");
}
}

3. ClientConvert.java
import java.rmi.*;
import java.io.*;
public class ClientConvert
{
public static void main(String args[]) throws Exception {
InterConvert h1 = (InterConvert)Naming.lookup("Wrd"); BufferedReader
br = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter a number : \t"); String no = br.readLine();
String ans = h1.convertDigit(no);
System.out.println("The word representation of the entered digit is : " +ans);
}
}
Output:

Practical No-5
Aim: Show the implementation of web services.
A) Implementing “Big” Web Service.

1) Creating a Web Service

A. Choosing a Container:
1. Choose File > New Project. Select Web Application from the Java Web.
2. Name the project CalculatorWSApplication. Select a location for the project. Click Next.
3. Select your server and Java EE version and click Finish
B. Creating a Web Service from a Java Class
1. Right-click the CalculatorWSApplication node and choose New > Web Service.
2. Name the web service CalculatorWS and type org.me.calculator in Package. Leave Create
Web Service from Scratch selected. If you are creating a Java EE 6 project on GlassFish or
WebLogic, select Implement Web Service as a Stateless Session Bean.
3. Click Finish. The Projects window displays the structure of the new web service and the
source code is shown in the editor area.

2) Adding an Operation to the Web Service

A. To add an operation to the web service:


1. Change to the Design view in the editor.
2. Click Add Operation in either the visual designer or the context menu. The Add
Operation dialog opens.
3. In the upper part of the Add Operation dialog box, type add in Name and type int
in the Return Type drop-down list.
4. In the lower part of the Add Operation dialog box, click Add and create a
parameter of type int named i.
5. Click Add again and create a parameter of type int called j. You now see the
following:
6. Click OK at the bottom of the Add Operation dialog box. You return to the editor.
7. The visual designer now displays the following:
8. Click Source. And code the following.

Code:

@WebMethod(operationName = "add")
public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
int k = i + j;
return k;
}

3) Deploying and Testing the Web Service

A. To test successful deployment to a GlassFish or WebLogic server:


1. Right-click the project and choose Deploy. The IDE starts the application server, builds
the application, and deploys the application to the server
2. In the IDE's Projects tab, expand the Web Services node of the
CalculatorWSApplication project. Right-click the CalculatorWS node, and choose Test
Web Service.
3. The IDE opens the tester page in your browser, if you deployed a web application to
the GlassFish server.
4. If you deployed to the GlassFish server, type two numbers in the tester page, as
shown below:
5. The sum of the two numbers is displayed:

4) Consuming the Web Service Now that you have deployed the web service, you need
to create a client to make use of the web service's add method.
1. Client: Java Class in Java SE Application
1. Choose File > New Project. Select Java Application from the Java category. Name the
project CalculatorWS_Client_Application. Leave Create Main Class selected and
accept all other default settings. Click Finish.
2. Right-click the CalculatorWS_Client_Application node and choose New > Web
Service Client. The New Web Service Client wizard opens.
3. Select Project as the WSDL source. Click Browse. Browse to the CalculatorWS web
service in the CalculatorWSApplication project. When you have selected the web
service, click OK.
4. Do not select a package name. Leave this field empty
5. Leave the other settings at default and click Finish. The Projects window displays the
new web service client, with a node for the add method that you created:
6. Double-click your main class so that it opens in the Source Editor. Drag the add node
below the main() method.

You now see the following:


public static void main(String[] args) {
// TODO code application logic here } private static int add(int i, int j)
{ CalculatorWS_Service service = new
CalculatorWS_Service();
CalculatorWS port = service.getCalculatorWSPort();
return port.add(i, j);
}

7. In the main() method body, replace the TODO comment with code that initializes
values for i and j, calls add(), and prints the result.
public static void main(String[] args) {
int i = 3; int j = 4;
int result = add(i, j);
System.out.println("Result = " + result);
}

8. Surround the main() method code with a try/catch block that prints an exception.
public static void main(String[] args) {
try {
int i = 3;
int j = 4;
int result = add(i, j);
System.out.println("Result = " + result);
} catch (Exception ex) {
System.out.println("Exception: " + ex);
}
}

9. Right-click the project node and choose Run.

Output:
B) Implementing Web Service that connects to MySQL database.

1) Creating MySQL DB Table


create database bookshop;
use bookshop;

Create a table named Books that will store valid books information

create table books(isbn varchar(20) primary key, bookname varchar(100), bookprice


varchar(10));

Insert valid records in the Books table

insert into books values("111-222-333","Learn My SQL","250");

insert into books values("111-222-444","Java EE 6 for Beginners","850"); insert into books


values("111-222-555","Programming with Android","500");

insert into books values("111-222-666","Oracle Database for you","400");

insert into books values("111-222-777","Asp.Net for advanced programmers","1250");

2) Creating a web service

i. Choosing a container

 Web service can be either deployed in a Web container or in an EJB container.


 If a Java EE 6 application is created, use a Web container because EJBs can be placed
directly in a Web application.

ii. Creating a web application

 To create a Web application, select File - New Project.


 New Project dialog box appears. Select Java Web available under the Categories
section and Web Application available under the Projects section. Click Next.
 New Web Application dialog box appears. Enter BookWS as the project name in the
Project Name textbox and select the option Use Dedicated Folder for Storing
Libraries.
 Click Next. Server and Settings section of the New Web Application dialog box
appears. Choose the default i.e. GlassFish v3 Domain as the Web server, the Java EE
6 Web as the Java EE version and the Context Path.
 Click –Finish
 The Web application named BookWS is created.

iii. Creating a web service


Right-click the BookWS project and select New -> Web Service as shown in diagram.
New Web Service dialog box appears. Enter the name BookWS in the Web Service Name
textbox, webservice in the Package textbox, select the option Create Web Service from
scratch and also select the option implement web service as a stateless session bean as
shown in the diagram.

Click Finish.
The web service in the form of java class is ready.

3) Designing the web service


Now add an operation which will accept the ISBN number from the client to the web service.

i.Adding an operation to the web service


 Change the source view of the BookWS.java to design view by clicking Design
available just below the name of the BookWS.java tab.
 The window changes as shown in the diagram.
 Click Add Operation available in the design view of the web service.
 Add Operation dialog appears. Enter the name getBookDetails in the Name textbox
and java.lang.String in the Return Type textbox as shown in the diagram.
 In Add Operation dialog box, click Add and create a parameter of the type String
named isbn as shown in the diagram.
 Click Ok. The design view displays the operation added as shown in the diagram.
 Click Source. The code spec expands due to the operation added to the web service
as shown in the diagram.
 Modify the code spec of the web service BookWS.java.

Code Spec
package webservice;
import java.sql.*;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.ejb.Stateless;

@WebService()
@Stateless()
public class BookWS {
/** * Web service operation */
@WebMethod(operationName = "getBookDetails") public
String getBookDetails(@WebParam(name = "isbn") String isbn) {
//TODO write your implementation code here:

Connection dbcon = null;


Statement stmt = null;
ResultSet rs = null; String query = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
dbcon =
DriverManager.getConnection("jdbc:mysql://localhost/bookshop","root","root");
stmt = dbcon.createStatement();
query = "select * from books where isbn = '" +isbn+ "'";
rs = stmt.executeQuery(query); rs.next();
String bookDetails = "The name of the book is " +rs.getString("bookname") + " and
its cost is " +rs.getString("bookprice") + ".";
return bookDetails;
} catch(Exception e) {
System.out.println("Sorry failed to connect to the database.." + e.getMessage());
}
return null;

}
}

4) Adding the MySQL connector


 We need to add a reference of MySQL connector to our web service. It is via this
connector that our web service will be able to communicate with the database.
 Right click on the libraries and select Add JAR/Folder as shown in the diagram.
 Choose the location where mysql-coonector-java-5.1.10-bin is located, select it
and click on open as shown.

5) Deploying and testing the web service


6) Consuming the web service

i. Creating a web application(BookWSServletClient)


ii. Adding the web service to the client application
iii. Creating a servlet
iv. Creating an HTML form
v. Building the Web Application
vi. Running the Application
Practical No-6
Aim: Implement Xen virtualization and manage with Xen Center
Install XenServer in VMware Workstation and select Guest operating system as Linux.
Note IP Address – “ 192.168.124.137” ping it from command prompt.
 Now Install Citrix App if not installed
 Now Open Citrix XenCenter – and Click and Add Server

 Fill IP address copied from Installation and User name as “root” and Password as “root123”
which we had given during installation and Click on Add.
 Then click on Ok
 Now Click on New Storage
 Select Window File Sharing (CIFS) and click on next
 Uncheck Auto generate option Click on Next.
 Provide the path of shared windows XP image and enter local pc credential , click on Finish
 Click on New VM – and Windows XP SP3
 Select ISO file and click on next –
 Click on Next –
 Uncheck – Start the new VM and click on create now
 Now Right click on Windows XP and Start.
Practical No-7
1. Aim: Implement virtualization using VMWare ESXi Server and managing with
vCenter
Steps:
Install ESXi iso in VMWare workstation.

Install VMware vSphere Client

In vSphere create new Virtual Machine. Install Windows XP iso file and open it.
Practical No-8
Aim: Implement Windows Hyper V virtualization

First we have to uninstall vmware software if already installed on computer because the VMware
Workstation installer does not support running on a Hyper-V virtual machine. after uninstalling
vmware we can proceed to next step go to control panel and click on uninstall a program.

Click on Turn windows features on or off.


Now in windows features check on Hyper-V option.
After Restart Search for hyper-v manager in search box and click on that.

Select DESKTOP-7434D07->QUICK CREATE->Window 10MSIX packaging environment-> create virtual


machine

After downloading and virtual machine will start.

You might also like