Web Services using JAX-WS

Steps
We have to make 2 projects one is Service Provider and another is Service Consumer(Client)
For Service
1. Make a simple WebProject.
2. Right click on project select New -> WebSerice and give name and package to the webservice
3. It will create a WebService Class so now go in design and add operation and give method name,return type and parameters to the method
.Wou can add multiple operations(methods)
For Client
1. Make a Another new Project and go to new and make web service client and add link but change tester to wsdl
2. Right click in jsp of client and click on call webservice operation to add code.
3. Sample Link which is to be provided to make client http://127.0.0.1:8080/WS_Service/CalcWebService?Tester(Tester must be replaced with WSDL)

Screen Shots For Service
 



package com.mywebservice;

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


@WebService(serviceName = "CalcWebService")
public class CalcWebService {

    
    @WebMethod(operationName = "calculateDigits")
    public Double calculateDigits(@WebParam(name = "a") Double a, @WebParam(name = "b") Double b, @WebParam(name = "operator") String operator) {
        Double ans = null;
        if (operator.equals("+")) {
            ans = a + b;
        } else if (operator.equals("-")) {
            ans = a - b;

        } else if (operator.equals("*")) {
            ans = a * b;

        } else if (operator.equals("/")) {
            ans = a / b;
        }
        return ans;
    }
}





<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <%
        java.lang.Double result = null;
        if ("Submit".equals(request.getParameter("submit"))) {
            try {
                com.mywebservice.CalcWebService_Service service = new com.mywebservice.CalcWebService_Service();
                com.mywebservice.CalcWebService port = service.getCalcWebServicePort();
                // TODO initialize WS operation arguments here
                java.lang.Double a = Double.parseDouble(request.getParameter("a"));
                java.lang.Double b = Double.parseDouble(request.getParameter("b"));
                java.lang.String operator = request.getParameter("operator");
                // TODO process result here
                result = port.calculateDigits(a, b, operator);

            } catch (Exception ex) {
                // TODO handle custom exceptions here
            }
        }
    %>    
    <body>
        <form method="post">   
            <table>
                <tr>
                    <td>A : </td>
                    <td>
                        <input type="text" name="a"/>
                    </td>
                </tr>
                <tr>
                    <td>B : </td>
                    <td>
                        <input type="text" name="b"/>
                    </td>
                </tr>
                <tr>
                    <td>Operator : </td>
                    <td>
                        <select name="operator">
                            <option value="+">+</option>
                            <option value="-">-</option>
                            <option value="*">*</option>
                            <option value="/">/</option>
                        </select>
                    </td>
                </tr>
                <tr>            
                    <td colspan="2">
                        <input type="submit" name="submit" value="Submit"/>
                    </td>
                </tr>
                <%if(result!=null){%>
                <tr>
                    <td>Ans : </td>
                    <td>
                        <%=result%>
                    </td>
                </tr>
                <%}%>
            </table>
        </form>
    </body>
</html>

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s