0% found this document useful (0 votes)
12 views29 pages

Practical 4 SOA

Uploaded by

quadrasquad44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views29 pages

Practical 4 SOA

Uploaded by

quadrasquad44
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 29

En-223SBEIT30002

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

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package com.rashmi;

import javax.jws.WebService;

import javax.jws.WebMethod;

import javax.jws.WebParam;

/**
En-223SBEIT30002

* @author student

*/

@WebService(serviceName = "calcservice")

public class calcservice {

/**

* Web service operation

*/

@WebMethod(operationName = "add")

public int add(@WebParam(name = "num1") int num1, @WebParam(name = "num2") int num2) {

//TODO write your implementation code here:

int sum;

sum=num1+num2;

return sum;

@WebMethod(operationName = "sub")

public int sub(@WebParam(name = "num1") int num1, @WebParam(name = "num2") int num2) {

//TODO write your implementation code here:

int sum;

sum=num1-num2;

return sum;

@WebMethod(operationName = "mul")

public int mul(@WebParam(name = "num1") int num1, @WebParam(name = "num2") int num2) {

//TODO write your implementation code here:

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) {

//TODO write your implementation code here:

int sum;

sum=num1/num2;

return sum;

/**

* This is a sample web service operation

*/


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>

<!--

To change this license header, choose License Headers in Project Properties.

To change this template file, choose Tools | Templates

and open the template in the editor.

-->

<html>

<head>

<title>TODO supply a title</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<div>TODO write content</div>

<form action="abc">

enter first number:<input type="text" name="num1" value=""/>

enter second number:<input type="text" name="num2" value=""/>

<input type="submit" value="submit"/>

</form>

</body>
En-223SBEIT30002

</html>

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

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"})

public class abc extends HttpServlet {

@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/myservice1/calcservice.wsdl")

private Calcservice_Service service;


En-223SBEIT30002

/**

* Processes requests for both HTTP <code>GET</code> and <code>POST</code>

* methods.

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

/* 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("<h1>addition of two numbers is: " + add(num1,num2) + "</h1>");

out.println("<h1>division of two numbers is: " + div(num1,num2) + "</h1>");

out.println("<h1>multiplication of two numbers is: " + mul(num1,num2) + "</h1>");

out.println("<h1>subtraction of two numbers is: " + sub(num1,num2) + "</h1>");

out.println("</body>");

out.println("</html>");
En-223SBEIT30002

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to


edit the code.">

/**

* Handles the HTTP <code>GET</code> method.

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

processRequest(request, response);

/**

* Handles the HTTP <code>POST</code> method.

* @param request servlet request

* @param response servlet response

* @throws ServletException if a servlet-specific error occurs

* @throws IOException if an I/O error occurs

*/

@Override

protected void doPost(HttpServletRequest request, HttpServletResponse response)


En-223SBEIT30002

throws ServletException, IOException {

processRequest(request, response);

/**

* Returns a short description of the servlet.

* @return a String containing servlet description

*/

@Override

public String getServletInfo() {

return "Short description";

}// </editor-fold>

private int add(int num1, int num2) {

// 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.

com.calc.Calcservice port = service.getCalcservicePort();

return port.add(num1, num2);

private int div(int num1, int num2) {

// 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.

com.calc.Calcservice port = service.getCalcservicePort();

return port.div(num1, num2);

private int mul(int num1, int num2) {


En-223SBEIT30002

// 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.

com.calc.Calcservice port = service.getCalcservicePort();

return port.mul(num1, num2);

private int sub(int num1, int num2) {

// 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.

com.calc.Calcservice port = service.getCalcservicePort();

return port.sub(num1, num2);

}
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

You might also like