Develop In Java
  • Home
  • News
  • Tips and Tricks
  • Articles
    • Books>
      • Alfresco Enterprise Content Management Implementation
        • Beginning Java EE 6 Platform with GlassFish 3
          • EJB 3.1 Cookbook
            • GlassFish Security
              • GWT Java Ajax Programming
                • Java EE 5 Development using GlassFish Application Server
                  • JBoss RichFaces
                    • Seam 2.x Web Development
                    • Desktop Java>
                      • An Overview of The New Features in J2SE 5.0
                        • Using FindBugs with NetBeans 5.5
                        • Enterprise Java>
                          • Basic Java CRUD Operations With MongoDB
                            • Continuous Integration with Hudson on Glassfish
                              • Creating a Simple Spring MVC Web App With NetBeans 5.0
                                • Creating a Simple Spring MVC Web App With NetBeans 5.0 Part 2
                                  • Creating a Weld Project using Maven and NetBeans
                                    • Deploying a Java EE Web Application to OpenShift Express
                                      • Developing your first Web Service with Java EE 5 and NetBeans
                                        • Introduction to JSF 2 Using NetBeans and GlassFish
                                          • Securing a Web Application on Glassfish using JAAS
                                            • Securing a Web Application on Glassfish using JAAS Part 2
                                          • Links
                                          • Forums
                                          • Search
                                          • Contact Us

                                          Developing your first Web Service with Java EE 5 and NetBeans

                                          In this article we aim to show how easy it is to generate simple webservices using Java EE 5 deploying to the Glassfish application server. The code in this article has been developed with beta 1 of NetBeans 5.5 with the enterprise pack installed and is running on build 48 of the Glassfish application server.

                                          To create a web service, first we need to create a project within NetBeans. Select File | New Project from the NetBeans main menu. On the resulting dialog, select the Enterprise category and choose Enterprise Application. On the resultant dialog, enter the project name as HelloWebService and choose a location for the project. Ensure that the Glassfish server is selected and that the J2EE version is set to Java EE 5. Since we are creating a simple webservice, select to create an EJB Module only. When this information has been specified, press the Finish button to create the project.
                                          Picture
                                          The project explorer now shows the HelloWebService and the HelloWebService-ejb nodes. Right click on the HelloWebService-ejb node and select the New | Web Service menu option. In the resultant dialog, enter the Web Service Name as HelloWorldService and enter the package as helloworld. Ensure that an empty web service is created as shown in the following screen shot and select the Finish button to create the web service.
                                          Picture
                                          After selecting the Finish button, the source code for the web service will be opened in the file HelloWorldService.java. Add the following code into the class to create the web service method.
                                          @WebMethod
                                          
                                          public String sayHello(String name) {
                                          return "Hello "+name;
                                          }
                                          Ensure that the project can be build by pressing F11 or selecting the Build Main Project menu option. If everything builds correctly, right click on the HelloWebService project node and select the Deploy Project menu option. This will start the application server (Glassfish in our sample) and deploy the sample web service. After a few seconds, confirmation will be shown in the project output that the webservice has been successfully deployed to the server.
                                          Testing the web service in NetBeans 5.5 is just as easy as creating the web service. Open up the Web Services node under the project as shown in the following screen dump and select the Test Web Service menu option.
                                          Picture
                                          This will cause the default browser to be opened with a test page being displayed for testing the web service as shown in the following screen shot.
                                          Picture
                                          Enter your name in the edit box towards the bottom of the screen and press the sayHellobutton. This will invoke the web service and display a HTML page showing the results of the web service and details of the SOAP request and response.
                                          This article shows how simple web services can easily be developed and tested in Java EE 5 using the Glassfish application server. Further information about NetBeans can be found on the NetBeans site. Further information on the Glassfish application server can be found on the Glassfish site.
                                          You can discuss this article here.

                                          Locations of visitors to this page

                                          Copyright (C) DevelopInJava.com 2006-2012