Chapter 3

Chapter 3
How to use
the NetBeans IDE
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 1
Objectives
Applied
 Install and configure NetBeans so it uses a Tomcat server and a
SQL database.
 Use NetBeans to develop and test web applications that consist of
HTML files, JSP files, servlets, and regular Java classes.
 Use NetBeans to add existing files to a project or to add a class
library or a JAR file to a project.
 Use a WAR file that was created by NetBeans to deploy an
application to a remote server.
 Use NetBeans to start, stop, or undeploy a web application.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 2
Objectives (cont.)
Knowledge
 Describe a NetBeans project or main project.
 In general terms, describe the difference between the Projects
window and the Files window for a NetBeans project.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 3
The NetBeans web site
www.netbeans.org
How to install NetBeans
1. Go to the NetBeans web site.
2. Download the Web and Java EE pack for NetBeans 6.0. On a
Windows system, the exe file should be named something like
netbeans-6.0-javaee-windows.exe. This download will include a
bundled version of Tomcat.
3. Run the install file and respond to the resulting dialog boxes. If
you’ve already installed Tomcat, you can uncheck the Tomcat box
when that option is offered so it isn’t installed again.
The default installation folder
C:\Program Files\NetBeans 6.0
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 4
The Welcome page
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 5
How to start NetBeans
 You can start NetBeans by selecting it from the Start menu just as
you would for any other program. When you start NetBeans for the
first time, it usually displays a Start Page tab.
 If the Start Page tab isn’t displayed on startup, you can display it by
selecting the Start Page command from the Help menu.
 You can use the ToolsJava Platforms command to find out what
version of Java the NetBeans IDE is using.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 6
The Servers dialog box
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 7
How to configure a Tomcat server

To view the available servers, select the ToolsServers command from the
menu system.

By default, NetBeans comes with a bundled version of Tomcat named
Apache Tomcat that runs on port 8084.

By default, NetBeans comes with a bundled version of the Glassfish
application server, which is also known as the Sun Java System Application
Server.

To change the settings for a server, select the server in the Servers pane and
use the other tabs to modify the settings for that server.

To add another server, click on the Add Server button and respond to the
dialog boxes.

After you add a server, you can specify the port that’s used by the server.

To remove a server, select the server in the Servers pane and click the
Remove button.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 8
The first Add Server Instance dialog box
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 9
The second Add Server Instance dialog box
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 10
The dialog box for resolving a database connection
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 11
The dialog box for registering the database
connection
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 12
How to register a database connection for a
project
1. Right-click on the project in the Project window and select the
Resolve Data Source Problem command to display the Resolve Data
Sources dialog box.
2. Select the data source that you want to resolve, and click the Add
Connection button to display the New Database Connection dialog
box.
3. If necessary, change any of the entries for the connection. Then,
click on the OK button to register the database connection.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 13
The first dialog box for creating a new web
application
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 14
The second dialog box for creating a new web
application
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 15
How to create a new web application
 To create a new project, select the FileNew Project command.
When you complete the second dialog box, you can click on the
Finish button. Or, you can click on the Next button to display a
third dialog box, make no entries, and then click the Finish button.
 If you want to change the server later, you can select the
FileProperties command for the project, select the Run group,
and select another server.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 16
The default web application
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 17
How to use the Projects window
 When you use NetBeans to create a web application, it creates a
web application that includes a working web.xml file and a JSP
that displays some text.
 To view the files that are available for a web application, you can
expand or collapse the folders that are available in the Projects
window.
 To open a file in the appropriate editor, you can double-click on
the file.
 To rename a file, you can right-click on it, select the Rename
command, and respond to the resulting dialog box.
 To delete a file, you can right-click on it, select the Delete
command, and respond to the resulting dialog box.
 To display the Projects window if it isn’t visible, use the
WindowProjects command.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 18
The Open Project dialog box
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 19
How to work with existing projects
 To open a project, select the FileOpen Project command. Then,
use the Open Project dialog box to locate the folder for the project
and click on the Open Project Folder button.
 When two or more projects are open at the same time, the main
project is displayed in bold in the Projects window.
 To change the main project, right-click on a different project and
select the Set Main Project command.
 To remove a project from the Projects window, right-click on the
project in the Projects window and select the Close command.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 20
The web browser that’s displayed when you run a
project
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 21
How to build, deploy, and run a web application
 To run the main project, press F6 or select the Run Main Project
command from the toolbar or the Run menu.
 When you run a web application, NetBeans automatically compiles all
files that need to be compiled, deploys the files for the project to the
specified server, starts the default web browser, and displays the first
page of the application in that browser.
 To run a specific file, right-click on the file and select the Run File
command. This deploys the web application and displays the file in
the default web browser.
 To change the default browser, select the ToolsOptions command,
select the General category, and select the browser that you want to
use.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 22
How to build, deploy, and run a web application
(continued)
 To view information about a test run, use the tabs in the Output
window. To open this window, use the WindowOutput
command. To clear the data from one of the tabs, right-click in the
window and select the Clear command.
 When NetBeans deploys an application to a local Tomcat server, it
copies an XML file for the web application into Tomcat’s
conf\Catalina\localhost folder. This XML file maps these folders
and files to its root folder on the web server.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 23
The New File dialog box
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 24
How to add an HTML or JSP file
 To add an HTML file, right-click on the Web Pages folder or one
of its subfolders and select the NewHTML command. Then, use
the resulting dialog box to specify the name and location of the file.
 To add a JSP file, right-click on the Web Pages folder or one of its
subfolders and select the NewJSP command. Then, use the
resulting dialog box to specify the name and location of the file.
 To enter a name for the HTML or JSP file, enter the name in the
File Name text box. You don’t need to enter the file extension.
 To specify a location, you can use the Folder text box. To create a
new subfolder, enter the name in the Folder text box. To select an
existing folder, click the Browse button to the right of the Folder
text box and use the resulting dialog box to select the folder where
you want to store the file.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 25
How to add an HTML or JSP file (continued)
 The Created File text box shows the complete path and filename for
the file that will be created.
 For most JSP files, you can use the JSP File (Standard Syntax)
option. The XML Syntax is rarely used.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 26
The HTML and JSP editor
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 27
How to edit an HTML or JSP file
 When you add a new HTML or JSP file to a web application,
NetBeans includes some starting code. You can delete this code or
modify it for use in your application.
 To edit source code, you can use the same techniques that you use
with other text editors.
 After you enter the starting bracket for an HTML or JSP tag, the
code completion feature provides a list of possible entries. Then,
you can select an item and press the Tab or Enter key to insert the
starting code for the tag.
 To insert the ending code for a tag right after the starting tag, press
Ctrl+Spacebar. You can also press Ctrl+Spacebar to activate the
code completion feature at other points in your entries.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 28
How to edit an HTML or JSP file (contined)
 To open the Palette window, use the WindowPalette command.
Then, to add HTML or JSP tags, drag the tag you want to add from
the Palette window into the code editor. Or, move the insertion
point to where you want to insert the code and double-click on the
tag in the Palette window. For most of these tags, NetBeans will
prompt you with a dialog box that gets some information before it
inserts the code into the code editor.
 To identify lines of code with entry errors, NetBeans displays
yellow or red markers in the bar at the left of the editor window.
Then, you can position the mouse pointer over a marker or click on
a marker to get more information about an error.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 29
The New Java Class dialog box
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 30
How to add a Java class
 To add a Java class, right-click on one of the project folders and
select the NewJava Class command. Then, use the resulting dialog
box to specify the name and location of the file.
 To specify a package for the class, use the Package combo box to
select an existing package or to enter the name for a new package.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 31
The first New Servlet dialog box
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 32
The second New Servlet dialog box
How to add a servlet
 Right-click on the package, select the NewServlet command, and
specify a servlet name and URL in the resulting dialog boxes.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 33
The Java editor with an error displayed
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 34
How to edit a Java file
 When you add a Java class or servlet to a web application,
NetBeans includes some starting code. You can delete this code or
modify it for use in your application.
 To enter and edit source code, you can use the same types of
techniques that you use with most code editors.
 After you enter a class or object name and a period, the code
completion feature provides a list of possible entries. Then, you can
select an item and press the Tab or Enter key to insert the code into
the file.
 To activate the code completion feature at other points in your
entries, you can press Ctrl+Spacebar.
 To identify lines of code that contain errors, NetBeans displays red
markers in the bar at the left of the code editor window. Then, you
can position the mouse pointer over a marker or click on a marker
to get more information about an error.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 35
The XML editor with the Pages tab displayed
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 36
The XML editor with the XML tab displayed
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 37
How to work with XML files
 The web.xml file is known as the deployment descriptor (DD).
 To edit an XML file, double-click on it to open it. Then, you can
use the seven tabs across the top of the page to modify the
web.xml file. Of these tabs, you can use the XML tab to manually
edit the file in the XML editor.
 To validate an XML file against its schema, you can right-click on
the file and select the Validate XML command from the resulting
menu. Then, the results of the validation will be displayed in an
XML Check window within the Output window.
 When you run an application after you modify its web.xml file,
NetBeans saves the file and forces Tomcat to read it again so the
changes will take effect. Then, if the elements in the web.xml file
aren’t in the correct order or if there is another problem with the
file, the errors will be noted in the Tomcat and Tomcat Log
windows within NetBeans.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 38
The Files window for the ch03email project
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 39
The folders that NetBeans uses
Folder
build\web
dist
nbproject
src
test
web
Description
Contains all necessary folders and files for the web
application after it has been built.
Contains the WAR file for the application.
Contains the configuration files and build scripts
for the NetBeans project.
Contains the source code for the Java files and
servlets.
Contains the source code for any automated testing
for the project.
Contains the HTML, JSP, and XML files for the
application.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 40
How to add existing files to a project
 Copy the files from the Windows Explorer and paste them into an
appropriate folder in the Projects window or the Files window.
How to deploy an application to a remote server
 Copy the WAR file for the application from the dist folder to the
appropriate folder for web applications on the remote server.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 41
The Services window with the Servers group
displayed
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 42
How to use the Services window
 To open the Services window, use the WindowServices
command. The Services window shows the services and other
runtime resources that are available to NetBeans.
 To view a server, expand the Servers group. Then, you can start,
stop, restart, or refresh the server by right-clicking on it and
selecting the appropriate command.
 To view the web applications that are running on the Tomcat
server, expand the Web Applications folder for the server. Then,
you can start, stop, or undeploy a web application by right-clicking
on it and selecting the appropriate command.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 43
The Add Library dialog box for the JSTL 1.1 library
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 44
How to add a class library or a JAR file to a
project
 If you add a library or a JAR file to a project, the JRE will be able
to find and run any of the classes within the JAR files that are
added to the project. When the application is deployed, these JAR
files will be stored in the application’s WEB-INF\lib folder.
 To add a library file, right-click on the Libraries folder, select the
Add Library command, and use the resulting dialog box to select
the library. This may add one or more JAR files to the project.
 To add a JAR file, right-click on the Libraries folder, select the
Add JAR/Folder command and use the resulting dialog box to
select the JAR file. If that doesn’t work, copy the JAR file into the
JDK’s jre\lib\ext folder.
 To remove a JAR file from the libraries for a project, right-click
on the JAR file and select the Remove command.
Murach’s Java Servlets/JSP ( 2nd Ed.), C3
© 2008, Mike Murach & Associates, Inc.
Slide 45