• No results found

WebServiceLab-sum.doc

N/A
N/A
Protected

Academic year: 2020

Share "WebServiceLab-sum.doc"

Copied!
12
0
0

Loading.... (view fulltext now)

Full text

(1)

JAVA WEB SERVICE CONSUMED BY A JSP CLIENT

Creating a Java Web Service

The goal of this exercise is to create a project appropriate to the deployment container

that you decide to use. Once you have a project, you will create a web service in it.

Choosing a Container

You can either deploy your web service in a web container or in an EJB container. This

depends on your choice of implementation. For example, if you plan to deploy to the

Tomcat Web Server, which only has a web container, create a web application, not an

EJB module.

1. Choose File > New Project (Ctrl-Shift-N). Select Web Application from the Web

category or EJB Module from the Enterprise category.

2. Name the project CalculatorWSApplication.

3. Depending on the deployment server that you want to use, do the following:

o For GlassFish, set the Java EE Version to Java EE 5.

o For the Tomcat Web Server, unselect the Set Source Level to 1.4

checkbox.

4. Click through the remaining pages and click Finish.

Creating a project in NetBeans 6.1 includes new options which can be left at the

default. For example, the Use Dedicated Folder for Storing Libraries checkbox

may be left unselected.

Creating a Web Service from a Java Class

(2)

2. Name the web service CalculatorWS, type org.me.calculator in Package, and click

Finish.

The Projects window displays the structure of the new web service and the visual

designer is shown in the editor area. For web applications you now see the

(3)

Designing the Web Service

The goal of this exercise is to add to the web service an operation that adds two numbers

received from a client.

Adding an Operation to the Web Service

1. Click Add Operation in the visual designer. A dialog box appears where you can

define the new operation.

2. In the upper part of the Add Operation dialog box, type add in Name and type int

in the Return Type drop-down list. In the lower part of the Add Operation dialog

box, click Add and create a parameter of type int named i. Then click Add again

and create a parameter of type int called j.

(4)

3. Click OK at the bottom of the Add Operation dialog box.

4. Similarly you can create the other Arithmetic Operations such as Subtraction.

Multiplication And Division.

(5)
(6)

5. Click Source and notice that the source code that has been generated in the

previous steps is as follows:

6. In the editor, extend the skeleton add operation to the following (changes are in

(7)

@WebMethod

public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) { int k = i + j;

return k; }

As you can see from the preceding code, the web service simply receives two numbers

and then returns their sum. Similarly changes the code for Subtraction, Multiplication &

division with respective Arithmetic Operators such as Place “–“, “*”,”/” instead of “+”

for performing the respective operations. In the next section, you use the IDE to test the

web service.

Deploying and Testing the Web Service

When you deploy a web service to a web container, the IDE lets you test the web service

to see if it functions as you expect. The Tester application, provided by GlassFish, is

integrated into the IDE for this purpose. For the Tomcat Web Server, there is a similar

tool. However, while GlassFish's Tester page lets you enter values and test them, the

Tomcat Web Server does not. In the latter case, you can only see that the web service is

deployed, you cannot test the values. No facility for testing whether an EJB module is

deployed successfully is currently available.

To test successful deployment to a web container:

1. Right-click the project and choose Undeploy and Deploy. The IDE starts the

application server, builds the application, and deploys the application to the

server. You can follow the progress of these operations in the

CalculatorWSApplication (run-deploy) and GlassFish V2 or Tomcat tabs in the

Output view.

2. In the IDE's Projects tab, expand the Web Services node of the

(8)

Test Web Service.

The IDE opens the tester page in your browser, if you deployed a web application

to GlassFish. For the Tomcat Web Server and deployment of EJB modules, the

(9)

o If you deployed to GlassFish, type two numbers in the tester page, as

shown below:

The sum of the two numbers is displayed:

Method parameter(s)

Type Value

(10)

int 33

Method returned

int : "66"

sub Method invocation

Method parameter(s)

Type Value

int 45 int 3

Method returned

int : "42"

div Method invocation

Method parameter(s)

Type Value

int 33 int 3

Method returned

(11)

o If you deployed to the Tomcat Web Server, you will see the following

instead, which indicates that you have successfully deployed your web service:

o Right-click the project node, choose Properties, and click Run. Depending

on the deployment server that you want to use, do the following:

For GlassFish, type /CalculatorWSService?Tester in the Relative

URL field.

For the Tomcat Web Server, type /CalculatorWS?Tester in the

Relative URL field.

Note: Since the result of a deployed EJB module is not displayed in a browser,

you cannot take the step above if you are working with an EJB module.

Consuming the Web Service

Now that you have deployed the web service, you need to create a client to make use of

the web service's add method. Here, you create the client a JSP page in a web application

Client : JSP Page in Web Application

In this section, you create a new web application and then consume the web service in the

(12)

1. Choose File > New Project (Ctrl-Shift-N). Select Web Application from the Web

category. Name the project CalculatorWSJSPClient. Click Finish.

2. Right-click the CalculatorWSJSPClient node and choose New > Web Service

Client.

3. In Project, click Browse. Browse to the web service that you want to consume.

When you have selected the web service, click OK.

4. Leave the other settings at default (in 6.0.x, do not set a package name) and click

Finish.

(13)

5. In the Web Service References node, expand the node that represents the web

service. The add, sub ,mul, div operations, which you will invoke from the client,

is now exposed.

6. Drag the add operation to the client's index.jsp page, and drop it below the H1

tags. The code for invoking the service's operation is now generated in the

index.jsp page, as you can see here:

(14)

try {

org.me.calculator.CalculatorWSService service = new org.me.calculator.CalculatorWSService(); org.me.calculator.CalculatorWS port =

service.getCalculatorWSPort();

// TODO initialize WS operation arguments here int i = 0;

int j = 0;

// TODO process result here int result = port.add(i, j); out.println("Result = "+result); } catch (Exception ex) {

// TODO handle custom exceptions here }

%>

Change the value for i and j from 0 to other integers, such as 3 and 4.

7. Similarly do the above steps 6 to 21 for Subtraction ,Multiplication and Division

operations

8. Right-click the project node and choose Run.

The server starts, if it wasn't running already. The application is built and

References

Related documents

If a consumer cannot solve the dispute arising from an online sales or service contract directly with the trader, the dispute still can be solved through the platform with

Old Habit: Paper CRF Process Primary Investigator Source Document CRC CRFs CRFs DB1 Double Data Entry Master Clinical 6 Query Report Form Site Sponsor Query Report Form CDM

FAILURE TO USE TWO OR MORE STRONG PEOPLE MAY ALLOW THE RAILS TO SPRING BACK INTO THE FOLDED POSITION AND STRIKE SOMEONE WHICH CAN CAUSE SERIOUS BODILY INJURY OR DEATH.. Keep

To do this, swipe your card first, then enter your WINDOWS username and password and add money to your account if you wish.. The next time you want to

The EJB can be deployed to an EJB or web container that is visible to the JAX-RS runtime, even on an application server separate from the JAX-RS runtime, since EJBs support local

At this point, we have constructed a xed point free action on a separable Hilbert space with a bounded orbit for any countable group containing a surface group.. The same

Edit the Apache configuration file to make the bobjer2 user the owner of the Apache process: # cd /opt/apache2/conf # vi httpd.conf CHANGE: User nobody Group #-1 TO User bobjer2

Summary statistics for all relevant data elements (e.g., non-procedural and procedural time, QMP and staff time, intensity, total work values, service mix, number of patients