This document takes you through the basics of using
NetBeans IDE 4.1 to develop and run web applications on Sun Java System
Web
Server 6.1.
Sun Java System Web Server 6.1 supports J2EE 1.3
based Web Applications.
This document is designed to get you going as quickly as
possible. For more information on working with NetBeans IDE, see the Support and Docs page on the
NetBeans website.
We will create,
deploy, and execute a simple web application on Sun Java System Web
Server 6.1. The application uses a
JavaServer Pages™ page to ask you to input your name. It then uses a
JavaBeans™ component to persist the name during the HTTP session and
repeats the name on another JavaServer Pages page.
Setting Up a Web Application Project
Before you start writing code, you have to make sure you
have all of the necessary software and that your project is set up
correctly.
Installing the Software
Before you begin, you need to download the following
software onto your computer:
Java Standard Development Kit (JDK™) version 1.4.2 (download) or
5.0 (download)
Next, you need to install the software on your computer.
Install the Java Standard Development Kit
Install Netbeans 4.1
Unzip the plugin file you downloaded using any zip program. (The instructions from this point
onward will reference the English version of the zip file - j2ee_sun_ws61_nbm-1_1_1.zip).
When you unzip the plugin file, a directory named "WebServerPluginFiles"
will be created at the same directory where the zip file was downloaded to.
Inside the new directory, you will find the plugin file "org-netbeans-modules-j2ee-sun-ws61.nbm.
Launch the Netbeans IDE. In the IDE main menu, select the "Tools" menu and select the "Update
Center" menu option. When the Update Center Wizard opens, select the "Install Manually Downloaded
Modules(.nbm Files)" radio button and then click the "Next" button to continue.
When the next screen appears, click the "Add..." button and navigate to the
"WebServerPluginFiles" directory that was created earlier. Select the
"org-netbeans-modules-j2ee-sun-ws61.nbm" file from the WebServerPluginFiles directory.
When the plugin appears in the "Modules to Install" list, click the "Next" button to continue.
When the "Select Modules to Install screen appears, select Sun Java System Web Server 6.1
in the "Include in Install" list on the right side of the screen, and then click the "Next" button
to continue.
You will then be asked to accept the license agreement for the plugin. Click the "Accept" button
to accept the license, and the Update Center will download the plugin into the IDE. When the download
is complete, click the "Next" button to continue.
In the "View Certificates and Install Modules" screen, select the "Include" checkBox belonging
to the Sun Java System Web Server 6.1 - version 1.1.1.1.1 module name that is listed. A "Module
Certificate" dialog box will open. Click the "Accept" button in the dialog box to accept the
certificate which is signed by Sun. After the dialog closes, click the "Finish" button on the
"View Certificates and Install Modules" screen to complete the installation. You are now ready to
proceed to the next step to register the Sun Java System Web Server 6.1 in the IDE.
Registering the Server
Before you can register Sun Java System Web Server 6.1, you need
to download the Web Server and install it. You can install the Web Server on the computer
where the Netbeans IDE was installed or you can install it on a remote computer.
Download the Web Server software onto the computer you want to run the Web Server on.
After you have completed the download, run the Web Server Installer to install the Web Server. When
the installation has been completed, you can then register an instance of the Web Server with the
IDE. You can choose to register a local instance on the same machine, or you can
choose the Web Server 6.1 running on a remote machine.
Choose Tools > Server Manager from the main window.
Click Add Server. Select the Sun Java System Web
Server 6.1 and give a
name to the instance. Then click Next.
Give the location of the
local instance of the web server or choose the remote web server.
Specify the server information i.e host, port, username and password of
the admin server.
The IDE displays a server node for your Web Server in
the Runtime window under the Servers node. For more information
on controlling the Sun Java System Web Server 6.1 life cycle from the
IDE, the JDBC resource listing and various things you can do with the
Web Server, please refer to the Netbeans IDE online help documentation.
Select the Sun Java System Web Server 6.1 to
which you want to deploy your
application. Only servers that are registered with the IDE are listed.
Leave the Set as Main Project checkbox selected.
Click Finish.
The IDE creates the $PROJECTHOME/HelloWeb
project folder. The project folder contains all of your sources and
project metadata, such as the project's Ant build script. The HelloWeb
project
opens in the IDE. You can view its logical structure in the Projects
window and its
file structure in the Files window.
Creating and Editing Web
Application Source Files
Creating and editing source files is the most important
function that the
IDE serves. After all, that's probably what you spend most of your day
doing.
The IDE provides a wide range of tools that can compliment any
developer's
personal style, whether you prefer to code everything by hand or want
the
IDE to generate large chunks of code for you.
Creating a Java Package and JavaBeans
Component
Expand the HelloWeb project node and the Source
Packages
node. Note the Source Packages node only contains an empty default
package node.
Right-click the Source Packages node and choose New
>
File/Folder. Under Categories, select JavaBeans Objects. Under
File Types, select JavaBeans Component and click Next.
Enter NameHandler in the Class Name text box and enter org.me.hello
in the Package combo box. Click
Finish.
Editing the JavaBeans Component
Expand the NameHandler.java node and double-click
the NameHandler class node. In the Source Editor, delete the following
part of the class declaration:
extends Object implements Serializable
Expand the NameHandler class node and the Fields
node. Three default fields are provided. Right-click the
PROP_SAMPLE_PROPERTY field and choose Delete from the contextual menu.
Do
the same for the other two fields. Notice that the lines of code that
use these deleted fields are underlined in red in the Source Editor.
Expand the Methods node and delete all the default
methods.
In the Source Editor, type the following code in line
16, directly
below the class declaration:
String name;
Expand the Constructors node and double-click the
NameHandler constructor. In the Source Editor, edit the NameHandler()
constructor by replacing its default code (propertySupport = new
PropertyChangeSupport(this);)
in line 18 with the following:
name = null;
Press Alt-Shift-F in the Source Editor to update the
import statements so
that your code specifies only those that are needed.
Renaming a Field
Right-click the word name in the field
declaration on line 15 and choose Refactor > Rename.
In the New Name field, type username.
Then click Next.
The Refactoring window previews all of the references that
will be changed to point to the newly named field. Double-click any
reference to jump to its location in the Source Editor. The check boxes
indicate which of the references will be refactored.
Click Do Refactoring. All checked references to the
field are
renamed.
Generating Getter and Setter Methods
Right-click the word username in the field
declaration on line 15 and choose Refactor > Encapsulate Fields.
Click Next to run the command with its default options.
Click Do Refactoring. Getter and setter methods are
generated for the username field and its access level
is changed to private. The JavaBeans component should now look like
this:
package org.me.hello; public class NameHandler { private String username; public NameHandler() { setUsername(null); } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
Editing the Default JavaServer Pages
File
Expand the HelloWeb project node and the Web Pages
node.
Note the IDE has created a default JavaServer Pages page, index.jsp,
for you.
Double-click index.jsp. It opens in the
Source
Editor.
Paste or type the following code into the body of index.jsp,
to replace the default <body> tags and their contents:
<body> <form method="post" action="response.jsp"> Enter your name: <input type="text" name="username"> <br> <input type="submit" value="Ok"> </form> </body>
Creating a JavaServer Pages File
Expand the HelloWeb project node and the Web Pages
node.
Right-click the Web Pages node and choose New >
JSP,
name the JavaServer Pages file response, and click Finish. response.jsp
opens in the Source Editor.
Below the <body> tag, type <jsp:u
and wait. When the code completion box appears,
see the popup Javadoc for the <jsp:useBean> syntax. If
the box does
not appear, press Ctrl-Space. Press Enter.
Press Space and the project offers all the variables
that
are applicable to <jsp:useBean>. Select id
and type "mybean" between the quotes.
Press Space after the final quote and select class.
Press Ctrl-Space between the quotes to open the code completion box.
The project offers code completion for all packages and classes in the
project's compilation classpath. Select org and press Enter.
Enter a period after org and press
Ctrl-Space.
The code completion box opens again. Select me, and continue
using code completion such that the line reads as follows:
Editing Sun Java System Web Server 6.1
specific deployment configuration file
Sun Java System Web Server
6.1 supports J2EE 1.3 standards for web applications.
In addition to the web.xml file, which is a J2EE 1.3 standard, another
file, sun-web.xml, is needed for each web application that is deployed
on Sun Java System Web Server 6.1.
When you create a web application from the IDE where the targeted
server is Sun Java
System Web Server 6.1, a ws61-sun-web.xml file is created. You
can find this file in the Projects window, by expanding the Web Pages
node and then the WEB-INF node.
To edit this file, click the file's node, right-click and choose Edit
from the contextual menu.
When you deploy the web application on the server, the IDE renames the
file on the server to sun-web.xml. The name in the IDE is different to
avoid conflicts between the sun-web.xml file that is specific to Sun
Java System Application Server 8.1.
Building and Running a Web
Application Project
The IDE uses an Ant build script to build and run your
web applications.
The IDE generates the build script based on the options
you enter in the New Project wizard and the project's Project
Properties dialog box.
Building a Project
Choose Build > Build Main Project (F11). The
HelloWeb project is built.
Running the Main Project
Choose Run > Run Main Project (F6)
from
the Run menu. Double-click the Output window's titlebar to maximize it
so you can
see all the output. Finally, it deploys the web
application on the Sun Java System Web Server 6.1 you specified when
creating the project you specified when creating the project. If
you explode the Sun Java System Web Server 6.1 node to the virtual
server level, you will see the HelloWeb under the Deployed Componets
node. Double-click the Output window's titlebar
again to minimize it.
Enter your name in the text box on your deployed index.jsp
page and click
OK. The response.jsp page should open and greet you.
Select the Files window and expand the HelloWeb
project
node. The build class files are in the build folder. The
build WAR file
is in the dist folder.
Press F6 to run the program again. Nothing new needs
to be compiled and the program is run.
Generating Javadoc
Right-click the project node and choose Generate
Javadoc for Project. Javadoc output appears
in the Output window, and your web browser opens displaying the
Javadoc.
Customizing the Build Process
You can customize the build process by doing any of the
following:
Enter basic options, like classpath settings and JAR
filters, in the New Project wizard when you create a project, or
afterwards in the Project Properties dialog box.
Customize existing Ant targets.
Edit properties in project.properties to
change the name and location
of build output folders and files.
The subsections below guide you through some of the
IDE's customizing options.
Overriding an Ant Property
In the Files window, expand the HelloWeb project
node and the nbproject folder.
Double-click project.properties to view all
of the Ant properties generated by the IDE for the project.
Copy the line containing build.dir=build.
This property sets the output directory for compiled classes.
In the Files window, expand the private
folder and double-click private.properties.
Paste the build.dir=build property into the
file and change the property to read build.dir=build/production
Choose Build > Clean and Build Main Project
(Shift-F11). The compiled classes are built to the build/production
folder.
Setting VM Arguments
Open the private.properties file if it is
not open already.
Enter a new line anywhere in the file, type run.jvmargs=-J-Xms24m
-J-Xmx160m, and choose File > Save. The project will be run
with the specified heap size and maximum memory.
Adding to an Ant Target
In the Files window, go to the nbproject
folder for the HelloWeb project.
Double-click build-impl.xml to open it in
the Source Editor. This file contains all of the Ant targets generated
by the IDE. Each target has a -pre target and a -post
target that you can use to add processing instructions that the IDE
runs before or after running the target. Do not change the targets
in this file — this file is generated automatically by the IDE and any
changes you make will be lost.
In the Files window, go to the HelloWeb project
folder and double-click build.xml. This is where you override
targets from build-imp.xml.
Next Steps
For more information about using NetBeans IDE 4.1, see
the following resources:
To send comments and suggestions, get support, and keep
informed on the latest developments on the NetBeans IDE J2EE
development features, join the
mailing list
.