Creating a Client to Consume a WSIT-Enabled Web Service
Now that you have built and tested a web service that uses WSIT technologies, 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:
Note: NetBeans submenus are dynamic, so the Web Service Client option may not appear. If you do not see the Web Service Client option, select NewFile\FolderWebservicesWeb Service Client.
http://localhost:8080/CalculatorApplication/CalculatorWSService?wsdl
When JAX-WS generates the web service, it appends "Service" to the class name by default.
- Type
org.me.calculator.client
in the Package field, and click Finish. The Projects window displays the new web service client, as shown in Figure 2-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 CalculatorWSServletClient project node, choose Properties, click Run, type /
ClientServlet
in the Relative URL field and click OK.- If
ClientServlet.java
is not already open in the Source Editor, open it.- 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 is as follows, with bold indicating code added by the IDE:
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
out.println("</body>");
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);
out.println("Result = "+result);
}catch (Exception ex) {
// TODO handle custom exceptions here
}
out.println("</html>");
out.close();
}- Change the value for
int i
andint j
to other numbers, such as 3 and 4.- Add a line that prints out an exception, if an exception is thrown. The
try
/catch
block is follows (new and changed lines from this step and the previous step are highlighted in bold text):try { // Call Web Service Operation
org.me.calculator.client.CalculatorWS port =
service.getCalculatorWSPort();
// TODO initialize WS operation arguments hereint i = 3;
int j = 4;// TODO process result here
} catch (Exception ex) {
int result = port.add(i, j);
out.println("<p>Result: " + result);out.println("<p>Exception: " + ex);
}
- If Reliable Messaging is enabled, the client needs to close the port when done or the server log will be overwhelmed with messages. To close the port, first add the following line to the import statements at the top of the file:
import com.sun.xml.ws.Closeable;
Then add the line in bold at the end of the
try
block, as shown below.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 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);((Closeable)port).close();
} 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, deployed, and run. The browser opens and displays the calculation result.
You have successfully created and deployed a WSIT-enabled client that can access a WSIT-enabled web service.