FeaturesPluginsDocs & SupportCommunityPartners

NetBeans IDE 4.0 Quick Start Guide for Web Applications

Feedback

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.

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

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.

Setting Up a Project

Creating a new web application
  1. Choose File > New Project. Under Categories, select Web. Under Projects, select Web Application and click Next.
  2. Under Project Name, enter HelloWeb. Change the Project Location to any directory on your computer. From now on, we will refer to this directory as $PROJECTHOME.
  3. Leave the Set as Main Project checkbox selected. Notice that the Context Path is /HelloWeb.
  4. 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 Ant 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 Java Source Code

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 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.

Refactoring

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. Uncheck the check box next to any reference to exclude it from the refactoring.
  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;
            }
        }

Creating and Editing JavaServer Pages Files

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, in line 6, 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>

Compiling and Running a Project

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. Note that Ant builds HelloWeb.war first and then compiles HelloWeb using it. Finally, it deploys the web application using your default server. 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, but 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.

Debugging a Project

Debugging a project
  1. Select the NameHandler.java tab in the Source Editor, place the caret in setUsername(null); (in the NameHandler constructor), and press Alt-G. The caret jumps to the setUsername method.
  2. In the setUsername method, place the caret anywhere inside this.username=username;. Then press Ctrl-F8 to set a breakpoint.
  3. Choose Run > Debug Main Project (F5). The IDE opens the Debugger windows.
  4. Enter your name. The IDE runs the project in the debugger until the breakpoint is reached.
  5. Click Continue (Ctrl-F5) on the main menu to step through the program and watch the name being constructed. When the program ends, the debugger windows close.

Customizing the Build Process

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 runing 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

Coin Flip Tutorial This tutorial shows you how to create a web application with interactions between a JavaServer Pages page, a JavaBeans component, and a Java servlet.
Midnight Cookie Company Tutorial This tutorial shows you how to build, deploy, and execute a web application that contains JavaServer Pages pages and Java servlets and that uses tags from the JSTL tag library. It also shows how to use the HTTP monitor to view the requests and responses between the server and the web browser.
Companion
Projects:
MySQL Database Server   Open JDK: an Open SourceJDK   GlassFish Community: an Open Source Application Server    Mobile & Embedded Community    Open Solaris   java.net - The Source for Java Technology Collaboration   Virtual Box - full virtualizer  Open ESB - The Open Enterprise Service Bus Powered by