Creating a Client to Consume a WSIT-enabled Web Service
Now that you have built and tested a web service that uses the WSIT Message Optimization technology, you can create a client that accesses and consumes that web service. The client will use the web service's WSDL to create the functionality necessary to satisfy the interoperability requirements of the web service.
To create a client to access and consume the web service, perform the following steps:
- Choose FileNew Project, select Web Application from the Web category and click Next.
- Name the project, for example, CalculatorWSServletClient.
- Make sure that the J2EE version is set to Java EE 5, then click Finish.
- Right-click the CalculatorWSServletClient node and select NewWeb Service Client. The New Web Service Client window appears.
- Cut and paste the URL of the web service that you want the client to consume into the WSDL URL field, for example,
http://localhost:8080/CalculatorApplication/CalculatorWSService?wsdl
, the URL of the CalculatorWS web service.- Type
org.me.calculator.client
in the Package field, and click Finish. The Projects tab displays the new web service client, shown in Figure 4-3.
- Right-click the CalculatorWSServletClient project node and choose NewServlet.
- Name the servlet ClientServlet, specify the package name, for example,
org.me.calculator.client
and click Finish.- To make the servlet the entry point to your application, right-click the project node, choose Properties, click Run, type
/ClientServlet
in the Relative URL field and click OK.- Double-click
ClientServlet.java
so that it opens in the Source Editor.- In the Source Editor, remove the line that comments out the body of the processRequest method. This is the start-comment line that starts the section that comments out the code:
/* TODO output your page here
- Delete the end-comment line that ends the section of commented out code:
*/
- Add some empty lines after the following line:
out.println("<h1>Servlet ClientServlet at " +
request.getContextPath () + "</h1>");- Right-click in one of the empty lines that you added. Choose Web Service Client ResourcesCall Web Service Operation. The Select Operation to Invoke dialog box appears.
- Browse to the Add operation and click OK. The
processRequest
method looks as follows (the added code is in bold below):
protected void processRequest(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet ClientServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet ClientServlet at "
+ request.getContextPath () + "</h1>");try { // Call Web Service Operation
org.me.calculator.client.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);
system.out.println("Result = "+result);
} catch (Exception ex) {
// TODO handle custom exceptions here
}out.println("</body>");
out.println("</html>");
out.close();
}- Change the value for
int i
andint j
to other numbers, such as 3 and 4.- Change the
System.out.println
statement toout.println
.- Add a line that prints out an exception, if an exception is thrown. The
try
/catch
block should look as follows (new and changed lines are highlighted in bold text):try { // Call Web Service Operation
org.me.calculator.client.CalculatorWS port =
service.getCalculatorWSPort();// TODO initialize WS operation arguments here
int i = 3;
int j = 4;// TODO process result here
int result = port.add(i, j);
out.println("<p>Result: " + result);
} catch (Exception ex) {out.println("<p>Exception: " + ex);
}
- Right-click the project node and choose Run Project. The server starts (if it was not running already) the application is built and deployed, and the browser opens and displays the calculation result.
You have successfully created and deployed a client that can access a web service with message optimization enabled.