ex 3 web service client

12
Creation of Web Service Client Aim: To create a client application to invoke the addition web service using Netbeans IDE. Procedure: 1. Create a web service to add two numbers 2. Create a Servlet in web application client to consume the addition web service. a. Choose File > New Project (Ctrl-Shift-N). Select Web Application from the Java Web category. Name the project CalculatorClient. Click Next and then click Finish. b. Right-click the CalculatorClient node and choose New > Web Service Client. The New Web Service Client wizard appears. c. In Project, click Browse. Browse to the web service that you want to consume. When you have selected the web service, click OK. d. Leave the other settings at default and click Finish. e. Right-click the CalculatorClient project node and choose New > Servlet. Name the calculatorServlet and place it in a package called org.servlets. Click Finish. f. Deploy the EJB module CalculatorWS that was created in Exp.No.4a. g. Under Calculatorclient project, expand Web Service References, expand calculator web service, expand calculatorService and expand calculatorPort. Now you find the add operation.

Upload: sathishtesttest

Post on 21-Jul-2016

15 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Ex 3 Web Service Client

Creation of Web Service Client

Aim:To create a client application to invoke the addition web service using Netbeans IDE.Procedure:1. Create a web service to add two numbers 2. Create a Servlet in web application client to consume the addition web service.a. Choose File > New Project (Ctrl-Shift-N). Select Web Application from the JavaWeb category. Name the project CalculatorClient. Click Next and then clickFinish.b. Right-click the CalculatorClient node and choose New > Web Service Client.The New Web Service Client wizard appears.c. In Project, click Browse. Browse to the web service that you want to consume.When you have selected the web service, click OK.d. Leave the other settings at default and click Finish.e. Right-click the CalculatorClient project node and choose New > Servlet. Namethe calculatorServlet and place it in a package called org.servlets. Click Finish.f. Deploy the EJB module CalculatorWS that was created in Exp.No.4a.g. Under Calculatorclient project, expand Web Service References, expandcalculator web service, expand calculatorService and expand calculatorPort.Now you find the add operation.h. In the calculatorServlet Source Editor, remove the line that comments out thebody of the processRequest method./* TODO output your page hereNext, delete the line that ends the section of commented out code:*/i. Add some empty lines after this line:out.println("<h1>Servlet ClientServlet at " +request.getContextPath () + "</h1>");

Page 2: Ex 3 Web Service Client

Now, drag the node that represents the add operation into the space that youcreated.j. The processRequest method now looks as follows (the added code is in boldbelow):protected void processRequest(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();out.println("<html>");out.println("<head>");out.println("<title>Servlet ClientServlet</title>");out.println("</head>");out.println("<body>");out.println("<h1>Servlet ClientServlet at " + request.getContextPath () +"</h1>");try { // Call Web Service Operationorg.me.calculator.CalculatorWS port =service.getCalculatorWSPort();// TODO initialize WS operation arguments hereint i = 0;int j = 0;// TODO process result hereint result = port.add(i, j);out.println("Result = "+result);} catch (Exception ex) {// TODO handle custom exceptions here}out.println("</body>");out.println("</html>");out.close();}k. Modify the declaration statements of variables i and j in processRequestmethod as following to receive the input values from index.jsp.int i = Integer.parseInt(request.getParameter("text1"));int j = Integer.parseInt(request.getParameter("text2"));l. Sending input numbers from JSP to Servlet.Modify the index.jsp in CalculatorClient web application as following:

Page 3: Ex 3 Web Service Client

<body><form name="Test" method="post" action="calculatorServlet"><p>Enter the numbers you want to add:</p><p><p><input type="text" name="text1"></p><p><p><input type="text" name="text2"></p><input type="submit" value="Add" name="addbutton"></form></body>3. Deploy and Run the CalculatorClient web application.

Result:Thus a client application was created to invoke the addition web service using Netbeans IDE.

CREATION OF WEB SERVICE CLIENT

Aim:

To create a web service for adding few numbers using NetBeans and write client side code to invoke the web service.

Algorithm:

1. Using the Netbeans API create a project of the type web application.2. Create a web service in the project.3. Click on the Design tab and design the prototype of the web service.4. Click on source tab and modify the application logic of the web service.5. Save the project.

Page 4: Ex 3 Web Service Client

6. Right click on the project and click on deploy and undeploy.7. Then test the web service.8. Create another web application project and create a jsp file.9. Right click on project and click on create web service client.10. Browse and choose the web service created i.ewsdlurl11. Drag and drop the web service reference to the source code window.12. Then pass the appropriate parameters to the web service client and invoke the web service.

STEPS TO CREATE CLIENT SIDE PROJECT:

1.create the new project as above and give the name as addclient.

2. addclient project will be created. right click it and choose the following.

Page 5: Ex 3 Web Service Client

3.Then browse and choose the addwebwsdl file

4.Then choose the following and add the source code in index.jsp and save it.

Page 6: Ex 3 Web Service Client

Index.jsp source code

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<h1>Hello World!</h1>

<form name="" action="actionn.jsp" method="post">

Enter 1st No:<input name="fst" type="text" /><br/>

Enter 2nd No:<input name="snd" type="text" /><br/>

<input name="ok" type="submit" value="Add" />

</form>

Page 7: Ex 3 Web Service Client

</body>

</html>

5.Then create an action.jsp as follows.

Right click web page in addclient and choose new->jsp

Name:action

Click finish

6.click on the actionn.jsppage..then right click in it and choose web service client reference ->call web service

Page 8: Ex 3 Web Service Client

7.The invoke the add service.

8. add the following code in the action.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

"http://www.w3.org/TR/html4/loose.dtd">

<html>

Page 9: Ex 3 Web Service Client

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>JSP Page</title>

</head>

<body>

<h1>Hello World!</h1>

<%

String a1=request.getParameter("fst");

String b1=request.getParameter("snd");

intaa=Integer.parseInt(a1);

int bb=Integer.parseInt(b1);

%>

<%-- start web service invocation --%><hr/>

<%

try {

org.AddwebService service = new org.AddwebService();

org.Addweb port = service.getAddwebPort();

// TODO initialize WS operation arguments here

int a = aa;

int b = bb;

// TODO process result here

int result = port.add(a, b);

out.println("Result = "+result);

} catch (Exception ex) {

// TODO handle custom exceptions here

Page 10: Ex 3 Web Service Client

}

%>

<%-- end web service invocation --%><hr/>

</body>

</html>

8.finallyundeploy and deploy the addclient and run it.

Page 11: Ex 3 Web Service Client