You are on page 1of 11

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!"hi#t!N$. "elect %eb &pplication #ro' the
(ava
%eb categor). Na'e the project CalculatorClient. Click Ne*t and then click
Finish.
b. +ight!click the CalculatorClient node and choose New > %eb "ervice
Client.
The New %eb "ervice Client wi,ard appears.
c. In Project- click .rowse. .rowse to the web service that )ou want to
consu'e.
%hen )ou have selected the web service- click /0.
d. 1eave the other settings at de#ault and click Finish.
e. +ight!click the CalculatorClient project node and choose New > "ervlet.
Na'e
the calculatorServlet and place it in a package called org.servlets. Click
Finish.
#. Deplo) the E(. 'odule CalculatorWS that was created in E*p.No.2a.
g. 3nder Calculatorclient project- e*pand Web Service References,
e*pand
calculator web service, e*pand calculatorService and e*pand
calculatorPort.
Now )ou 4nd the add operation.
h. In the calculatorServlet "ource Editor- re'ove the line that co''ents
out the
bod) o# the process+e5uest 'ethod.
! "#$# output %our pa&e here
Ne*t- delete the line that ends the section o# co''ented out code6
!
i. &dd so'e e'pt) lines a#ter this line6
out.println'()h1*Servlet ClientServlet at ( +
re,uest.&etConte-tPath '. + ()h1*(./
Now- drag the node that represents the add operation into the space that
)ou
created.
j. The process+e5uest 'ethod now looks as #ollows the added code is in
bold
below$6
protected void process+e5uest7ttp"ervlet+e5uest re5uest-
7ttp"ervlet+esponse response$
throws "ervletE*ception- I/E*ception 8
response.setContentT)pe9te*t:ht'l;charset<3TF!=9$;
Print%riter out < response.get%riter$;
out.println9>ht'l>9$;
out.println9>head>9$;
out.println9>title>"ervlet Client"ervlet>:title>9$;
out.println9>:head>9$;
out.println9>bod)>9$;
out.println9>h?>"ervlet Client"ervlet at 9 @ re5uest.getConte*tPath $ @
9>:h?>9$;
tr% 0 Call Web Service #peration
or&.me.calculator.CalculatorWS port 1
service.&etCalculatorWSPort'./
"#$# initiali2e WS operation ar&uments here
int i 1 3/
int 4 1 3/
"#$# process result here
int result 1 port.add'i, 4./
out.println'(Result 1 (+result./
5 catch '6-ception e-. 0
"#$# handle custom e-ceptions here
5
out.println9>:bod)>9$;
out.println9>:ht'l>9$;
out.close$;
A
k. Bodi#) the declaration state'ents o# variables i and j in processRe,uest
'ethod as #ollowing to receive the input values #ro' inde*.jsp.
int i 1 7nte&er.parse7nt're,uest.&etParameter'(te-t1(../
int 4 1 7nte&er.parse7nt're,uest.&etParameter'(te-t2(../
l. Sendin& input numbers from 8SP to Servlet.
Bodi#) the inde*.jsp in CalculatorClient web application as #ollowing6
>bod)>
>#or' na'e<9Test9 'ethod<9post9 action<9calculator"ervlet9>
>p>Enter the nu'bers )ou want to add6>:p>
>p>
>p>>input t)pe<9te*t9 na'e<9te*t?9>>:p>
>p>
>p>>input t)pe<9te*t9 na'e<9te*tC9>>:p>
>input t)pe<9sub'it9 value<9&dd9 na'e<9addbutton9>
>:#or'>
>:bod)>
9. $eplo% and Run the CalculatorClient web application.
Result:
Thus a client application was created to invoke the addition web service
using
Netbeans IDE.
CR6A"7#: #; W6< S6R=7C6 C>76:"
Aim:
To create a web service #or adding #ew nu'bers using Net.eans and write client
side code to invoke the web service.
Al&orithm:
?. 3sing the Netbeans &PI create a project o# the t)pe web application.
C. Create a web service in the project.
D. Click on the Design tab and design the protot)pe o# the web service.
2. Click on source tab and 'odi#) the application logic o# the web service.
E. "ave the project.
F. +ight click on the project and click on deplo) and undeplo).
G. Then test the web service.
=. Create another web application project and create a jsp 4le.
H. +ight click on project and click on create web service client.
?I..rowse and choose the web service created i.ewsdlurl
??.Drag and drop the web service re#erence to the source code window.
?C.Then pass the appropriate para'eters to the web service client and invoke
the web service.
S"6PS "# CR6A"6 C>76:" S7$6 PR#86C":
?.create the new project as above and give the na'e as addclient.
C. addclient project will be created. right click it and choose the #ollowing.
D.Then browse and choose the addwebwsdl 4le
2.Then choose the #ollowing and add the source code in inde*.jsp and save it.
Inde*.jsp source code
>JKpage contentT)pe<9te*t:ht'l9 pageEncoding<93TF!=9J>
>LD/CTMPE 7TB1 P3.1IC 9!::%DC::DTD 7TB1 2.I? Transitional::EN9
9http6::www.wD.org:T+:ht'l2:loose.dtd9>
>ht'l>
>head>
>'eta http!e5uiv<9Content!T)pe9 content<9te*t:ht'l; charset<3TF!=9>
>title>("P Page>:title>
>:head>
>bod)>
>h?>7ello %orldL>:h?>
>#or' na'e<99 action<9actionn.jsp9 'ethod<9post9>
Enter ?st No6>input na'e<9#st9 t)pe<9te*t9 :>>br:>
Enter Cnd No6>input na'e<9snd9 t)pe<9te*t9 :>>br:>
>input na'e<9ok9 t)pe<9sub'it9 value<9&dd9 :>
>:#or'>
>:bod)>
>:ht'l>
E.Then create an action.jsp as #ollows.
+ight click web page in addclient and choose new!>jsp
Na'e6action
Click 4nish
F.click on the actionn.jsppage..then right click in it and choose web service client
re#erence !>call web service
G.The invoke the add service.
=. add the #ollowing code in the action.jsp
>JKpage contentT)pe<9te*t:ht'l9 pageEncoding<93TF!=9J>
>LD/CTMPE 7TB1 P3.1IC 9!::%DC::DTD 7TB1 2.I? Transitional::EN9
9http6::www.wD.org:T+:ht'l2:loose.dtd9>
>ht'l>
>head>
>'eta http!e5uiv<9Content!T)pe9 content<9te*t:ht'l; charset<3TF!=9>
>title>("P Page>:title>
>:head>
>bod)>
>h?>7ello %orldL>:h?>
>J
"tring a?<re5uest.getPara'eter9#st9$;
"tring b?<re5uest.getPara'eter9snd9$;
intaa<Integer.parseInta?$;
int bb<Integer.parseIntb?$;
J>
>J!! start web service invocation !!J>>hr:>
>J
tr) 8
org.&ddweb"ervice service < new org.&ddweb"ervice$;
org.&ddweb port < service.get&ddwebPort$;
:: T/D/ initiali,e %" operation argu'ents here
int a < aa;
int b < bb;
:: T/D/ process result here
int result < port.adda- b$;
out.println9+esult < 9@result$;
A catch E*ception e*$ 8
:: T/D/ handle custo' e*ceptions here
A
J>
>J!! end web service invocation !!J>>hr:>
>:bod)>
>:ht'l>
=.4nall)undeplo) and deplo) the addclient and run it.

You might also like