Creating a Project in NetBeans

In order to work on a file in NetBeans, it is necessary to create a project.

  1. From the File menu, select New Project...
  2. Choose the Category of project as Web
  3. Choose the Project type as Web Application
  4. Enter a Project Name and a Project Location. (If you are creating this project in the JCCL on the U: drive, do not create the project in the webapps folder. I suggest that you create a folder named local in the cgs4854 directory, and place your projects there.)
  5. Use the default context path, and use J2EE 1.5 or greater.
  6. Select Tomcat as the Server. (If Tomcat is not listed, then you are using NetBeans 6.0 and did not download the correct package. Download the correct package and update your Netbeans 6.0 installation.)
  7. Set the Java EE version to J2EE 1.5 or greater.
  8. After clicking finish, there should be a project listed in your Projects tab. It will look something like this (in NetBeans 6.0, the Runtime tab is named Services)

What is a Web Project in NetBeans?

A Web Project in NetBeans is set of directories and files that allow for servlets and JSPs to be executed and debugged.

Web Pages
This directory is for HTML pages, images, CSS style sheets, and JSP pages.
This contains the context.xml file that defines the URL that is used to access this application.
This contains the web.xml file that defines many aspects of the web application, including the URLs to access servlets.
This is the default web page when this web application is loaded from Tomcat. Place links in this page to your main servlet.
Source Packages
Define all source files for Java Servlets. Place all source files in a package.
<default package>
This is the package that has no name. Avoid using it. It is better to always place your servlets in a named package. Strange things can happen if you use the default package.
To add JAR files to your web application, follow these steps.
  1. Download the JAR files to your computer.
  2. Open NetBeans and right-click on the project name in the Projects tab.
  3. Select Properties.
  4. Select Libraries.
  5. Click the Add Jar/Folder button.
  6. Navigate to the directory where the downloaded JAR files are.
  7. Select all the JAR files you want to include in the web application.

Place Servlets in Packages

All servlets for this course should be contained in a package.

Right-click on Source Packages and select New, then select Java Package.

Type in the full name of the Java package, for instance my.pack, then click Finish.

Right click on the new package and create a new class.

Name it FirstServlet.

After completing these steps, you can see the servlet in the Source Packages folder.

Servlets must be Identified as Servlets

If you right-click the servlet in the file tree, and select Run, you will get this error:

Newer versions of NetBeans do not add a web.xml to the web application, automatically. To add a web.xml to your web app, select File -> New File -> Web -> Standard Deployment Descriptor (web.xml).

The solution is to add a servlet mapping for this servlet to the web.xml file. I like to use a url-pattern that is the same as the full name of the class, replacing each period with a slash. For instance, if the class is my.pack.FirstServlet then define the URL as /my/pack/FirstServlet. Notice that the servlet-name tag does not have to agree with the actual name of the servlet.


Open the web.xml file. There are many ways to view the contents of the web.xml file, click the XML tab above the edit window to view the XML source.

Define the servlet and servlet-mapping tags.

If you try to run the servlet and get a log in screen for the manager, then you will need to set the username and password for the manager. Do this from the Tools -> Server Manager menu (Tools -> Servers in NetBeans 6.0).

Including Source Files in the WAR File

By default, the .java files are not added to the WAR file. To have them added to the WAR file when the project is built, right-click on the name of the project in the project window and select properties.

From the properties screen, select Build -> Packaging. Edit the Exclude From WAR File box and remove **/*.java,.

Add a jar File

Later in the course, additional features will be added to a web application by using jar files. Download the jar file to your system, then right-click the Libraries folder and select Add JAR/Folder....


Where is the Web Application?

A Project in NetBeans is an abstraction of a web application. The actual directory and file structure is in the location that was specified when the application was created. However, not all of the directories are visible from the project view, but they are visible from the Files view.

The web directory contains the actual web application directory for your project.

The src files are located in the src directory.

The .class files for your servlets are located in the build directory. Open the web sub-directory of build. In the WEB-INF is located the classes directory, which contains the .class files for the servlets.

Whenever the project is cleaned and built (select Clean and Build from the Build menu), a Web Archive (WAR) file is created. This WAR file is actually a ZIP file that contains the web application. The WAR file contains a web application that has a standard format. The WAR file is located under the Files tab in the dist folder.