Download NetBeans

Download Netbeans to your local computer. Be sure to download a version of Netbeans that includes Tomcat. The labs are still using NetBeans 8.2, so that is the one I recommend that you download to your computer. NetBeans is transitioning from Oracle to Apache. For now, we will stay with the last, stable, Oracle release.

If you have already installed Netbeans and did not install the Web version or the full version, then download either of those versions and reinstall. Netbeans will upgrade your current installation.

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.
  6. Select Tomcat as the Server. (If Tomcat is not listed, then you did not download the correct package. Download the correct package.)
  7. Set the Java EE version to Java EE 7 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.
META-INF
This contains the context.xml file that defines the URL that is used to access this application.
WEB-INF
This contains the web.xml file that defines many aspects of the web application, including the URLs to access servlets.
index.jsp or index.html
This is the default web page when this web application is loaded from Tomcat. Place links in this page to your main servlet. Some versions of NetBeans use .html and some use .jsp. Use whichever file has been created for you.
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.
Libraries
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.

    <servlet>
        <servlet-name>ExampleServlet</servlet-name>
        <servlet-class>my.pack.FirstServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ExampleServlet</servlet-name>
        <url-pattern>/my/pack/FirstServlet</url-pattern>
    </servlet-mapping>

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.