You are on page 1of 44

Chapter 3

How to work with NetBeans and


Tomcat
Mai Anh Thơ
thoma@hcmute.edu.vn

Chapter 3 - How to work with NetBeans and Tomcat 1


Objectives
Applied
1. Use NetBeans to develop and test web application that consist of HTML
files, JSP files, servlets, regular Java classes, and XML files.
2. Use NetBeans to build, deploy and run a web application on a local server.
3. Use NetBeans to add a class library or JAR file to a project.
4. Use NetBeans to register a database connection.
5. Use NetBeans to start or stop a local web sever.
Knowledge
6. In general terms, describe a NetBeans project.
7. In general terms, describe the difference between the Projects window, the
Files window, and the Services window for a NetBeans project.
Chapter 3 - How to work with NetBeans and Tomcat 2
The Start page

Chapter 3 - How to work with NetBeans and Tomcat 3


How to create a new project
• Select File  New Project
• The first dialog box for new web application

Chapter 3 - How to work with NetBeans and Tomcat 4


The second dialog box for a new web application

Chapter 3 - How to work with NetBeans and Tomcat 5


The third dialog box for a new web application

Chapter 3 - How to work with NetBeans and Tomcat 6


The default web application

Chapter 3 - How to work with NetBeans and Tomcat 7


How to work files
• To open a file in the appropriate editor, double-click on it.
• To rename a file, right-click on it and select Rename.
• To delete a file, right-click on it and select Delete.

Chapter 3 - How to work with NetBeans and Tomcat 8


How to open an existing project
• Select File  Open Project
• The Open Project dialog box

Chapter 3 - How to work with NetBeans and Tomcat 9


How to work with an existing project
• To remove a project, right-click on the project, and select Close.
• To run the current project, press F6 or select Run Project from the
toolbar or the Run menu.
• To run a specific file, right-click on the file and select Run File. This
deploys the web application and displays the file in the default web
browser.
• To change the default browser, select Tools  Options, select the
General category, and select the browser that you want to use.
• 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 Clear.

Chapter 3 - How to work with NetBeans and Tomcat 10


The web browser when you run a project

Chapter 3 - How to work with NetBeans and Tomcat 11


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
• Displays the first page of the application in that browser.

Chapter 3 - How to work with NetBeans and Tomcat 12


How to add HTML or JSP files to a project
• To add an HTML file, right-click on the Web Pages folder or one of its
subfolders and select New  HTML. 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 New  JSP. Specify the name and location of
the file.

Chapter 3 - How to work with NetBeans and Tomcat 13


The New File diaglog box

Chapter 3 - How to work with NetBeans and Tomcat 14


The HTML and JSP editor

Chapter 3 - How to work with NetBeans and Tomcat 15


How to add Java classes and servlets to a project
• To add a Java class, right-click on one of the project folders and select
New  Java Class. Specify the name and location of the file.
• To specify a package for the class, use the Package combobox to select
an existing package or to enter the name for a new package.
• To add a servlet, right-click on the package, and select New 
Servlet. Specify a name and URL for the servlet.

Chapter 3 - How to work with NetBeans and Tomcat 16


The New Java Class dialog box

Chapter 3 - How to work with NetBeans and Tomcat 17


The first New Servlet dialog box

Chapter 3 - How to work with NetBeans and Tomcat 18


The second dialog box

Chapter 3 - How to work with NetBeans and Tomcat 19


The Java editor with an error displayed

Chapter 3 - How to work with NetBeans and Tomcat 20


How to work with code completion
• After you enter a class or object name and a period, the code
completion feature provides a list of possible entries. Then, select an
item and press the Tab or Enter key to insert the code into the file.
• To active the code completion feature at other points in your entries,
press Ctrl + Space.

Chapter 3 - How to work with NetBeans and Tomcat 21


The XML editor the Pages tab displayed

The XML editor with the Source tab displayed

Chapter 3 - How to work with NetBeans and Tomcat 22


How to work with XML files
• To edit an XML file, double-click on it to open it. Then, use the tabs
across the top of the page to modify the web.xml. Use the Source tab
to manually edit the file in the XML editor.
• To validate an XML file against its schema, right-click on the file and
select Validate XL. The results of the validation will be displayed in an
XML Check window within the Output window.

Chapter 3 - How to work with NetBeans and Tomcat 23


The Files window for the ch04email project

Chapter 3 - How to work with NetBeans and Tomcat 24


The folders that NetBeans uses
Folder Description
build\web All folders and files for the web application after it
has been built.
dist The WAR file for the application.
nbproject The configuration files and build scripts.
src The source code for the Java files and servlets.
web The HTML, JSP, and XML files for the application.

Chapter 3 - How to work with NetBeans and Tomcat 25


How to add existing files to a project
• Copy the files from the Windows Explorer or mac Finder 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 application on the remote server.

Chapter 3 - How to work with NetBeans and Tomcat 26


Services window with Servers group displayed

Chapter 3 - How to work with NetBeans and Tomcat 27


How to work with servers
• To view a server, expand the Servers group. Then, right-click on a
server to start, stop, restart, or refresh it.
• To view the web applications that are running on the Tomcat server,
expand the Web Applications folder for the server. Then, right-click on
an application to start, stop, or undeploy it.

Chapter 3 - How to work with NetBeans and Tomcat 28


The Add Library dialog box

Chapter 3 - How to work with NetBeans and Tomcat 29


How to work with libraries and JAR files
• To add a library file to the libraries for a project, 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 to a project, right-click on the Libraries folder, select
the Add JAR / Folder command and use the resulting dialog box to
select the JAR file.

Chapter 3 - How to work with NetBeans and Tomcat 30


How to resolve a database connection

Chapter 3 - How to work with NetBeans and Tomcat 31


How to register the database connection

Chapter 3 - How to work with NetBeans and Tomcat 32


How to register a database connection
1. Right-click on the project in the Project window and select the
Resolve Data Source Problem command.
2. Select the data source and click the Add Connection button.
3. If necessary, change any of the entries for the connection. Then,
click on the OK button to register the database connection.

Chapter 3 - How to work with NetBeans and Tomcat 33


Terms
• The web.xml file is known as the deployment descriptor (DD).
• After you enter the starting bracket or an HTML or JSP tag, the code
completion feature provides a list of possible entries.

Chapter 3 - How to work with NetBeans and Tomcat 34


Summary
• The NetBeans IDE and the Tomcat web server are open-source, available for
free, and run on all modern operating systems.
• A project is a folder that contains all of the folders and files that make up an
application. You can use the Projects window to work with the folders and files
of a project.
• You can run the project that’s currently selected by pressing F6 or by clicking
the Run Project button in the toolbar.
• You can use NetBeans to add and edit HTML, JSP, Java, servlet, and XML
files. When you edit a file, you can use the code completion feature to help you
enter code.
• Since the web.xml file describes how the application will be configured when it
is deployed, this file is known as the deployment descriptor (DD). The web.xml
file is stored in the WEB-INF folder.
Chapter 3 - How to work with NetBeans and Tomcat 35
Summary (continued)
• You can add existing folders and files to a project by copying them to the
appropriate folders.
• You can use the Files window to view the physical locations of the folders and
files for a project.
• You can deploy a web application to a remote server by copying its WAR (web
archive) file from the dist folder for a project to the appropriate folder on the
remote server.
• You can use the Services window to start or stop a server such as the Tomcat
web server. In addition, you can use this window to undeploy a web application
from a Tomcat server.
• You can add a library to a project by right-clicking on the Libraries folder,
selecting the Add Library command, and using the resulting dialog box to
select the library.
Chapter 3 - How to work with NetBeans and Tomcat 36
Practice 3.1 – Install and use the NetBeans IDE
Test NetBeans and Tomcat
1. Create a new project for a web application named testApp, and save
it in the ex_starts directory.
2. Make sure this project uses the Tomcat server.
3. Use the Projects window to review the folders and files for the
testApp project.
4. Run the testApp web application, and review the information that’s
in the NetBeans Output window.

Chapter 3 - How to work with NetBeans and Tomcat 37


Practice 3.1 – Install and use the NetBeans IDE
Add a JSP file to the project
1. Add a JSP file named test.jsp to the project.
2. Edit the code for the test.jsp file so it includes this tag within its
body tags: <h2>Test JSP</h2>
• To do that, type an opening bracket (<), select the h2 tag from the resulting
list, and press the Enter key. Next, press the Enter key again to complete the
tag, and type “Test JSP” within the tag.
3. Right-click on the test.jsp file in the Projects window, and select the
Run File command to display this JSP in a browser.

Chapter 3 - How to work with NetBeans and Tomcat 38


Practice 3.1 – Install and use the NetBeans IDE
5. Use the Window  IDE Tools  Palette command to open the
Pallette window and click on the plus signs for HTML, HTML
Forms, and JSP to expand these items. Then, drag the Text Input
item to the test.jsp file right after the h2 tag that you just entered. In
the dialog box that opens, type testname for the Name and click on
the OK button. Note the HTML tags that are added to the JSP.
6. Right-click on the test.jsp file, and select the Run File command to
display this JSP in a browser.
7. Press the F6 button to run the application. Note that the index.html
files runs instea o the test.jsp ile which is usually what you want.

Chapter 3 - How to work with NetBeans and Tomcat 39


Practice 3.1 – Install and use the NetBeans IDE
Add a servlet file to the project
1. Add a Java servlet named TestServlet to a package named murach.test and map it
to the /test URL.
2. In the editor window, scroll to the bottom of the TestServlet file and click on the
+ sign before the HttpServlet methods. That should expand the starting code for
these methods. Then, note that the doGet and doPost methods call the
processRequest method that’s earlier in the servlet. You will earn how to code the
doGet and doPost methods of a servlet later.
3. In the Projects window, right-click on the TestServlet file and select the Run File
command. In the resulting dialog box, click OK. At this point, NetBeans should
display a browser window with some text in it that indicates that this servlet has
executed. NetBeans uses the URL that you specified in step 1 to call the servlet
4. Close the testApp project.

Chapter 3 - How to work with NetBeans and Tomcat 40


Practice 3.2 – Creating a basic JSP Component
• Overview: In this practice, you create, deploy, and run a web
application project with a basic JSP
• Tasks
1. Add GlassFish Server to the NetBeans IDE
a. In NetBeans, from the menu select Window > Services
b. Right-click Servers and click Add Server…
c. Select GlassFish Server
d. Select GlassFish Server 5.1.0, then Download it.
e. Make sure that Local Domain is selected, and the domain name is domain1
f. Click Finish.

Chapter 3 - How to work with NetBeans and Tomcat 41


Practice 3.2 – Creating a basic JSP Component
• Tasks (continued)
2. Develop a basic JSP page.
a. Ensure that NetBeans is running.
b. In NetBeans, from the menu, select File > New Project.
c. Under Categories, click Java Web.
d. Under Projects, click Web Application.
e. Click the Next button.
f. The new Web Appication project should have the following characteristics:
• Project Name: SampleWebApplication
• ProjectLocation: D:\Labs\netbeans\projects (or up to you)
• Use Dedicated Folder for Storing Libraries: (deselected)
g. Click Next button.
h. The ”Server and Settings” dialog box should have the following information:
• Server: GlassFish Server
• Jave EE Version: Jave EE 7 Web
• Context Path: /SampleWebApplication
i. Click the Finish button.

Chapter 3 - How to work with NetBeans and Tomcat 42


Practice 3.2 – Creating a basic JSP Component
• Tasks (continued)
2. Develop a basic JSP page (continued).
j. An index.jsp file is created for you automatically. You can place any static HTML in a
JSP page. Experiment with adding Java code to the JSP page.
k. The following is an example of what you might enter: <%= new java.util.Date() %>
3. Deploy and test the sample application
a. Save any modified files. If Deploy on Save is not enabled, deploy the
SampleWebApplication Web project by right-clicking the project folder in NetBeans
and selecting Deploy.
b. Test the application by right-clicking the project folder and selecting Run, or by poiting
a web browser at: http://localhost:8080/ SampleWebApplication/index.jsp

Chapter 3 - How to work with NetBeans and Tomcat 43


Reference
• Murach’s Java Serverlets and JSP (3rd edition)

Chapter 3 - How to work with NetBeans and Tomcat 44

You might also like