NetBeans IDE 4.1 Quick Start Guide

Source: Internet
Author: User
Tags end string version tostring web services in domain stringbuffer netbeans
Quick Start

WEB services are distributed application components that meet certain criteria and are therefore available for external use. The proliferation of distributed environments presents new requirements for enterprises, which must be able to expose all or part of an application's functionality to other applications through an open network. With Web services, you can solve the integration problems of applications that are independently developed and run on a variety of software and hardware platforms.

The NetBeans IDE 4.1 is bundled with JSR-109 Web service support, JSR-109 is a development paradigm that works with Jax-RPC (JSR-101) based Java EE development. The Web services feature in NetBeans IDE 4.1 is part of the End-to-end Java feature set. Using WEB services in NetBeans IDE 4.1 is much easier than using NetBeans IDE 4.0. For example, NetBeans IDE 4.1 provides wizards for creating Web services and Web service clients.

In this tutorial, you will create a Java EE application that contains one of the following:

WEB applications (services exposed using the Servlet)

EJB modules (Services exposed using session beans)

Note that you must select which of the two scenarios you want to generate. You can use this tutorial to create a WEB application or EJB module.

This tutorial describes the following topics:

Setting up the environment

Developing WEB Services

Exposing Web Services

Using WEB Services

Wrapping Applications

Setting up the environment

Before you start writing code, you must make sure that all the software you need is installed and the server is set up correctly.

Installing software

Before you start the installation, you need to install the following software on your computer:

· NetBeans IDE 4.1 (download)

· Sun Java System (SJS) application Server Platform Edition 8 2005q1 (download)

· Java Standard Development Kit (JDKTM) 1.4.2 version (download) or version 5.0 (download)

Registering the Sun Java System application Server

Before you can compile a WEB service, you must register a local instance of SJS application Server. If the NetBeans IDE 4.1/SJS Application Server package is installed, the local application server is automatically registered.

1. Select Tools > Server Manager from the main window.

2. Click Add Server. Select "Sun Java Systems Application Server 8.1" and specify a name for this instance. Then click Next.

3. Specify server information, the location of the local instance of the application server, and the domain to deploy.

Developing WEB Services

It is easy to encode a Web service. The IDE handles all implementation details, so you can focus on coding the business logic of the WEB service.

Create a project

1. Decide whether to implement the Web service as a Web application or as a session Bean.

2. Select "File" > "New Project" (Ctrl-shift-n), and then do one of the following:

o under Categories, select Web. Under Projects, select Web application. Click "Next".

o under Categories, select Enterprise. Under Projects, select Enterprise JavaBeans module. Click "Next".

3. Name the project HIWS, change the project folder to any folder on your computer, and make sure that the Sun Java System application Server is selected in the Server Drop-down list. Click Finish.

Creating a Web Service

1. Right-click the project node and choose New > Web service. Name the Web service HIWS, type Org.me.hi in the Package field, and then click Finish. An implementation class named Hiwsimpl.java is opened in the source editor (in a Web application), or a session Bean called Hiwsbean.java (in an EJB module) is opened.

2. In the Projects window, expand the Web Services node, right-click the HIWS Web Service node, and choose Add action. Type Sayhi in the Name field, and then select string in the Type combo box. Click Add to define a java.lang.String type parameter named S. Click OK to create the action frame.

3. Expand the Source Packages node and the Org.me.hi node. Double-click the Hiwssei.java node to notice that the operation has been declared in the interface class.

4. In the source editor, fill in the action frame into the implementation class or session Bean. To do this, replace "return null;" With the following code in the body of the Sayhi action:

5. Return "Hi" + S + "!";

6. The code now looks like this:

Package Org.me.hi;

public class Hiwsimpl implements Hiwssei {

Public String Sayhi (java.lang.String s) {

Return "Hi" + S + "!";

}

}

Generating and configuring SOAP message handlers

1. Right-click the project node and select New > Files/folders. Under Categories, select Web Services. Under File types, select message handlers. Click "Next".

2. Name the message handler Hiwslogger, select Org.me.hi in the Package Drop-down list, and then click Finish. An implementation class named Hiwslogger.java will be created and opened in the source editor.

3. In the HandleRequest method, note that the message to be logged is defined as follows:

4. StringBuffer message = new StringBuffer ();

5. while (Childelems.hasnext ()) {

6. Child = (soapelement) childelems.next ();

7. Message.append (New Date (). toString () + "--");

8. Formlogmessage (child, message);

9.}

10.

SYSTEM.OUT.PRINTLN ("message:" + message.tostring ());

12.

13. In the Projects window, expand the Web Services node, right-click the node for the Web service, and choose Configure handlers. In the Configure SOAP Message Handler dialog box, click Add, and then browse to the Hiwslogger class. Click OK.

The message handler class is listed in the dialog box.

Click OK to complete the configuration of the SOAP message handler.

Exposing Web Services

When exposing a Web service, you can make it available to the client machine. You can use the IDE as a client to test it after deployment.

Deploying WEB Services

Depending on how your WEB service is implemented, do one of the following:

· For Web services implemented as Web applications, do the following:

1. Right-click the HIWS project node in the Projects window and choose Properties. In the Project Properties dialog box, select the Run pane. Type/HIWS in both the context Path text box and the relative URL text box. Click OK.

2. Right-click the HIWS project in the Projects window and choose Run Project.

This will start the Java-compliant server and deploy the Web service. A message similar to the following should appear in the IDE's default browser:

Invalid WSDL request Http://localhost:8080/HiWS/HiWS for Web service HIWS

· For a WEB service that is implemented as a session Bean, right-click the HIWS project in the Projects window and choose Deploy Project. This will start the Java-compliant server and deploy the Web service.

Note that the Web service must be kept deployed in order to create a Web service client in the next section.

Registering and testing a WEB service

1. In the Projects window, expand the Web Services node, right-click the HIWS Web Service node, and choose Add to Registry.

You may receive an error message similar to the following:

Unable to the Add Web service to registry. Make sure it has been deployed,

The server is running, and the correct WSDL URL was entered.

If you receive this error message, make sure that you have deployed the WEB service and then select Add to registry again.

A dialog box appears that recommends the URL that you can use to register the WEB service.

2. Note The URL, because you will need to use it later when you create a WEB service client. Click OK.

 

3. In the Run Environment window, expand the Web Services node, and then continue to expand the node until you find the Sayhi node. Right-click the Sayhi node and choose Test Actions. In the Test Web Service Action dialog box, type "John" in the Value text box, and then click Submit. The result "Hi john!" will be displayed at the bottom of the dialog box.

4. Expand the Servers node, right-click the Sun Java System Application Server 8.1 node, and select View server log. The Server.log file will be displayed, including a message similar to the following:

5. message:wed 16:56:48 CEST 2005--sayhi String_1:john |#]

This is the log message generated by the SOAP message handler created in the previous section.

After you have tested the operation, if you are satisfied with the Web service, you can integrate its functionality into the Web service client.

Using WEB Services

Using a Web service is the action that a client performs when it uses a Web service. Using the IDE makes it easy to set up a client to use a WEB service. You can use the wizard to import a WSDL file that describes the Web service interface. It can then be integrated into the client to enable the Web service to do some useful work.

Creating a WEB Application project

· Select File > New Project (ctrl-shift-n). Under Categories, select Web. Under Projects, select Web application. Name the project Hiwsclient, change the project folder to any folder on your computer, and make sure that the Sun Java System application Server is selected in the Server Drop-down list. Click Finish.

Search for information about a WEB service

1. Right-click the project node and choose New > Web Service client. Copy the URL of the running Web service and paste it into the WSDL URL text box, and then click Retrieve WSDL. If the WSDL filename is filled in in the local File name text box, the WSDL file has been retrieved correctly. Type Org.me.hi in the Package field, and then click Finish.

2. Expand the Web Service References node, and then continue to expand the node until you find the Sayhi node. Right-click the Sayhi node and choose Test Actions. In the Test Web Service Action dialog box, type "John" in the Value text box, and then click Submit. The result "Hi john!" will be displayed at the bottom of the dialog box. After testing the operation, if you are satisfied with the exposed Web service, you can create a client to use it.

3. Right-click the Hiwsclient project node and choose New > Servlet. Name the Web service Hiservlet, type Org.me.hi in the Package field, click Next, and then click Finish. The Servlet Hiservlet.java is opened in the source editor.

Creating a Web service client

1. In the source Editor, click the right mouse button in the ProcessRequest action of the Hiservlet class. Select the Web service Client Resource > Invoke Web Service operation, select the Sayhi action, and then click OK. Cut and paste the frame operation and place it in the Out.close (); Above the line.

2. You can now fill out the frame operation so that it looks like this:

3. try {

4. Out.println (Gethiwsseiport () sayhi ("Ludwig"));

5.} catch (Java.rmi.RemoteException ex) {

6. Out.println ("

Caught an exception

"+ ex);

7.}

8. (optional) If you want users to be able to interact with the Web service, remove the above code and add the following code between the tags of the ProcessRequest method:

       
        
         
        Out.println ("<p>enter Your Name:"); Out.println ("<form method=\" get\ ">"); Out.println ("<input type=\" text\ "name=\" name\ "size=\" 25\ ">"); Out.println ("<br>"); Out.println ("<p>"); Out.println ("<input type=\" submit\ "value=\" submit\ ">"); Out.println ("</form>"); String name = Request.getparameter ("name"); if (name!= null) {try {out.println (Gethiwsseiport (). Sayhi (name)), catch (Java.rmi.RemoteException ex) {out.println ( "<p>caught an exception <p>" + ex); } }
       
        



Wrapping Applications

Optionally, you can wrap the application in the EAR file and deploy it. Alternatively, you can deploy the Web service and the Web service client separately.

Create a Java EE application project

1. Select "File" > "New Project" (Ctrl-shift-n). Under Categories, select Enterprise. Under Projects, select Empty enterprise applications. Name the project Hiapp, and then change the project folder to any folder on your computer. Click Finish.

2. Expand the Hiapp project node, and then right-click the Java EE Module node. Select "Add Java EE module". Select Hiwsclient and click OK. Then repeat this step and select "Hiws".

Deploy the Java EE application

1. Right-click the Hiapp project node in the Projects window and choose Properties. In the Project Properties dialog box, select Execute. Make sure the Hiwsclient.war is filled in in the Client module URI text box. Type/hiservlet in the relative URL text box. Click OK.

2. Right-click the Hiapp project in the Projects window and choose Run Project.

You may receive an error message similar to the following:

Deploying application in Domain failed; Cannot deploy. Application already exists.

Please select the redeploy option. ; Requested operation cannot be completed

If you receive this error message, expand the Sun Java System Application Server 8 node in the Server registration node of the Run Environment window. then expand the Applications node, and then expand the Enterprise Applications node and the WEB application node. If Hiapp, HIWS, or hwsclient are listed, you need to remove them. Right-click the nodes and choose Remove. In the Projects window, run the Hiapp project again.

The Java-compliant server is launched, the application is deployed, and the deployment results are displayed in the IDE's default Web browser.

3. If you added a user interface to your application (as described in the previous section), enter a name, and then click Submit. The client will use the "Hi" and "!" strings in the Web service to insert the name you entered and to display the greeting in the browser. If no user interface is added, the browser displays the message "Hi ludwig!".



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.