当前页面: 开发资料首页 → Netbeans 专题 → Web Services (JAX-WS) in Java EE 5
摘要: Java API for XML Web Services (JAX-WS) 2.0, JSR 224, is an important part of the Java EE 5 platform. A follow-on 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
This document takes you through the basics of using the IDE to develop a JAX-WS web service and to consume it in three different clients—either a Java class in a Java SE application, or a servlet or JSP page in a web application. The three clients that you create in this document are separate applications, all consuming the same web service.
Expected duration: 25 minutes
Before you begin, you need to install the following software on your computer:
If you have not registered an instance of the Sun Java System Application Server 9.0, you must do so before you can begin developing Java EE 5 applications:
Note: If you want to deploy to the Tomcat Web Server, you can do so, except that, since it only has a web container, you should create a web application, not an EJB module, in the next section. JAX-WS web services, unlike JSR-109 web services, can deploy successfully to the Tomcat web container.
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 implementation choices. For example, if you plan to deploy to the Tomcat Web Server, which only has a web container, you should choose to create a web application, and not an EJB module.
The Projects window displays the new web service. For example, for web applications the Projects window now looks as follows:
The IDE automatically creates the deployment descriptors required for the server, if any. For the Sun Java System Application Server, no deployment descriptor is needed. For web services deployed to the Tomcat Web Server, sun-jaxws.xml and a WSServlet item in web.xml are added.
In this exercise, you created a NetBeans project and set up the web service.
The goal of this exercise is to do something meaningful with the files and code that the IDE has generated for you. You will add an operation that will add two numbers received from a client.
@WebMethod public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) { // TODO implement operation return 0; }
@WebMethod public int add(@WebParam(name = "i") int i, @WebParam(name = "j") int j) { int k = i + j; return k; }
In this exercise, you added code to 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 the Sun Java System Application Server, is integrated into the IDE for this purpose. For the Tomcat Web Server, there is a similar tool. However, while the Sun Java System Application Server'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:
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.
The IDE starts the application server, builds the application, and opens the tester page in your browser, if you deployed a web application to the Sun Java System Application Server. For the Tomcat Web Server and deployment of EJB modules, the situation is different:
Deployment of application CalculatorWSApplication completed successfully Enable of CalculatorWSApplication in target server completed successfully Enable of application in all targets completed successfully All operations completed successfully run-deploy: run: BUILD SUCCESSFUL
In this exercise, you deployed a web service and tested it.
Now that we have deployed our web service, we need to create a client to make use of the web service's add method. Here, we create three clients— a Java class in a Java SE application, a servlet, and a JSP page in a web application.
Note: At the time of writing, issue Issue 10 was unresolved; therefore, you must create your web service client in a project folder that does not contain spaces in its path. For example, the path should not be "C:\Documents and Settings\...".
Click Finish.
The Projects window displays the new web service client:
System.out.println("Sum: " + port.add(3,4));
The Output window should now show the following:
compile: run: Sum: 7 BUILD SUCCESSFUL (total time: 1 second)
Note: At the time of writing, issue Issue 10 was unresolved; therefore, you must create your web service client in a project folder that does not contain spaces in its path. For example, the path should not be "C:\Documents and Settings\...".
Click Finish.
The Projects window displays the new web service client:
/* 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, right-click in one of the empty lines that you added. Choose Web Service Client Resources > Call Web Service Operation. The Select Operation to Invoke dialog box appears.
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.client.CalculatorWSService service = new org.me.calculator.client.CalculatorWSService(); org.me.calculator.client.CalculatorWS port = service.getCalculatorWSPort(); // TODO initialize WS operation arguments here int arg0 = 0; int arg1 = 0; // TODO process result here int result = port.add(arg0, arg1); 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 arg0 and arg1 to other numbers, such as 3 and 4.
Change the System.out.println statement to out.println.
Add a line that prints out an exception, if an exception is thrown.
The try/catch block should now look as follows (new and changed lines are highlighted):
try { // Call Web Service Operation org.me.calculator.client.CalculatorWSService service = new org.me.calculator.client.CalculatorWSService(); org.me.calculator.client.CalculatorWSApplication port = service.getCalculatorWSApplicationPort(); // TODO initialize WS operation arguments here int arg0 = 3; int arg1 = 4; // TODO process result here int result = port.add(arg0, arg1); out.println("<p>Result: " + result); } catch (Exception ex) { out.println("<p>Exception: " + ex); }
The server starts, if it wasn't running already; the application is built and deployed, and the browser opens, displaying the calculation result.
Note: At the time of writing, issue Issue 10 was unresolved; therefore, you must create your web service client in a project folder that does not contain spaces in its path. For example, the path should not be "C:\Documents and Settings\...".
Click Finish.
The Projects window displays the new web service client.
Change the value for arg0 and arg1 to other numbers, such as 3 and 4.
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 5.5 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.