Starting in 1996, Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to the Wayback Machine after an embargo period.
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:
Java Standard Development Kit (JDKTM) version
1.4.2 (download)
or 5.0 (download)
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.
Choose Tools > Server Manager from the main window.
Click Add Server. Select the server type and give
a name to the instance. Then click Next.
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
Choose File > New Project. Under Categories, select Web.
Under Projects, select Web Application and click Next.
Under Project Name, enter HelloWeb. Notice
that the Context Path is /HelloWeb.
Change the
Project Location to any directory on your computer. From now
on, we will refer to this directory as $PROJECTHOME.
Select the recommendations to which your source structure will adhere:
Select the server to which you want to deploy your application. Only
servers that are registered with the IDE are listed.
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
Expand the HelloWeb project node and the Source Packages
node. Note the Source Packages node only contains an empty default
package node.
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
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
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.
Expand the Methods node and delete all the default methods.
In the Source Editor, type the following code in line 16, directly
below the class declaration:
String name;
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;
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
Right-click the word name in the field
declaration on line 15 and choose Refactor > Rename.
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.
Click Do Refactoring. All checked references to the field are
renamed.
Generating Getter and Setter Methods
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.
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
Expand the HelloWeb project node and the Web Pages node.
Note the IDE has created a default JavaServer Pages page, index.jsp,
for you.
Double-click index.jsp. It opens in the Source
Editor.
Paste or type the following code into the body of index.jsp,
to replace the default <body> tags and their contents:
<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
Expand the HelloWeb project node and the Web Pages node.
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.
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.
Press Space and the project offers all the variables that
are applicable to <jsp:useBean>. Select id
and type "mybean" between the quotes.
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.
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:
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
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.
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.
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.
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
In the Files window, expand the HelloWeb project node and the nbproject
folder.
Double-click project.properties to view all of the Ant properties
generated by the IDE for the project.
Copy the line containing build.dir=build. This property sets
the output directory for compiled classes.
In the Files window, expand the private folder and double-click
private.properties.
Paste the build.dir=build property into the file and change
the property to read build.dir=build/production
Choose Build > Clean and Build Main Project (Shift-F11). The compiled
classes are built to the build/production folder.
Setting VM Arguments
Open the private.properties file if it is not open already.
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
In the Files window, go to the nbproject folder for the
HelloWeb project.
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.
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
.