Building Web applications with JDK 1.4.2

Now that the latest JDK 1.4.2 includes an optional bundled IDE, NetBeans 3.5, it's a snap to build a Web application. This article will show you how.

JDK 1.4.2, introduced in July, offers a package bundled with NetBeans IDE 3.5, a popular open source project backed by Sun Microsystems. NetBeans provides a sophisticated GUI environment to develop Java applications, including tools for project management, version control, and debugging.

Another important feature, different from typical code editors and the focus of this article, is the integration of a servlet container. NetBeans IDE 3.5 contains an internal copy of Tomcat, an open source servlet container. You can also optionally integrate with another external Tomcat, installed separately. This support for a servlet container linked with NetBeans' debugging tools makes for a robust environment for Web application development.

Getting started
We'll assume you've installed the JDK 1.4 bundled with NetBeans. Now, when you first start the NetBeans IDE, you should install one more module called treefs. This add-on gives NetBeans views over a filesystem, which is very important because those views will be classpaths when compiling your code. There is no need to explicitly set your own CLASSPATH in your operating system.

To begin the installation of treefs, select Tools | Update Center from the menu bar. You will see a list of Update Centers. Check all the selections and click Next. NetBeans will go out to the Internet and retrieve a list of all items available for download. Find the item called "View over a filesystem" and click the greater than arrow to move the module into your list of modules to install. Then, click the Next button to complete the installation.

After restarting the IDE, you will find a window titled Explorer on the left-hand side. There are three tabs: Filesystems, Project Default, and Runtime. Click Runtime, which should bring up a list of services in the Explorer window. Expand the Server Registry node to reveal two child nodes: Default Servers and Installed Servers. Now, expand the Installed Servers node to reveal Internal Tomcat (4.0.6) [Not Running]. Right-click this Tomcat node, and select Start Server from the pop-up menu (Figure A).

Figure A
Starting the internal Tomcat servlet container

You will get a pop-up window as NetBeans starts the Tomcat service, and a message like this should appear in the pop-up:
D:\Program Files\j2sdk\j2sdk1.4.2\jre\bin\java -classpath "D:\Program Files\j2sdk\netbeans3.5\tomcat406\bin\bootstrap.jar";"D:\Program Files\j2sdk\j2sdk1.4.2\lib\tools.jar" -Dcatalina.home="D:\Program Files\j2sdk\netbeans3.5\tomcat406" -Dcatalina.base="C:\Documents and Settings\cs_cwt\.netbeans\3.5\tomcat406_base" org.apache.catalina.startup.Bootstrap "start"
Starting service Tomcat-Netbeans
Apache Tomcat/4.0.6

This tells you that the internal Tomcat has started successfully and you're ready to start your Web application development.

Setting up the project
Now, you have to reserve a location on your local disk to store the actual source code for your Web application. Download our helloweb example and decompress to your local disk to see one example of how to organize your code.

Inside the helloweb folder is a subfolder called src. This folder is supposed to store our Java source codes. Within src, the subfolder called main stores backend code while the subfolder, Web, stores code for the Presentation tier. This structure is not a must but is recommended as a good practice.

Let's create a new project for our Web app. Click Project on the menu bar to start the Project Manager. Click the New button, and fill in the name for your project.

The next step is to mount files for this project or construct a workplace for this project. Right-click Filesystem, and select the Local Directory option under the Mount node. You'll get a pop-up window to select a working folder. After you select a folder for storing your source code (for example, the helloweb folder you decompressed earlier), a new node appears under the root Filesystems.

Now, let's start a class called Hello under /helloweb/src/main/. Click to select the /helloweb/src/main folder and then click New on the menu bar. You will find a list of templates in a pop-up window. For the Hello class, select Java Class under Java Classes, and follow the instructions from the wizard.

This wizard includes templates for JSP as well as Java files. Use it now to create a JSP file for our Web app called HelloUI.jsp in the /helloweb/src/web folder.

Putting it together
Now, we have two pieces of code and we will build our simple application. But before compiling them, we should prepare a way so that our JSP code can import the Java class. We will see how to use View on a filesystem, the add-on we installed earlier.

Since the JSP file will import the Java class, we should include the path of the JSP file and that of the Java class file in CLASSPATH. Expand the node, main, which contains, then right-click the node and select Tools | Mount as a new filesystem. Do the same to the node, Web, which contains HelloUI.jsp. Now, we have included them in our "CLASSPATH".

Figure B
Using the treefs add-on to add classpath information

After creating these views on a filesystem, right-click Hello under the node, src/main/, and select Build from the pop-up menu. This action will compile into Hello.class. Then, right-click HelloUI under the node src/web/, and select Compile to turn the JSP file into its corresponding compiled servlet class file.

After the compilation of the JSP is finished, the IDE will prompt you on whether you want to create a Web Module Group for deployment. For now, click Cancel and develop your own Web Module Group as follows.

In order to create a Web Module Group based on the JSP file's folder, you should right-click the node, src/web/, and select All Templates under an option New. There will be a list of templates in the New Template wizard. Select Web Module Group under the node called JSPs & Servlets, click Next, and fill in the name for this Web Module Group. The name is just a reference for the IDE and does not effect our deployment. Let's just call ours helloweb.

Now, you will see a new node called helloweb under the src/web node. Right-click this new node, and select Add Web Module. This action lets you add Web modules into the group. In this case, type /helloweb for our mapping to the Web module src/web. This lets the Web server know that /helloweb points to the defined path.

Figure C
Adding the helloweb module to our Web Module Group

You've now prepared a structure to run the JSP on the integrated Tomcat Web Server. Right-click the Web Module Group, and select Execute (Force Reload) to run your first Web Application. It's as simple as that. This is your first webapp with NetBeans and JDK 1.4.

Editor's Picks