Advanced Web Service Interoperability In Easy Steps

Implement Client to call the web service

We will continue in our sample with the Client implementation. The client will be a web application that will show a simple form to input the data for a bank order together with a Submit button. Clicking Submit will submit the form data to a processing servlet, which will call the web service. Based on success or failure feedback from the service, the client will redirect to success,jsp or failure.jsp pages. To implement what we described, create a simple Web Application by selecting New Project -> Web Application. The name of the application is going to be WebBankClientApplication. The wizard screen is shown in Figure 5.


Figure 5. Creating Client Web Application

Continue with creating a Web Service Client. Right-click the WebBankClientApplication node, and select New -> Web Service Client. In the wizard window, select the service we implemented by clicking the Browse button next to the project field. Place the client in the package bankorder.webclient. The details are shown in Figure 6.


Figure 6. Creating a Web Service Client

Once the client is created, you should see a Web Service References node in the project view, and expanding all nodes underneath should reveal the web service operation. Now let's use the client and call the web service operation in a processing servlet. The servlet will accept requests coming from the JSP page, and will transform the data from a request into our data transfer object called BankOrder. Then it will invoke the service with the BankOrder object as a parameter. Create the servlet by choosing New -> Servlet in your client project context menu, name it OrderSubmission and place it in the bankorder.web package. Then implement the servlet according to Listing 3. To help with web service invocation, drag the receiveBankOrder() web service operation from Web Service References into your servlet source code – directly into processRequest() method. NetBeans will generate the necessary annotations and web service invocation code for you.

Listing 3. OrderSubmission.java Processing servlet, processRequest() operation implementation

response.setContentType("text/html;charset=UTF-8");
try {
bankorder.webclient.BankOrderService port = service.getBankOrderServicePort();
bankorder.webclient.BankOrder order = new bankorder.webclient.BankOrder();
order.setAmount((String)request.getParameter("amount"));
order.setReceiverAccount((String)request.getParameter("recAcc"));
order.setSenderAccount((String)request.getParameter("senderAcc"));
java.lang.String result = port.receiveBankOrder(order);
if (result.equals("OK")) {
response.sendRedirect("success.jsp");
} else {
response.sendRedirect("failure.jsp");
}
} catch (Exception e) {
System.err.print(e);
response.sendRedirect("failure.jsp");
}

Next, create the pages that the servlet redirects to. Their names shall be failure.jsp and success.jsp. Contents for both is in Listing 4 and 5.

Listing 4. failure.jsp Failure notification page
    ...
<body>
<h2>Service invocation failed!
Check server log for more info.</h2>
</body>
...

Listing 5. success.jsp Success notification page
    ...
<body>
<h2>Bank Order successfully submitted!</h2>
</body>
...

As a last step, we're missing the web form to fill the bank order data into. The form can reside in the default index.jsp which is already created in your project. Listing 6 shows the form, which will submit the data to the implemented OrderSubmission servlet.

Listing 6. index.jsp Form for entering bank order data (account numbers, amount)
    ...
<body>
<form name="OrderSubmission" action="OrderSubmission" method="POST">
Amount: <input type="text" name="amount" value="100" /><br/>
Sender Account: <input type="text" name="senderAcc" value="5678" /><br/>
Recipient Account: <input type="text" name="recAcc" value="1234" /><br/>
<input type="submit" value="Submit" name="Submit" />
</form>
</body>
...

And we're ready! Run the web application, enter some data, click Submit and observe the message flow in the GlassFish output window.

To see the messages and HTTP requests and responses flowing between services and clients, add following Java options to your GlassFish domain.xml ($GLASSFISH_DIR/domains/domain1/config/domain.xml):

 <jvm-options>-Dcom.sun.xml.ws.transport.http.HttpAdapter.dump=true</jvm-options>
<jvm-options>-Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true</jvm-options>

 

AttachmentSize
figure1.png69.91 KB
figure2.png58.05 KB
figure3.png38.63 KB
figure4.png46.74 KB
figure5.png68.87 KB
figure6.png58.29 KB
figure7.png77.2 KB
figure8.png12.83 KB
figure9.png20.8 KB
figure10.png42.9 KB
figure11.png81.43 KB
0
Average: 5 (1 vote)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Jeff Rubinoff replied on Mon, 2008/06/16 - 11:11am

Very nice, simple introduction to WSIT functionality.

akon23 replied on Tue, 2009/10/06 - 12:04am

Very nice simple introductin of wsit.Any way I'll be subscribing to your feed and I hope you post again soon.

<a href=" http://www.allsarongs.com/"> Sarong </a>

 

 

 

pa1000 replied on Wed, 2009/10/07 - 12:20pm

I read this article. Pretty good post. I just stumbled upon your blog and wanted to say,simple introduction to WSIT functionality. Lawyers Devon Park

naser00 replied on Thu, 2009/10/29 - 12:53am

Most of the web service related features in NetBeans are built with the use I can have it installed with your NetBeans IDE 6.1 installation if i choose the Full or Web & J2EE download or specifically select GlassFish).        <a  href="http://www.premiertimeshareresale.com">Timeshare Resales</a>

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.