Practical 4 SOA
Practical 4 SOA
Practical-4
AIM: Create web calculator service in NET Beans and consume it.
First , Open NetBeans IDE then Go to the file option in that click on new project.
In the New Project wizard, select Java Web and in that select Web Application,
and click Next.
En-223SBEIT30002
Now ,give the name to your project (myservice1) and click on the next
En-223SBEIT30002
After that select the server(GlassFish Server 4.1.1) and java EE version if not
Java EE 7 Web and then click on the next and then finish.
En-223SBEIT30002
Now, Right click on the project (myservice 1) and in that click on new and in that select
the option Web Service. It will create a web service for project.
En-223SBEIT30002
Now ,Name the web service(calcservice) and then give a package name (com.rashmi)
and then click on the finish.
En-223SBEIT30002
After that one folder name Web Services is created in the project and in that there is a web
service file name calcservice ,right click on that and click on option of Add Operation.
And then the window of Add Operation is open and in that add the details like name of
operation ,its return type and to add the parameters click on add option and then give name to
parameters and their type after that click on OK (it’s for the below figures).
Like above add more operation like multiplication,division etc for the calculator service.
En-223SBEIT30002
Calcservice.java
/*
*/
package com.rashmi;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
/**
En-223SBEIT30002
* @author student
*/
@WebService(serviceName = "calcservice")
/**
*/
@WebMethod(operationName = "add")
public int add(@WebParam(name = "num1") int num1, @WebParam(name = "num2") int num2) {
int sum;
sum=num1+num2;
return sum;
@WebMethod(operationName = "sub")
public int sub(@WebParam(name = "num1") int num1, @WebParam(name = "num2") int num2) {
int sum;
sum=num1-num2;
return sum;
@WebMethod(operationName = "mul")
public int mul(@WebParam(name = "num1") int num1, @WebParam(name = "num2") int num2) {
int sum;
sum=num1*num2;
En-223SBEIT30002
return sum;
@WebMethod(operationName = "div")
public int div(@WebParam(name = "num1") int num1, @WebParam(name = "num2") int num2) {
int sum;
sum=num1/num2;
return sum;
/**
*/
N
ow after adding operations make parameters to store result and then return the it.
En-223SBEIT30002
After that to run it we have to first right click on myservice1 project and in that click on the
clean and build option,after that if there is no any error or issues then it gives us “Build
Successful “ message.
En-223SBEIT30002
After that right click on myservice1 project and in that click on the Deploy option,after that if
there is no any error or issues then it gives us “Build Successful “ message.
En-223SBEIT30002
Then in our myservice1 project go to the web services folder in that right click on the
calcservice file and in that click on the Test Web Service option.after that the service is
open in the browser.
En-223SBEIT30002
En-223SBEIT30002
En-223SBEIT30002
Now right click on source package folder of myservice1 project and in that click on new and
select servlet.
After that a new servlet window will open and in that add class name (abc1) and click
on the next.
En-223SBEIT30002
After that we have to give servlet name (abc1) and click on add information to deployment
descriptor(web.xml) and then click on finish.
En-223SBEIT30002
Now right click on the myservice1 and in that select new option and click on the Web
Service Client, if you don’t get option then go to other.
En-223SBEIT30002
Now the new window will open for web service client and have to browse project so click on
the browse.
En-223SBEIT30002
And after clicking the browser option select the service whatever you want as for now we have
to select calcservice because in this our all operation are added so select it and click on ok
and then finish.
En-223SBEIT30002
After that the abc.java file will be open then we have to just add the all operation in
abc.java file and that as shown above and edit the html code as shown below.
En-223SBEIT30002
En-223SBEIT30002
Index.html
<!DOCTYPE html>
<!--
-->
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="abc">
</form>
</body>
En-223SBEIT30002
</html>
/*
*/
import com.calc.Calcservice_Service;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebServiceRef;
/**
* @author student
*/
@WebServlet(urlPatterns = {"/abc"})
@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/myservice1/calcservice.wsdl")
/**
* methods.
*/
response.setContentType("text/html;charset=UTF-8");
/* TODO output your page here. You may use following sample code. */
int num1,num2;
num1=Integer.parseInt(request.getParameter("num1"));
num2=Integer.parseInt(request.getParameter("num2")); out.println("<!
DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet abc</title>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");
En-223SBEIT30002
/**
*/
@Override
processRequest(request, response);
/**
*/
@Override
processRequest(request, response);
/**
*/
@Override
}// </editor-fold>
// Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe.
// If the calling of port operations may lead to race condition some synchronization is required.
// Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe.
// If the calling of port operations may lead to race condition some synchronization is required.
// Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe.
// If the calling of port operations may lead to race condition some synchronization is required.
// Note that the injected javax.xml.ws.Service reference as well as port objects are not thread safe.
// If the calling of port operations may lead to race condition some synchronization is required.
}
En-223SBEIT30002
After that go to the web pages and in that we got a index.html file thenright click on
index.html then click on the run file. Then the file will be open in the browser and give the
input data then click on submit .
En-223SBEIT30002