Introduction
JBuilder 2 from Inprise has many improvements over the original JBuilder product. One of the nice additions to JBuilder 2 is the ability to quickly build a template for Java servlets. After you fill in a few simple forms, JBuilder generates the source code template for the Java servlet you've defined.
In this article we'll show you how to quickly and easily create a Java servlet using JBuilder. We'll also look at two related issues:
I think you'll find that creating a template for a Java servlet in JBuilder 2 is pretty easy, once you've seen it done once or twice. To start building our "Hello, world" servlet, open JBuilder and then select File | New .... When the New... frame opens, double-click on the Servlet icon to start the servlet-building process.
| Figure 1: | Start by selecting the Servlet icon on the File | New ... form. |
| Figure 2: | The default values are shown here for JBuilder's Project Wizard. |
When the Servlet Wizard: Step 1 of 2 screen appears, delete the value in the Package: field. Because this is just a demonstration, there's no need to get involved in package names. Next, change the Class: field to something more descriptive, like JBHelloWorldServlet.
Leave the Servlet Style options as they are, but change the Servlet Methods checkboxes so that the doGet() field is selected, and no other fields are selected. When you've made these changes, your form should look like Figure 3 below.
| Figure 3: | A view of the completed Servlet Wizard: Step 1 of 2 form. |
After you've made these changes, click the Next button to move to the next form.
Next, the Servlet Wizard: Step 2 of 2 form appears. This is shown in Figure 4. For the case of our simple "Hello" example, you don't need to make any changes to this form, bt you should get used to seeing it for more complex servlets. You'll use this form to define servlet parameters that are being passed from your HTML pages to your Java servlets. Because we don't have any parameters for our example, just click Finish, and you'll be taken back to JBuilder's IDE and AppBrowser.
| Figure 4: | The Servlet Wizard: Step 2 of 2 form doesn't require any changes for our simple example. |
Modifying the source code
Now you've made it through the two wizards required to set up your
servlet project. There's just a couple of things left to do. First,
we need to modify the Java source code a little bit to echo out the traditional
words "Hello, world". To do this we want to edit the JBHelloWorldServlet
source code.
To get to this source code, click on the JBHelloWorldServlet.java icon in the Navigation Pane of the AppBrowser (the upper-left pane of the browser). When you click on that icon, the source code is displayed automatically in the Source window of the Content Pane (i.e., the source code editor). This is shown in Figure 5.
| Figure 5: | The JBHelloWorldServlet.java icon is highlighted in the Navigation Pane, and the Java source code is shown in the Source window. |
Changing the source code is easy. In the editor, just add the "Hello, world" text to the out.println statement that contains the opening <body> tag. To do this, just change this line in the code:
Almost ready to compile ...
In a traditional Java/JBuilder application, you'd be ready to compile
your code right now. But, when creating Java servlets with JBuilder, there's
one more requirement -- you first need to add the Java Servlet Development
Kit (JSDK) JAR file to your JBuilder environment. If you don't, your
attempts to compile the JBHelloWorldServlet won't get too far.
Copyright © 1998-2005 DevDaily Interactive, Inc.
All Rights Reserved.