Creation and Consumption of WebService using Soap in Java and Deploying it on Glassfish Server and adding Glassfish server to Netbeans IDE

Soap in java 



First we have check whether glassfish server is installed or not 
if it is not installed


Follow this STEPS 

Adding GlassFish Server as a Server in NetBeans IDE

To run the tutorial examples in NetBeans IDE, you must register your GlassFish Server installation as a NetBeans Server Instance. Follow these instructions to register the GlassFish Server in NetBeans IDE.

  1. Select Tools -> Server Manager to open the Servers dialog.
  2. Click Add Server.
  3. Under Server, select GlassFish v3 and click Next.
  4. Under Platform Location, browse to or enter the location of your GlassFish Server installation.
  5. Click Next.
  6. Under Domain, use the drop-down list to select an existing domain, type in the path to the domain directly in the field, or type the name of a new domain to create.
  7. Click Finish.
After installation and addition of Glassfish server in NetBeans IDE

Creating new project


select file new projectselect java web & web applicationclick next

give project name and location and click nextclick finish


Now select your project in project explorer
right click it
select new >> webservices


give name to your websevice and package name
finish



On new tab your webservice appears copy/write/type your program

Sample Calculator program
package com.compsciengg;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
/**
 *
 * @author Administrator
 */
@WebService()
public class Calculator{

    /**
     * Web service operation
     */
@WebMethod(operationName = "add")
public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
return i + j;
    }
@WebMethod(operationName = "Sub")
public int Sub(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
return i-j;
    }
@WebMethod(operationName = "Mul")
public int Mul(@WebParam(name = "i") int i, @WebParam(name = "j") int j) {
return i*j;
}
@WebMethod(operationName = "Div")
public int Div(@WebParam(name = "i") int i, @WebParam(name = "j") int j)
{        return i/j;
}

    }

Deploying the Web Service


Now that we’ve created the web service, we should see if it actually works. So to get the web service running on the server, follow these steps:
  1. Right click your project and select “Clean and Build”.
  2. After the projects been built, right click it again and select “Deploy”
The IDE will now launch an instance of Glassfish and put your web service up on it. Now we need to check that Glassfish has successfully deployed the service. To do this, we need to go to the console, which we access via a browser. Navigate to localhost:4848. Once the console is loaded, go to Applications on the sidebar.
 GlassFish
You should be able to see your java web service listed as an application.
applications

Click on the name of the web service to open the Edit Application. On the table at the bottom find the action called “View Endpoint”.
VieweEndpoint
From there, we go to a page with a couple of links. The first link takes you to the previous page. The second link offers a tester utility which allows us to test the web service without the need of creating a client application, it also allows us to view the raw SOAP XML. The third one allows us to look at the actual WSDL generated for the web service.
Go to the second link (the url ending in ?Tester) and click the first link (with the port 8080). You should see the following:

On clicking add button 

 add Method invocation



Method parameter(s)

TypeValue
int
10
int
10

Method returned

int : "20"

SOAP Request


<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <S:Body>
        <ns2:add xmlns:ns2="http://compsciengg.com/">
            <i>10</i>
            <j>10</j>
        </ns2:add>
    </S:Body>
</S:Envelope>

SOAP Response


<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <S:Body>
        <ns2:addResponse xmlns:ns2="http://compsciengg.com/">
            <return>20</return>
        </ns2:addResponse>
    </S:Body>
</S:Envelope>



Thank You for Reading


Labels: , ,