Vous êtes sur la page 1sur 82

Ex.

No: Date:

EXPERIMENTING C# .NET COMPONENT

AIM:
To Develop Components Such As Order Processing,Payment Processing Using .Net

PROCEDURE:
File New Project Class Library-(Name: computer).

Enter the code below in the coding window opened.

Code:
using System; using System.Collections.Generic; using System.Text; namespace computer { public class com { int a; public void display1() { Console.WriteLine("COMPUTERS"); Console.WriteLine("1.dell"); Console.WriteLine("2.hcl"); Console.WriteLine("3.lava"); Console.WriteLine("4.sony"); Console.WriteLine("5.samsung"); } } } Save Build Solution.

Create four more such class libraries of:

Com_det:
using System; using System.Collections.Generic; using System.Text;
2

namespace com_det { public class co_det { int d; string w; public void detail3() { Console.WriteLine("SELECTED YOUR DESIRED COMPUTER"); w=Console.ReadLine(); d=int.Parse(w); switch (d) { case 1: { Console.WriteLine("YOU HAVE BEEN SELECTED DELL"); Console.WriteLine("ITEM ID:09"); Console.WriteLine("ITEM PRICE:34,000"); Console.WriteLine("MODEL:INSPIRON"); } break; case 2: { Console.WriteLine("YOU HAVE BEEN SELECTED HCL"); Console.WriteLine("ITEM ID:59"); Console.WriteLine("ITEM PRICE:24,000"); Console.WriteLine("MODEL:ASPIRE"); } break; case 3: { Console.WriteLine("YOU HAVE BEEN SELECTED LAVA"); Console.WriteLine("ITEM ID:05"); Console.WriteLine("ITEM PRICE:55,000");

Console.WriteLine("MODEL:LAVAW"); } break; case 4: { Console.WriteLine("YOU HAVE BEEN SELECTED SONY"); Console.WriteLine("ITEM ID:75"); Console.WriteLine("ITEM PRICE:23,000"); Console.WriteLine("MODEL:DEVINE"); } break; case 5: { Console.WriteLine("YOU HAVE BEEN SELECTED SAMSUNG"); Console.WriteLine("ITEM ID:03"); Console.WriteLine("ITEM PRICE:65,000"); Console.WriteLine("MODEL:GALLERY"); } break; case 6: break; } } } }

Customer:
using System; using System.Collections.Generic; using System.Text; namespace customer
4

{ public class cus { string name; string add; string phno; string email; public void display() { int q; string v; Console.WriteLine("if you are conformed with this product fill the customer form"); Console.WriteLine("1.yes/2.no"); v=Console.ReadLine(); q=int.Parse(v); switch(q) { case 1: { Console.WriteLine("enter your name:"); name = Console.ReadLine(); Console.WriteLine("enter your address:"); add = Console.ReadLine(); Console.WriteLine("enter your phone number:"); phno = Console.ReadLine(); Console.WriteLine("enter your email id:"); email = Console.ReadLine(); Console.WriteLine("CHECK YOUR DETAILS"); Console.WriteLine(""); Console.WriteLine("NAME={0}", name); Console.WriteLine("ADDRESS={0}", add); Console.WriteLine("PHONE NUMBER={0}", phno); Console.WriteLine("EMAIL ID={0}", email);
5

Console.WriteLine(""); Console.ReadLine(); } break; case 2: break; } } } }

Invoice:
using System; using System.Collections.Generic; using System.Text; namespace invoice { public class inv { public void display5() { Console.WriteLine("YOUR PRODUCT WILL BE DELIVERED ON 14.9.2011"); Console.ReadLine(); } } }

OrderClass:
using System; using System.Collections.Generic; using System.Text; namespace orderclass
6

{ public class Corder { int s; string f; public void display4() { Console.WriteLine("select payment method"); Console.WriteLine("1.CHEQUE/2.CASH"); f = Console.ReadLine(); s = int.Parse(f); switch (s) { case 1: { Console.WriteLine("enter your account no:"); Console.ReadLine(); Console.WriteLine("name of bank:"); Console.ReadLine(); Console.WriteLine("your order conformed"); } break; case 2: { Console.WriteLine("your order conformed"); } break; case 3: break; } } } }

File New Project Console Application Ok.

Code:
class Program { static void Main(string[] args) { string i; int h;
8

Console.WriteLine("ONLINE SHOPPING"); Console.WriteLine("COMPUTER SALE"); Console.WriteLine("1.VIEW CATALOGUE"); Console.WriteLine("2.EXIT"); Console.WriteLine("ENTER YOUR CHOICE"); i = Console.ReadLine(); h = int.Parse(i); switch (h) { case 1: { computer.com o2 = new computer.com(); o2.display1(); com_det.co_det O3 = new com_det.co_det(); O3.detail3(); customer.cus o1 = new customer.cus(); o1.display(); orderclass.Corder o4 = new orderclass.Corder(); o4.display4(); invoice.inv o5=new invoice.inv(); o5.display5(); Console.ReadLine(); } break; case 2: break; } } } }

Solution Explorer Right Click Project Name Add Reference Browse Tab Project Name Bin Debug Dll File Ok.

Add all five components as reference as above. Save Run.

10

OUTPUT:

11

Ex.No: Date:

Developing EJB Components

Aim:
To Create Components in EJB using EJB Componet Technology

Procedure:
1. Creating a Project: In the NetBeans IDE 7.0, choose File > New Project. In the New Project Dialog select the java EE category, and choose Enterprise Application from the projects list. Then, Click Next.

12

In the Name and Location page, set the location where you want to create the web application, and provide a name for the project. Click Next.

Select GlassFish Server3.x & Java EE

13

2. Creating a Bean Give a name your choice (simpleBean) select Stateless as session type and give the package name as stateless. In the main tab, the simpleBean.java will be opened automatically.. (1) SIMPLE INTREST simpleBean.java package stateless; import javax.ejb.Stateless; import javax.jws.WebService; /** * * @author XPPRESP3 */ @Stateless @WebService public class simpleBean implements simpleRemote { public int simpleint(int p, int n, int r) { int si=p*n*r/100; return si; }

// Add business logic below. (Right-click in editor and choose // "Insert Code > Add Business Method" or "Web Service > Add Operation") } Right Click simpleEJB, New>Javaclass. Give name simpleRemote.

14

simpleRemote.java
package stateless; import javax.ejb.Remote; /** * * @author XPPRESP3 */ @Remote public interface simpleRemote { int simpleint(int p, int n, int r); } Deploy The Project In web servie there is simpleBean and right click test web service

15

OUTPUT

16

2 STUDENT REGISTER stuEJBbean.java package Stateless; import javax.ejb.Stateless; import javax.jws.WebService; /** * * @author XPPRESP3 */ @Stateless @WebService public class stuEJBBean implements stuEJBRemote { public int stureg(int sregno, String sname, int subj1, int subj2, int subj3) { int total=subj1+subj2+subj3; return total; } // Add business logic below. (Right-click in editor and choose // "Insert Code > Add Business Method" or "Web Service > Add Operation") }

17

Right Click simpleEJB, New>Javaclass. Give name stuEJBRemote. stuEJBRemote.java package stateless; import javax.ejb.Remote; /** * * @author XPPRESP3 */ @Remote public interface stuEJBRemote { public int stureg(int sregno, String sname, int subj1, int subj2, int subj3); }

18

OUTPUT

19

Ex.No: Date:

INVOKE .NET COMPONENTS AS WEB SERVICE

AIM:
To Develop .NET Components As Web Services. Procedure: Step 1: Open Microsoft visual studio 2010. Step 2: Click OnFile->New->Project Select project Templates: Visual C# Projects->ASP.NET Web Service Application Click Ok.

20

Step 3: Right click Service1.asmx Click on view code Enable the methods to use by removing comments and adding the new methods.

Code As Main Web Method: [WebMethod] public string mymailid(string firstname,string lastname) { String strjoin; String[] inputstring=new String[2]; inputstring[0] = firstname; inputstring[1] = lastname; strjoin = string.Join("@", inputstring); return strjoin; } Step 4: Build solution Debug->start without debugging.

21

OUTPUT

22

23

Ex.No: Date:

INVOKE EJB COMPONENTS AS WEB SERVICES

AIM:
To create a web service for adding few numbers using NetBeans.

PROCEDURE:
STEPS TO CREATE ADDITION WEB SERVICE: I.STEPS FOR CREATING SERVER SIDE PROJECT: 1.OPEN File->New->NewProject->Web->Web App..click next

24

2.Give Project Name->Addserverthen Click Finish

3.The addserver project will be created in right side.Right click it and choose the following. Give the web service name as addweb.

25

4.After this in left side ,the design window choose the add operation

5. Give the following in the opened window for creating operation Name->add

6.Then in the source add the following code and save it. package org; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService;
26

@WebService() public class addweb { /** * Web service operation */ @WebMethod(operationName = "add") public int add(@WebParam(name = "a") int a, @WebParam(name = "b") int b) { int k=a+b; return k; } }

7.Then right click on add addserver and perform Deploy Operationafter that right click on addweb and do test web service to see the SOAP request and response message.

27

Give some integers and click add.

28

Ex.No: Date:

CREATION OF A BPEL MODULE AND A COMPOSITE APPLICATION

AIM:
To Create a SOA project with BPEL Module To Compose A Web Service. PROCEDURE: STEPS TO CREATE A BPEL MODULE Creating a new BPEL Module Project The following sections use the Synchronous sample project, and provide step-by-step directions for creating a simple BPEL module project, using the Synchronous sample project. To Create a BPEL Module Project 1. In the NetBeans IDE, choose File > New Project. The New Projects wizard appears. 2. Under Categories, select Service Oriented Architecture. 3. Under Projects, select BPEL Moduleand click Next.

29

4. In the Name and Location page, enter the project name (for this example HelloWorld) and specify the project location or accept the defaults.

5. Click Finish. The Projects window now contains a project node for the BPEL Module project.

Creating a BPEL Process Using the BPEL Designer Now that you have created your XML Schema and WSDL Document, you can create your BPEL process. To Create the BPEL Process
30

1. In the Projects window, expand your BPEL module project node, right-click the Process Files node, and choose New -> BPEL Process. The New BPEL Process dialog box appears.

2. Enter a name for the process file name (HelloWorldProcess for this example), and click Finish. The new BPEL file opens in the Design view of the BPEL Designer. If the Palette and Properties windows are not displayed in your current view, click Windows -> Reset Windows on the NetBeans menu.

31

3. Add a partner link to the BPEL Process. a. In the Projects window, expand your project's Process Files node and select the .wsdl file (Synchronous.wsdl for this example). b. Drag the WSDL file from the Projects window to the left side of the Design view canvas. The IDE provides visual prompts to show you where you can drop the selection. The BPEL Editor adds a partner link to the canvas.

32

4. Add a Receive activity to the BPEL Process. a. From the Web Service section of the Palette window, select the Receive activity. b. Drag the Receive activity to the HelloWorldProcess process box in the Design view canvas, between the Process Start and the Process End activities. The Receive1 activity is added to the process box. c. Click the Receive1 activity's Edit icon. The Receive1 Property Editor appears. d. On the Main tab, change the value in the Name field to start. e. From the Partner Link drop-down list, choose PartnerLink1. The IDE populates the Operation field with NewWSDLOperation. f. Click the Create button next to the Input Variable Field. The New Input Variable dialog box appears. g. Click OK to accept the default values. h. Click OK to close the Receive1 Property Editor. The Design view displays the new connection between PartnerLink1 and the Start activity in the process box.

33

5. Add a Reply activity to the BPEL Process. a. Select the Reply activity in the Web Service section of the Palette. Drag and drop the Reply to the prompt between the Start activity and the Process End activity in the process box on the design view canvas. A Reply1 activity is added to the design view canvas. b. Click the Reply1 activity's Edit icon. The Reply1 Property Editor appears. c. On the Main tab, change the value in the Name field to End. d. From the Partner Link drop-down list, choose PartnerLink1. The IDE populates the Operation field with NewWSDLOperation. e. To create anew output variable, make sure that Normal Response is selected, and click the Create button next to the Input Variable Field. The New Input Variable dialog box appears. Click OK to accept the default values. f. Click OK to close the Reply1 Property Editor. The Design view displays the new connection between the End activity in the process box and PartnerLink1.
34

6. Add a Assign activity to the BPEL Process. a. Select the Assign activity in the Basic Activities section of the Palette. Drag and drop the Assign to the prompt between the Start activity and the End activity in the process box on the design view canvas. The Assign1 activity is added to the design view canvas. b. Select the Assign1 activity and click the Mapper button on the editors toolbar. The BPEL Mapper appears. c. Map the paramA node under Variables -> NewWSDLOperationIn -> inputType in the Output pane of the BPEL Mapper, to the paramA node under Variables -> NewWSDLOperationOut -> resultType in the Input pane of the Mapper. To do this, select theparamA node under Variables -> NewWSDLOperationIn -> inputType in the Output pane, and drag your cursor to the paramA node under Variables -> NewWSDLOperationOut -> resultType in the Input pane. This assignment copies the input statement into the output.

35

7. To save your changes click the Save All icon in the IDE menu bar. Creating a Composite Application Project A BPEL Module project is not directly deployable. You must first add a BPEL Module project, as a JBI module, to a Composite Application project. You can then deploy the Composite Application project. Deploying the project makes the service assembly available to the application server and enables its service units to run. To Create a New Composite Application Project 1. Choose File > New Project (Ctrl-Shift-N). 2. In the Categories list choose Service Oriented Architecture, in the Projects list choose Composite Application, and click Next. 3. In the Name and Location page, change the project name to HelloWorldApplication, and specify the location of project files. 4. To set the new Composite Application the main project as main, leave the Set as Main Project checkbox selected, and click Finish. 5. To add the BPEL Module as a JBI module to the Composite Application project, rightclick the new Composite Application and choose Add JBI Module. The Select Project dialog box opens. 6. Select the HelloWorld project you created earlier and click Add Project JAR Files. The Select Project dialog box closes and the HelloWorld.jar file is added to the JBI Modules node of the HelloWorldApplication Composite Application Building and Deploying the Composite Application Project Building a project compiles the BPEL source file and packages the BPEL file and web service artifacts, including WSDL and XSD files, into a JAR archive. Deploying the project compiles the files in the Composite Application project, packages the compiled BPEL and related web service artifacts (including WSDL and XSD files) into an archive, and deploys them to the Application Server.
36

To Build and Deploy the Composite Application Project 1. Right-click the Composite Application project's node, and choose Build. When the build is complete the Output window reports Build Successful. If the Output window is not visible, choose Window -> Output -> Output. 2. Right-click the Composite Application project's node, and choose Deploy. 3. Deployment has succeeded when you see a Build successful message in the GlassFish tab of the Output window. 4. Open the Services window and expand Servers -> GlassFish V2 -> JBI -> Service Assemblies to see your new deployed Service Assembly. If you do not see the deployed project, right-click the Service Assemblies node and choose Refresh.

Testing the Composite Application You can test your Composite Application project by adding test cases, binding to the operation, supplying input, and then using the tester. Test the HelloWorldApplication Composite Application Project 1. In the Projects window, expand the HelloWorldApplication project node, right-click the Test node, and choose New Test Case. The New Test Case wizard opens. 2. Accept the default test case name, TestCase1, and click Next. 3. From the Select the WSDL Document page, expand the HelloWorld - Proecss Files node, select Synchronous.wsdl, and click Next. 4. From the Select the Operation to Test page, select the Operation1 and click Finish.

37

A new TestCase1 node is added under the project's Test node in the Projects window, containing two subnodes, Input and Output. The Source Editor appears containing the Input file, Input.xml

Note If the Source Editor does not contain a tab for Input.xml, double-click the Input node in the Projects window to open the file.

5. From the Input.xml tab of the Source Editor, do the following: a. Locate the line:

<syn:paramA>?string?<syn:paramA> b. Replace ?string? with Hello World, so that the line appears as follows:

<syn:paramA>Hello World<syn:paramA> c. From the NetBeans IDE menu bar, click Save All.

6. In the Projects window, double-click the Output node under Test -> TestCase1. Output.xml is opened in the Source Editor. Initially, Output.xml is empty until the first test run populates the file. 7. In the Projects window, right-click the TestCase1 node and choose Run from the pop-up menu.
38

When the Overwrite Empty Output dialog box appears, click Yes to accept new output. The first test run populates the Output.xml file displayed in the Source Editor.

The test compares the output to the contents of the output file. Because the first run of the test has nothing to compare itself to, the first test fails. Subsequent test runs will compare their output with the contents of Output.xml and should succeed. 8. Run the test again. The test case is compared to the current output file and succeeds.

39

Ex.No: Date: AIM:

DEVELOP A J2EE CLIENT TO ACCESS A .NET WEB SERVICE

To Development of a J2EE Application for consuming the .NET web service. Procedure: Step 1: Open Microsoft visual studio 2010. Step 2: Click On.File->New->Project Select Project Templates:Visual C# Projects->ASP.Net Web Service Application Click ok.

Step 3: Right click Service1.asmx Click on view code

40

Enable the methods to use by removing comments and adding the new methods. Code As Main Web Method: [WebMethod] public string mymailid(string firstname,string lastname) { String strjoin; String[] inputstring=new String[2]; inputstring[0] = firstname; inputstring[1] = lastname; strjoin = string.Join("@", inputstring); return strjoin; }

Step 4: Build Solution Debug->Start Without Debugging.

41

Copy The Wsdl Link.

42

Step 5: In NetBeans IDE 7.0,Creating Web Service Client

Click on New -> New Web Service Client

43

Paste The Wsdl Link

Give the name of the package Click finish Step 6: Browse the file index.jsp Right Click->Select Call Web Service Select the operation and click ok Write the required code This creates the client Step 7: Right Click the index.jsp and run.

44

Client 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> <h2>Hello World!</h2> <%-- start web service invocation --%><hr/> <% try { Man.Service1 service = new Man.Service1(); Man.Service1Soap port = service.getService1Soap(); // TODO initialize WS operation arguments here java.lang.String firstname = "deepu7"; java.lang.String lastname = "gmail.com"; // TODO process result here java.lang.String result = port.mymailid(firstname, lastname); out.println("My Mail id is "+result); } catch (Exception ex) { // TODO handle custom exceptions here } %> <%-- end web service invocation --%><hr/> </body> </html>

45

OUTPUT:

46

Ex.No: Date:

.NET CLIENT TO ACESS A J2EE WEBSERVICE

AIM:
To develop .Net Client To Acess A J2EE Web Service

PROCEDURE:
Create a Java Web Service (Java EE, JAX-WS) 1. Create a new Web Application In the NetBeans IDE 7.0, choose File > New Project. In the New Project Dialog select the Web category, and choose Web Application from the projects list. Then, Click Next.

In the Name and Location page, set the location where you want to create the web application, and provide a name for the project. Click Next.

In the Server and Settings page, leave the default settings (Java EE 5, Use GlassFish V3) and Click Finish.
47

This creates the initial web application and opens the project for editing.

2. Create the Web Service Add a new web service to the project. Right click the project node and choose New > Web Service.

* Notice that the location of the Web Service option in the menu may change from this image and your IDE. In the New Web Service dialog, provide the Web Service Name, and the Package. The name of the service will affect the final URL For Calling The Service, and the package name will be the namespace of the service contract. Click Finish.

48

The Web Service now appears in the project tree.

To implement the service, double click the service node in the project tree (in the figure above CalculatorService). This will open the Web Service in Design mode, that lets you graphically design your service.

Change to Source View by clicking on the Source button in the upper toolbar, and this will open CalculatorService.Java file for editing. Here is a sample implementation of the service.
49

Notice how Java Annotations are similar to .Net Attributes, especially how similar they are to the Web Services attributes we know CalculatorService.Java package org.bursteg.calculator; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService;

@WebService public class CalculatorService { @WebMethod public int Add(@WebParam(name="a") int a, @WebParam(name="b") int b) { return a + b; } } Deploy the web service to the web application server. From the NetBeans IDE this is done by right clicking the project node, and choosing Undeploy and Deploy.

50

After the web application has been deployed, just to make sure the web service works as expected, you can right click the web service node, and choose Test Web Service.

This will open the browser and navigate to a test page with the url of the service (http://localhost:8080/Calculator/CalculatorServiceService?Tester suffix.)

51

Call the Java Web Service from a .Net Client In Visual Studio 2010, Create A New Console Application.

This creates a new solution with a single Console Application project in it.

Right click the project node and choose Add Service Reference.

In the Add Service Reference Dialog, paste the address of the service metadata endpoint (http://localhost:8080/Calculator/CalculatorServiceService?wsdl), and click Go. The dialog will get the service metadata and understand the service contract.

52

Provide a namespace for the service reference, and click OK. This will generate the client side proxy that lets you consume the service easily, and the required configuration settings into the configuration file.

To call the service using the generated client side proxy, open Program.cs and use the following code: static void Main(string[] args) { CalculatorService.CalculatorServiceClient proxy = new CalculatorService.CalculatorServiceClient(); int result = proxy.Add(2, 3); Console.WriteLine("Calculator Service returned: " + result.ToString()); }

53

Run the program and see that the web service is being called and the result is correct.

54

Ex.No: Date: AIM:

JAVA WEB SERVICE AND INVOKE IT FROM ASP.NET APPLICATION

To create Java web service and invoke it from ASP.Net application PROCEDURE: Create a Java Web Service: There are a few steps involved in developing a web service using it. These steps can be summarized as follows: Create a web application project. Add web service to the project. Add operations to the web service. Implementing the web methodes. Deploy and test the web service. Step 1. Create a web application project In Netbeans IDE 7.0,Go To File->New Project. The New Project wizard opens. Select the web from categories options and web application from project section and then press the next button. (i.e.,Java Web->Web Application)

Figure One: Create New Project Step 1

55

On the next screen mention the project name, select the project location. We can also mention the server name in which we want to deploy our web application as well we can change the default context path. Here we mention the project name JSimpCalcWebService and keep the context path same as project name. We use GlassFish V2 application server for deployment.

Figure Two: Create New Project Step 2 Now, click the finish button. Step 2. Add web service to the project

56

Right click on the project name in the Projects explorer window.

Figure Three: Web Service Menu Option From the context menu options select the Web Service menu. A web service dialog opens.

Figure Four: Add New Web Service Mention the web service name and the package name as mentioned in the image below, and then click the finish button. In our example the web service name is JSimpCalcWebService and the package name is calc.ws. Step 3. Add operations to the web service
57

After we add web service to our application, now it's time to add web service operation or WebMethod. We can do it in two possible ways one is through design mode and another is through source mode. In our example we use design mode for creating skeleton of WebMethod in easiest way.

Figure Five: Add Operation To Web Service As we can see from highlighted section in the figure five we can add web service operations by clicking Add Operation button. It opens a Add Operation dialog box. Please refer to figure six.

Figure Six: Add Parameters To Web Operation In the Add Operation dialog we must mention name (which is actually a WebMethod name). We can also enter parameter names and their types (these parameter are known as WebParam). In the figure six we mention the WebMethod name as addition whose return type is java.lang.String and it takes two parameters (parameter1. and parameter2) of type double.
58

Similarly we create other operations as well like subtraction, multiplication, division, power, maximum, minimum. Step 4: Implementing the web methodes Once we finish step three, a basic structure of our web service should be ready. Then we switch from design mode to source mode as shown in figure seven to do rest of the implementation.

Figure Seven: Write The Code For The Web Service The code should look like as follows: Collapse | Copy Code package calc.ws; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import calc.util.NumberFormater; /** * @author Bikash Shaw */ @WebService() public class JSimpCalcWebService { /** * Web service operation */ @WebMethod(operationName = "addition") public String addition(@WebParam(name = "parameter1") double parameter1, @WebParam(name = "parameter2")
59

double parameter2) { //TODO write your implementation code here: return NumberFormater.format((parameter1 + parameter2),0,6); } /** * Web service operation */ @WebMethod(operationName = "subtraction") public String subtraction(@WebParam(name = "parameter1") double parameter1, @WebParam(name = "parameter2") double parameter2) { //TODO write your implementation code here: return NumberFormater.format((parameter1 - parameter2),0,6); } /** * Web service operation */ @WebMethod(operationName = "multiplication") public String multiplication(@WebParam(name = "parameter1") double parameter1, @WebParam(name = "parameter2") double parameter2) { //TODO write your implementation code here: return NumberFormater.format((parameter1 * parameter2),0,6); }

/** * Web service operation */ @WebMethod(operationName = "division") public String division(@WebParam(name = "parameter1") double parameter1, @WebParam(name = "parameter2")
60

double parameter2) { //TODO write your implementation code here: return NumberFormater.format((parameter1 / parameter2),0,6); } /** * Web service operation */ @WebMethod(operationName = "power") public String power(@WebParam(name = "parameter1") double parameter1, @WebParam(name = "parameter2") double parameter2) { //TODO write your implementation code here: return NumberFormater.format(Math.pow(parameter1, parameter2),0,6); }

/** * Web service operation */ @WebMethod(operationName = "maximum") public String maximum(@WebParam(name = "parameter1") double parameter1, @WebParam(name = "parameter2") double parameter2) { //TODO write your implementation code here: return NumberFormater.format(Math.max(parameter1, parameter2),0,6); }

/** * Web service operation */ @WebMethod(operationName = "minimum") public String minimum(@WebParam(name = "parameter1")
61

double parameter1, @WebParam(name = "parameter2") double parameter2) { //TODO write your implementation code here: return NumberFormater.format(Math.min(parameter1, parameter2),0,6); } }

Source One: Java JSimpCalcWebService.java Web Service Source Code Here we use NumberFormater.format(double number, int minFractionDigits, int maxFractionDigits) method to format the double value to java.lang.String up to six decimal point. Refer to the source two.

Collapse | Copy Code package calc.util;

import java.text.NumberFormat;

/**

62

public class NumberFormater {

public static String format(double number, int minFractionDigits, int maxFractionDigits) { NumberFormat format = NumberFormat.getInstance(); format.setMaximumFractionDigits(maxFractionDigits); format.setMinimumFractionDigits(minFractionDigits); return format.format(number); } } Source Two: NumberFormater.java Source Code Step 5. Deploy and test the web service Now our web service is ready deployment and test. With Netbeans 6 it can achieved very with few steps. First make sure that the GlassFish server is running. To start the server we need to perform the following steps. Go to Services explorer window. Expand the Servers node. Right click on the server name (in our case its GlassFish V2). A context menu pops up. Select Start from the menu options.

Figure Eight: Start GlassFish V2 Application Server Now, as our server is running it's time to deploy the application and test the web service that we have developed. Netbeans does the deployment for us with few mouse clicks, mentioned as follows: Go to Projects explorer window.
63

Expand the Web Services node. Right click on the web services name (in our case its JSimpCalcWebService). A context menu pops up. Click the Test Web Service menu.

Figure Nine: Test Web Service The above mentioned steps deploy the application and lunch the default browser in which the web service can be tested via SOAP request and response. Please refer to the figure ten for sample output. We can also view the WSDL (Web Services Description Language) file by clicking on the hyperlink.

64

Figure Ten: Test Web Service Alternatively we can test the web service and view its WSDL document through our GlassFish application servers admin consol as shown in figure eleven.

Figure Eleven: Test Web Service via Admin Consol Once we click on the View WSDL link we can view a XML file for describing our web service. The WSDL file should look like the figure twelve.

65

Figure Twelve: WSDL File The URL shown at the address bar will require invoking the web service. We shall demonstrate its usage during creating of our web application using ASP.net. Create ASP.net Web Site Using C#: Now, our web service is ready to get invoked from non Java based development platform. In this article we develop a sample ASP.net based client. Using Visual Studio 2008 it can be achieved in few steps, which can be summarized as follows: Create ASP.net web site. Add web reference. Write code to invoke web service. Test web service client application. Step 1. Create ASP.net web site Start Visual Studio 2008; go to the New Web Site which is available under File menu.

66

Figure Thirteen: New Web Site Window Select ASP.net Web Site. Select the folder in which we want to create the web site. In our case its JSimpCalcWebServiceWebSite Select the language Visual C# and click OK button. Step 2. Add web reference Now we need to mention the WSDL file in our web site. To add the web service reference we must perform the following steps: Go to Solution Explorer window. Right click on the project name (in our case its JSimpCalcWebServiceWebSite). A context menu pops up. Click the Add Web Reference menu. The Add Web Reference dialog opens.

67

Figure Fourteen: Add Web Reference Context Menu Copy and paste the WSDL URL from our web browers address bar (refer to figure twelve) to Add Web Reference dialogs address bar and press go button (refer to figure fifteen).

Figure Fifteen: Add Web Reference Dialog We can see all the methods names of our web service. Give a name to the web reference (in this example its JSimpCalcWebService) and click the Add Reference button. Step 3. Write code to invoke web service Using C# we can very easily invoke the web service with few lines of code: We first design an ASP.net page. The default fie name is Default.aspx (the source is available in zip file).

68

Induce the web reference in to our code (i.e. Default.aspx.cs). For example: using JSimpCalcWebServiceService; Next, create the object of the web reference. JSimpCalcWebServiceService.JSimpCalcWebServiceService proxy = new JSimpCalcWebServiceService.JSimpCalcWebServiceService(); Now, we can access the WebMethod like any other method call. For example: proxy.addition(10,20); Code for Aspx.cs file using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using JSimpCalcuWSService;

public partial class _Default : System.Web.UI.Page { JSimpCalcuWSService.JSimpCalcuWS proxy;

protected void Page_Load(object sender, EventArgs e) {


69

proxy = new JSimpCalcuWSService.JSimpCalcuWS(); } protected void btnAddition_Click(object sender, EventArgs e) { try { lblResultAddition.Text = "Result: " + proxy.addition(int.Parse(txtAdditionx.Text), int.Parse(txtAdditiony.Text)); } catch (FormatException) { lblResultAddition.Text = "Result: Invalide Input"; } UpdatePanelAddition.Update(); } protected void btnSubtraction_Click(object sender, EventArgs e) { try { lblResultSubtraction.Text = "Result: " + proxy.subtraction(int.Parse(txtSubtractionx.Text), int.Parse(txtSubtractiony.Text)); } catch (FormatException) { lblResultSubtraction.Text = "Result: Invalide Input"; } UpdatePanelSubtraction.Update();
70

} protected void btnMultiplication_Click(object sender, EventArgs e) { try { lblResultMultiplication.Text = "Result: " + proxy.multiplication(int.Parse(txtMultiplicationx.Text), int.Parse(txtMultiplicationy.Text)); } catch (FormatException) { lblResultMultiplication.Text = "Result: Invalide Input"; } UpdatePanelMultiplication.Update(); } protected void btnDivision_Click(object sender, EventArgs e) { try { lblResultDivision.Text = "Result: " + proxy.division(int.Parse(txtDivisionx.Text), int.Parse(txtDivisiony.Text)); } catch (FormatException) { lblResultDivision.Text = "Result: Invalide Input"; }

71

UpdatePanelDivision.Update(); } protected void btnMaximum_Click(object sender, EventArgs e) { try { lblResultMaxMin.Text = "Result: " + proxy.maximum(int.Parse(txtMaxMinx.Text), int.Parse(txtMaxMiny.Text)); } catch (FormatException) { lblResultMaxMin.Text = "Result: Invalide Input"; } UpdatePanelMaxMin.Update(); } protected void btnMinimum_Click(object sender, EventArgs e) { try { lblResultMaxMin.Text = "Result: " + proxy.minimum(int.Parse(txtMaxMinx.Text), int.Parse(txtMaxMiny.Text)); } catch (FormatException) { lblResultMaxMin.Text = "Result: Invalide Input";

72

} UpdatePanelMaxMin.Update(); } protected void btnPower_Click(object sender, EventArgs e) { try { lblResultPower.Text = "Result: " + proxy.power(int.Parse(txtPowerx.Text), int.Parse(txtPowery.Text)); } catch (FormatException) { lblResultPower.Text = "Result: Invalide Input"; } UpdatePanelPower.Update(); } } Code for Aspx file <%@ Page Language="C#" AutoEventWireup="true" CodeFile="JSimpcalc.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server">

73

<title>Java simple Calculation Example</title> </head> <body> <form id="form1" runat="server"> <asp:scriptmanager ID="Mano" runat="server"></asp:scriptmanager> <asp:Table ID="Table1" runat="server" Width="372px" BorderWidth="1" BorderStyle="Solid" Height="70px" HorizontalAlign="Left"> <asp:TableRow ID="TableRow1" runat="server"> <asp:TableCell ID="TableCell1" runat="server" ColumnSpan="3" HorizontalAlign="Center" ForeColor="Blue" Font-Bold="true"> <asp:UpdatePanel ID="UpdatePanelAddition" runat="server" RenderMode="Inline"> <ContentTemplate> <asp:Table ID="Table2" runat="server" Width="372px" BorderWidth="1" BorderStyle="Solid" Caption="ADDITION"> <asp:TableRow ID="TableRow2" runat="server"> <asp:TableCell ID="TableCell2" runat="server"> <asp:Label ID="Label1" runat="server" Text="First Parameter"> <asp:TextBox ID="txtAdditionx" runat="server" MaxLength="10"></asp:TextBox> </asp:Label><br/> <asp:Label ID="Label2" runat="server" Text="Scnd Parameter"> <asp:TextBox ID="txtAdditiony" runat="server" MaxLength="10"></asp:TextBox> </asp:Label><br/> <asp:Label ID="Label3" runat="server" Text="Result"><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="lblResultAddition" runat="server" Text="Compute" OnClick="btnAddition_Click"></asp:Button>
74

</asp:Label> </asp:TableCell> </asp:TableRow> </asp:Table> </ContentTemplate> </asp:UpdatePanel> </asp:TableCell> </asp:TableRow> </asp:Table> <asp:Table ID="Table3" runat="server" Width="372px" BorderWidth="1" BorderStyle="Solid" Height="70px" HorizontalAlign="Justify"> <asp:TableRow ID="TableRow3" runat="server"> <asp:TableCell ID="TableCell3" runat="server" ColumnSpan="3" HorizontalAlign="Center" ForeColor="Blue" Font-Bold="true"> <asp:UpdatePanel ID="UpdatePanelSubtraction" runat="server" RenderMode="Inline"> <ContentTemplate> <asp:Table ID="Table4" runat="server" Width="372px" BorderWidth="1" BorderStyle="Solid" Caption="SUBTRACTION"> <asp:TableRow ID="TableRow4" runat="server"> <asp:TableCell ID="TableCell4" runat="server"> <asp:Label ID="Label4" runat="server" Text="First Parameter"></asp:Label> <asp:TextBox ID="txtSubtractionx" runat="server" MaxLength="10"></asp:TextBox> <br/> <asp:Label ID="Label5" runat="server" Text="Scnd Parameter"></asp:Label>

75

<asp:TextBox ID="txtSubtractiony" runat="server" MaxLength="10"></asp:TextBox> <br/> <asp:Textbox ID="lblResultSubtraction" runat="server"></asp:Textbox> <asp:Button ID="Button1" runat="server" Text="Compute" OnClick="btnSubtraction_Click"></asp:Button> </asp:TableCell> </asp:TableRow> </asp:Table> </ContentTemplate> </asp:UpdatePanel> </asp:TableCell> </asp:TableRow> </asp:Table> <asp:Table ID="Table5" runat="server" Width="372px" BorderWidth="1" BorderStyle="Solid" Height="70px" HorizontalAlign="Left"> <asp:TableRow ID="TableRow5" runat="server"> <asp:TableCell ID="TableCell5" runat="server" ColumnSpan="3" HorizontalAlign="Center" ForeColor="Blue" Font-Bold="true"> <asp:UpdatePanel ID="UpdatePanelMultiplication" runat="server" RenderMode="Inline"> <ContentTemplate> <asp:Table ID="Table6" runat="server" Width="372px" BorderWidth="1" BorderStyle="Solid" Caption="MULTIPLICATION"> <asp:TableRow ID="TableRow6" runat="server"> <asp:TableCell ID="TableCell6" runat="server"> <asp:Label ID="Label7" runat="server" Text="First Parameter">
76

<asp:TextBox ID="txtMultiplicationx" runat="server" MaxLength="10"></asp:TextBox> </asp:Label><br/> <asp:Label ID="Label8" runat="server" Text="Scnd Parameter"> <asp:TextBox ID="txtMultiplicationy" runat="server" MaxLength="10"></asp:TextBox> </asp:Label><br/> <asp:Label ID="lblResultMultiplication" runat="server" Text="Result"><asp:TextBox ID="TextBox7" runat="server"></asp:TextBox> <asp:Button ID="Button2" runat="server" Text="Compute" UseSubmitBehavior="true"></asp:Button> </asp:Label> </asp:TableCell> </asp:TableRow> </asp:Table> </ContentTemplate> </asp:UpdatePanel> </asp:TableCell> </asp:TableRow> </asp:Table> <asp:Table ID="Table7" runat="server" Width="372px" BorderWidth="1" BorderStyle="Solid" Height="70px" HorizontalAlign="Justify"> <asp:TableRow ID="TableRow7" runat="server"> <asp:TableCell ID="TableCell7" runat="server" ColumnSpan="3" HorizontalAlign="Center" ForeColor="Blue" Font-Bold="true"> <asp:UpdatePanel ID="UpdatePanelDivision" runat="server" RenderMode="Inline"> <ContentTemplate>

77

<asp:Table ID="Table8" runat="server" Width="372px" BorderWidth="1" BorderStyle="Solid" Caption="DIVISION"> <asp:TableRow ID="TableRow8" runat="server"> <asp:TableCell ID="TableCell8" runat="server"> <asp:Label ID="Label10" runat="server" Text="First Parameter"> <asp:TextBox ID="txtDivisionx" runat="server" MaxLength="10"></asp:TextBox> </asp:Label><br/> <asp:Label ID="Label11" runat="server" Text="Scnd Parameter"> <asp:TextBox ID="txtDivisiony" runat="server" MaxLength="10"></asp:TextBox> </asp:Label><br/> <asp:Label ID="lblResultDivision" runat="server" Text="Result"><asp:TextBox ID="TextBox10" runat="server"></asp:TextBox> <asp:Button ID="Button3" runat="server" Text="Compute" UseSubmitBehavior="true"></asp:Button> </asp:Label> </asp:TableCell> </asp:TableRow> </asp:Table> </ContentTemplate> </asp:UpdatePanel> </asp:TableCell> </asp:TableRow> </asp:Table> <asp:Table ID="Table9" runat="server" Width="372px" BorderWidth="1" BorderStyle="Solid" Height="70px" HorizontalAlign="Left">
78

<asp:TableRow ID="TableRow9" runat="server"> <asp:TableCell ID="TableCell9" runat="server" ColumnSpan="3" HorizontalAlign="Center" ForeColor="Blue" Font-Bold="true"> <asp:UpdatePanel ID="UpdatePanelPower" runat="server" RenderMode="Inline"> <ContentTemplate> <asp:Table ID="Table10" runat="server" Width="372px" BorderWidth="1" BorderStyle="Solid" Caption="POWER"> <asp:TableRow ID="TableRow10" runat="server"> <asp:TableCell ID="TableCell10" runat="server"> <asp:Label ID="Label13" runat="server" Text="First Parameter"> <asp:TextBox ID="txtPowerx" runat="server" MaxLength="10"></asp:TextBox> </asp:Label><br/> <asp:Label ID="Label14" runat="server" Text="Scnd Parameter"> <asp:TextBox ID="txtPowery" runat="server" MaxLength="10"></asp:TextBox> </asp:Label><br/> <asp:Label ID="lblResultPower" runat="server" Text="Result"><asp:TextBox ID="TextBox13" runat="server"></asp:TextBox> <asp:Button ID="Button4" runat="server" Text="Compute" UseSubmitBehavior="true"></asp:Button> </asp:Label> </asp:TableCell> </asp:TableRow> </asp:Table> </ContentTemplate> </asp:UpdatePanel>
79

</asp:TableCell> </asp:TableRow> </asp:Table> <asp:Table ID="Table11" runat="server" Width="372px" BorderWidth="1" BorderStyle="Solid" Height="70px" HorizontalAlign="Justify"> <asp:TableRow ID="TableRow11" runat="server"> <asp:TableCell ID="TableCell11" runat="server" ColumnSpan="3" HorizontalAlign="Center" ForeColor="Blue" Font-Bold="true"> <asp:UpdatePanel ID="UpdatePanelMaxMin" runat="server" RenderMode="Inline"> <ContentTemplate> <asp:Table ID="Table12" runat="server" Width="372px" BorderWidth="1" BorderStyle="Solid" Caption="MAX_MIN"> <asp:TableRow ID="TableRow12" runat="server"> <asp:TableCell ID="TableCell12" runat="server"> <asp:Label ID="Label16" runat="server" Text="First Parameter"> <asp:TextBox ID="txtMaxMinx" runat="server" MaxLength="10"></asp:TextBox> </asp:Label><br/> <asp:Label ID="Label17" runat="server" Text="Scnd Parameter"> <asp:TextBox ID="txtMaxMiny" runat="server" MaxLength="10"></asp:TextBox> </asp:Label><br/> <asp:Label ID="lblResultMaxMin" runat="server" Text="Result"><asp:TextBox ID="TextBox16" runat="server"></asp:TextBox> <asp:Button ID="Button5" runat="server" Text="Compute" UseSubmitBehavior="true"></asp:Button> </asp:Label>
80

</asp:TableCell> </asp:TableRow> </asp:Table> </ContentTemplate> </asp:UpdatePanel> </asp:TableCell> </asp:TableRow> </asp:Table> </form> </body> </html>

Figure Sixteen: Web Methods Invocation Code Source Three: Default.aspx.cs Source Code Step 4. Test web service client application Now, it's time to test our web service client application by clicking on the Start Debugging toolbar button or by pressing F5 key. The web page should look like the figure below.

81

OUTPUT

82

Vous aimerez peut-être aussi