The Wayback Machine - https://web.archive.org/all/20050515004823/http://www.netbeans.org:80/kb/41/quickstart-webapps.html

NetBeans IDE 4.1 Quick Start Guide for Web Applications

This document takes you through the basics of using NetBeans IDE 4.1 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.

We will create, deploy, and execute a simple web application. The application uses a JavaServer PagesTM page to ask you to input your name. It then uses a JavaBeansTM component to persist the name during the HTTP session and repeats the name on another JavaServer Pages page.

Sample Projects

Just want to play with some projects? In the IDE, choose File > New Project, then look under the Samples folder. The IDE includes both web apps and J2SE sample projects.


Setting Up a Web Application Project

Before you start writing code, you have to make sure you have all of the necessary software and that your project is set up correctly.

Installing the Software

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

Optionally, you can download and use the Sun Java System (SJS) Application Server Platform Edition 8 2005Q1 (download). However, the Tomcat Web Server that is bundled with the IDE provides all the support you need for two-tier web applications such as the one described in this quick start guide. The SJS Application Server is only required when you want to develop enterprise applications.

Registering the Server

The bundled Tomcat Web Server is registered with the IDE automatically. However, before you can deploy to the SJS Application Server, you have to register a local instance with the IDE. If you installed the NetBeans IDE 4.1/SJS Application Server bundle, the local application server is registered automatically.

  1. Choose Tools > Server Manager from the main window.
  2. Click Add Server. Select the server type and give a name to the instance. Then click Next.
  3. Specify the server information, the location of the local instance of the application server, and the domain to which you want to deploy.

Creating a New 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, we will refer to this directory as $PROJECTHOME.
  4. Select the recommendations to which your source structure will adhere:
  5. Select the server to which you want to deploy your application. Only servers that are registered with the IDE are listed.
  6. 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 JavaBeans Component

  1. Expand the HelloWeb project node and 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 > File/Folder. Under Categories, select JavaBeans Objects. Under File Types, select JavaBeans Component and click Next. Enter NameHandler in the Class Name text box and enter org.me.hello in the Package combo box. Click Finish.

Editing the JavaBeans Component

  1. Expand the NameHandler.java node and double-click the NameHandler class node. In the Source Editor, delete the following part of the class declaration:
        extends Object implements Serializable
  2. Expand the NameHandler class node and the Fields node. Three default fields are provided. Right-click the PROP_SAMPLE_PROPERTY field and choose Delete from the contextual menu. Do the same for the other two fields. Notice that the lines of code that use these deleted fields are underlined in red in the Source Editor.
  3. Expand the Methods node and delete all the default methods.
  4. In the Source Editor, type the following code in line 16, directly below the class declaration:
        String name;
  5. Expand the Constructors node and double-click the NameHandler constructor. In the Source Editor, edit the NameHandler() constructor by replacing its default code (propertySupport = new PropertyChangeSupport(this);) in line 18 with the following:
        name = null;
  6. Press Alt-Shift-F in the Source Editor to update the import statements so that your code specifies only those that are needed.

Renaming a Field

  1. Right-click the word name in the field declaration on line 15 and choose Refactor > Rename.
  2. In the New Name field, type username. Then click Next.
    The Refactoring window previews all of the references that will be changed to point to the newly named field. Double-click any reference to jump to its location in the Source Editor. The check boxes indicate which of the references will be refactored.
  3. Click Do Refactoring. All checked references to the field are renamed.

Generating Getter and Setter Methods

  1. Right-click the word username in the field declaration on line 15 and choose Refactor > Encapsulate Fields. Click Next to run the command with its default options.
  2. Click Do Refactoring. Getter and setter methods are generated for the username field and its access level is changed to private. The JavaBeans component should now look like this:
        package org.me.hello;
        public class NameHandler {
            private String username;
            public NameHandler() {
                setUsername(null);
            }
            public String getUsername() {
                return username;
            }
            public void setUsername(String username) {
                this.username = username;
            }
        }

Editing the Default JavaServer Pages File

  1. Expand the HelloWeb project node and the Web Pages node. Note the IDE has created a default JavaServer Pages page, index.jsp, for you.
  2. Double-click index.jsp. It opens in the Source Editor.
  3. Paste or type the following code into the body of index.jsp, to replace the default <body> tags and their contents:
  4.     <body>
            <form method="post" action="response.jsp">
            Enter your name: <input type="text" name="username">
            <br>
            <input type="submit" value="Ok">
            </form>
        </body>

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.
    response.jsp opens in the Source Editor.
  3. Below the <body> tag, type <jsp:u and wait. When the code completion box appears, see the popup Javadoc for the <jsp:useBean> syntax. If the box does not appear, press Ctrl-Space. Press Enter.
  4. Press Space and the project offers all the variables that are applicable to <jsp:useBean>. Select id and type "mybean" between the quotes.
  5. Press Space after the final quote and select class. Press Ctrl-Space between the quotes to open the code completion box. The project offers code completion for all packages and classes in the project's compilation classpath. Select org and press Enter.
  6. Enter a period after org and press Ctrl-Space. The code completion box opens again. Select me, and continue using code completion such that the line reads as follows:
        <jsp:useBean id="mybean" class="org.me.hello.NameHandler" />
  7. Paste or type the following code into the body of response.jsp, directly below the <jsp:useBean> tag:
  8.     <jsp:setProperty name="mybean" property="*" />
        <h1>Hello, <jsp:getProperty name="mybean" property="username" />!</h1>

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.

Building a Project

  • Choose Build > Build Main Project (F11). The HelloWeb project is built.

Running the Main Project

  1. Choose Run > Run Main Project (F6) from the Run menu. Double-click the Output window's titlebar to maximize it so you can see all the output. Finally, it deploys the web application using the server you specified when creating the project. Double-click the Output window's titlebar again to minimize it.
  2. Enter your name in the text box on your deployed index.jsp page and click OK. The response.jsp page should open and greet you.
  3. Select the Files window and expand the HelloWeb project node. The build class files are in the build folder. The build WAR file is in the dist folder.
  4. Press F6 to run the program again. Nothing new needs to be compiled and the program is run.

Generating Javadoc

  • Right-click the project node and choose Generate Javadoc for Project. Javadoc output appears in the Output window, and your web browser opens displaying the Javadoc.

Customizing the Build Process

You can customize the build process by doing any of the following:

  • Enter basic options, like classpath settings and JAR filters, in the New Project wizard when you create a project, or afterwards in the Project Properties dialog box.
  • Customize existing Ant targets.
  • Edit properties in project.properties to change the name and location of build output folders and files.

The subsections below guide you through some of the IDE's customizing options.

Overriding an Ant Property

  1. In the Files window, expand the HelloWeb project node and the nbproject folder.
  2. Double-click project.properties to view all of the Ant properties generated by the IDE for the project.
  3. Copy the line containing build.dir=build. This property sets the output directory for compiled classes.
  4. In the Files window, expand the private folder and double-click private.properties.
  5. Paste the build.dir=build property into the file and change the property to read build.dir=build/production
  6. Choose Build > Clean and Build Main Project (Shift-F11). The compiled classes are built to the build/production folder.

Setting VM Arguments

  1. Open the private.properties file if it is not open already.
  2. Enter a new line anywhere in the file, type run.jvmargs=-J-Xms24m -J-Xmx160m, and choose File > Save. The project will be run with the specified heap size and maximum memory.

Adding to an Ant Target

  1. In the Files window, go to the nbproject folder for the HelloWeb project.
  2. Double-click build-impl.xml to open it in the Source Editor. This file contains all of the Ant targets generated by the IDE. Each target has a -pre target and a -post target that you can use to add processing instructions that the IDE runs before or after running the target. Do not change the targets in this file — this file is generated automatically by the IDE and any changes you make will be lost.
  3. In the Files window, go to the HelloWeb project folder and double-click build.xml. This is where you override targets from build-imp.xml.

Next Steps

For more information about using NetBeans IDE 4.1, see the following resources:

To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE J2EE development features, join the mailing list .

 
 

Related Documents

A short guide to getting your existing web applications into NetBeans IDE 4.1.
Create and deploy a complex web application.
Quickly create, build, and execute a simple a simple J2SE application.
Create, expose, and consume a simple web service.
Generate a set of CMP entity beans from a database and access the beans from a Web application client.
Complete list of documentation for NetBeans IDE 4.1.