Creating a Web Service

toaststicksrifleSoftware and s/w Development

Aug 15, 2012 (5 years ago)

241 views

JAVA WEB SERVICE CONSUMED BY A JSP CLIENT


C
reating 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 applicatio
n, 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 wh
ich 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

1.

Right
-
click the
CalculatorWSApplication

node and choose New > Web Service.

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
following:



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 Operatio
n 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
.

You now see the following:



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.

The visual designer now displays the following
:



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
a
dd

operation to the following (changes are in
bold):


@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 simp
ly 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 s
ection, 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 GlassF
ish, 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. Th
e 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
CalculatorWSApplication project. Right
-
click the CalculatorWS node, and choose
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
situation is different:

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

int

33

int

33


Method returned

int : "
66
"

sub Method invocation



Method parameter(s)

Type

Value

int

45

int

3


Method returned

int : "
42
"

div Method invoca
tion



Method parameter(s)

Type

Value

int

33

int

3


Method returned

int : "
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
/Cal
culatorWSService?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 wo
rking 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
default JSP page that the Web Application wizard creates.

1.

Choose File > New Project (Ctrl
-
Shift
-
N). Select Web Application from the Web
category. Name th
e 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.

The Projects window displays the new web service client, as shown below:



5.

In the Web Service References node, expand the node that represents the web
service.
The
add
,
sub ,mul, div

operation
s
, 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:

<%

try {


org.me.calculator.CalculatorWSService service =


new org.me.calculator.CalculatorWSService();


org.me.calculator.CalculatorWS port =


service.getCalculatorWSPort();


// TODO initialize WS opera
tion 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

fro
m 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 a
nd
deployed, and the browser opens, displaying the calculation result: