Introduction to Developing Web Applications

This document takes you through the basics of using NetBeans IDE 6.0 to develop web applications. This document is designed to get you going as quickly as possible. For more information on working with NetBeans IDE, see the Support and Docs page on the NetBeans website. You create, deploy, and execute a simple web application. The application uses a JavaServer Pages™ page to ask you to input your name. It then uses a JavaBeans™ component to persist the name during the HTTP session and repeats the name on another JavaServer Pages page.

Contents

  Content on this page applies to the NetBeans 6.0 IDE

Installing the Software

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


Setting Up a Web Application Project

  1. Choose File > New Project. Under Categories, select Web. Under Projects, select Web Application and click Next.
  2. Under Project Name, enter HelloWeb. Notice that the Context Path is /HelloWeb.
  3. Change the Project Location to any directory on your computer. From now on, this directory is referred to as $PROJECTHOME.
  4. Select the server to which you want to deploy your application. Only servers that are registered with the IDE are listed. Click Next.
  5. Leave the Set as Main Project checkbox selected. Click Finish.

    The IDE creates the $PROJECTHOME/HelloWeb project folder. The project folder contains all of your sources and project metadata, such as the project's Ant build script. The HelloWeb project opens in the IDE. You can view its logical structure in the Projects window and its file structure in the Files window.

Creating and Editing Web Application Source Files

Creating and editing source files is the most important function that the IDE serves. After all, that's probably what you spend most of your day doing. The IDE provides a wide range of tools that can compliment any developer's personal style, whether you prefer to code everything by hand or want the IDE to generate large chunks of code for you.

Creating a Java Package and a Java Source File

  1. Expand the Source Packages node. Note the Source Packages node only contains an empty default package node.
  2. Right-click the Source Packages node and choose New > Java Class. Enter NameHandler in the Class Name text box and type org.me.hello in the Package drop-down. Click Finish.
  3. In the Source Editor, declare a field by typing the following line directly below the class declaration:
        String name;
  4. Add the following constructor to the Java class:
        public NameHandler()
  5. Add the following line in the NameHandler() constructor:
        name = null;

Generating Getter and Setter Methods

  1. Right-click the word name in the field declaration at the start of the class and choose Refactor > Encapsulate Fields.
  2. Click Refactor. Getter and setter methods are generated for the name field and its access level is changed to private. The Java class should now look similar to this:
        package org.me.hello;
    
        /**
         *
         * @author Administrator
         */
    
        public class NameHandler {
    
    	private String name;
    
            /** Creates a new instance of NameHandler */
            public NameHandler() {
               name = null;
            }
    
            public String getName() {
               return name;
            }
    
            public void setName(String name) {
               this.name = name;
            }
    
        }

Editing the Default JavaServer Pages File

  1. Expand the HelloWeb project node and the Web Pages node. Note that the IDE has created a default JavaServer Pages file, index.jsp, for you. When you create the project, the IDE opened the index.jsp file in the Source Editor.
  2. Select the index.jsp Source Editor tab. The index.jsp file now has focus in the Source Editor.
  3. In the Palette on the right side of the Source Editor, expand HTML Forms and drag a Form item below the <h2> tags in the Source Editor.

    The Insert Form dialog box appears:

    Palette

    Set the following values:

    • Action: response.jsp
    • Method: GET
    • Name: Name Input Form

    Click OK. The Form is added to the index.jsp file.

  4. Drag a Text Input item to just before the </form> tag.

    Set the following values:

    • Name: name
    • Type: text

    Click OK. The Text Input is added between the <form> tags.

  5. Drag a Button item to just before the </form> tag.

    Set the following values:

    • Label: OK
    • Type: submit

    Click OK. The Button is added between the <form> tags.

  6. Change the text between the h2 tags to Entry Form.
  7. Type Enter your name: in front of the <input> tag and change the text between the <h2> tags to Entry Form.

    The tags between the <body> tags now look as follows:

  8. <h2>Entry Form</h2><form name="Name Input Form" action="response.jsp" method="GET">
    Enter your name: <input type="text" name="name" value="" />
    <input type="submit" value="OK" /></form>

Creating a JavaServer Pages File

  1. Expand the HelloWeb project node and the Web Pages node.
  2. Right-click the Web Pages node and choose New > JSP, name the JavaServer Pages file response, and click Finish.

    The new response.jsp opens in the Source Editor.

  3. In the Palette on the right side of the Source Editor, expand JSP and drag a Use Bean item right below the <body> tag in the Source Editor.

    Set the following values:

    • ID: mybean
    • Class: org.me.hello.NameHandler
    • Scope: session

    Click OK. The Use Bean is added below the <body> tag.

  4. Change the text between the <h2> tags look like this:
    <h2>Hello, !</h2>
  5. Drag a Get Bean Property item fromt he Palette and drop it after the comma between the <h2> tags.
  6. Drag a Set Bean Property item from the Palette. Then change the code so that the tags between the <body> tags look as follows:
  7. <jsp:useBean id="mybean" scope="session" class="org.me.hello.NameHandler" />
    <jsp:setProperty name="mybean" property="*" />
    <h2>Hello, <jsp:getProperty name="mybean" property="name" />!</h2>

Building and Running a Web Application Project

The IDE uses an Ant build script to build and run your web applications. The IDE generates the build script based on the options you enter in the New Project wizard and the project's Project Properties dialog box.

  1. Choose Run > Run Main Project (F6) from the Run menu.

    The IDE builds the web application and deploys it, using the server you specified when creating the project.

  2. Enter your name in the text box on your deployed index.jsp page:

    Palette

    Click OK. The response.jsp page should open and greet you:

    Palette

Next Steps

For more information about developing web applications in NetBeans IDE 6.0, 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 [email protected] mailing list.


This page was last modified: November, 2007