In order to work on a file in NetBeans, it is necessary to create a project.
U:drive, do not create the project in the
webappsfolder. I suggest that you create a folder named
cgs4854directory, and place your projects there.)
A Web Project in NetBeans is set of directories and files that allow for servlets and JSPs to be executed and debugged.
context.xmlfile that defines the URL that is used to access this application.
web.xmlfile that defines many aspects of the web application, including the URLs to access servlets.
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.
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).
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
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....
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
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.