Java API for XML Web Services (JAX-WS) 2.0/2.1, JSR 224, is an important part of the Java EE 5 platform. A follow-up to the release of Java API for XML-based RPC 1.1(JAX-RPC), JAX-WS simplifies the task of developing web services using Java technology. It addresses some of the issues in JAX-RPC 1.1 by providing support for multiple protocols such as SOAP 1.1, SOAP 1.2, XML, and by providing a facility for supporting additional protocols along with HTTP. JAX-WS uses JAXB 2.0 for data binding and supports customizations to control generated service endpoint interfaces. With its support for annotations, JAX-WS simplifies web service development and reduces the size of runtime JAR files.
This document demonstrates the basics of using the IDE to develop a JAX-WS web service. After you create the web service, you write three different web service clients that use the web service over a network, which is called "consuming" a web service. The three clients are a Java class in a Java SE application, a servlet, and a JSP page in a web application. A more advanced tutorial focusing on clients is Developing JAX-WS Web Service Clients.
Contents
To follow this tutorial, you need the following software and resources.
If you are using JDK 6, you must have JDK 6 Update 7 or later.
Both Tomcat and GlassFish can be installed with the Web and Java EE distribution of NetBeans IDE. Alternatively, you can visit the GlassFish downloads page or the Apache Tomcat downloads page.
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.
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 application, not an EJB module.
Creating a project in NetBeans 6.1 includes new options which can be left at the default. For example, the Use Dedicated Folder for Storing Libraries checkbox may be left unselected.
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:

The goal of this exercise is to add to the web service an operation that adds two numbers received from a client.
You now see the following:
Click OK at the bottom of the Add Operation dialog box.
The visual designer now displays the following:
Click Source and notice that the source code that has been generated in the previous steps is as follows:
In the editor, extend the skeleton add 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 simply receives two numbers and then returns their sum. In the next section, you use the IDE to test 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 GlassFish, 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:
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:
The sum of the two numbers is displayed:
If you deployed to the Tomcat Web Server, you will see the following instead, which indicates that you have successfully deployed your web service:
Right-click the project node, choose Properties, and click Run. Depending on the deployment server that you want to use, do the following:
Note: Since the result of a deployed EJB module is not displayed in a browser, you cannot take the step above if you are working with an EJB module.
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 three clients— a Java class in a Java SE application, a servlet, and a JSP page in a web application.
Note: A more advanced tutorial focusing on clients is Developing JAX-WS Web Service Clients.
In this section, you create a standard Java application. The wizard that you use to create the application also creates a Java class. You then use the IDE's tools to create a client and consume the web service that you created at the start of this tutorial.
If you are using JDK 6 with NetBeans IDE 6.5, you must have JDK 6 Update 7 or later.
The Projects window displays the new web service client, with a node for the add method that you created:
Double-click Main.java so that it opens in the Source Editor. Delete the TODO comment and then drag the add node above into the empty line. You now see the following:
public static void main(String[] args) {
try { // Call Web Service Operation
org.me.calculator.CalculatorWSService service = new org.me.calculator.CalculatorWSService();
org.me.calculator.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
}
}
Note: Alternatively, instead of dragging the add node, you can right-click in the editor and then choose Web Service Client Resources > Call Web Service Operation.
Initialize the two ints. Just change the values of the two ints above from 0 to other integers, such as 3 and 4. Right-click the project node and choose Run.The Output window now shows the sum:
compile:
run:
Result = 7
BUILD SUCCESSFUL (total time: 1 second)
In this section, you create a new web application, after which you create a servlet. You then use the servlet to consume the web service that you created at the start of this tutorial.
If you are using JDK 6, you must have JDK 6 Update 7 or later.
The New Web Service Client wizard appears.
In Project, click Browse. Browse to the web service that you want to consume. When you have selected the web service, click OK. Leave the other settings at default and click Finish.The Web Service References node in the Projects window displays the structure of your newly created client, including the add operation that you created earlier in this tutorial:
Right-click the CalculatorWSServletClient project node and choose New > Servlet. Name the servlet ClientServlet and place it in a package called org.me.calculator.client. Click Finish. Right-click the CalculatorWSServletClient project node and choose Properties. Open the Run properties and unselect Deploy on Save. To make the servlet the entry point to your application, right-click the project node, choose Properties, click Run, and type /ClientServlet in Relative URL. Click OK. If there are error icons for ClientServlet.java, right-click the project node and select Clean and Build. In the Source Editor, remove the line that comments out the body of the processRequest method. This is the line:
/* TODO output your page here
Next, delete the line that ends the section of commented out code:
*/
Add some empty lines after this line:
out.println("<h1>Servlet ClientServlet at " + request.getContextPath () + "</h1>");
Now, drag the node that represents the add operation into the space that you created.
The processRequest method now 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.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("</body>");
out.println("</html>");
out.close();
}
Change the values for i and j to positive integers, such as 3 and 4.
Right-click the project node and choose Run.The server starts, if it wasn't running already; the application is built and deployed, and the browser opens, displaying the calculation result, as shown below:

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.
If you are using JDK 6, you must have JDK 6 Update 7 or later.
The Projects window displays the new web service client, as shown below:
In the Web Service References node, expand the node that represents the web service. The add operation, which you will invoke from the client, is now exposed. 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 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
}
%>
Change the value for i and j from 0 to other integers, such as 3 and 4.
Right-click the project node and choose Run.The server starts, if it wasn't running already. The application is built and deployed, and the browser opens, displaying the calculation result:

For more information about using NetBeans IDE to develop Java EE applications, see the following resources:
To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, join the mailing list.
You are viewing a mobilized version of this site...
View original page here