search the site  

Enter search terms:


subscribe to newsletters   WebReference.com logo   WebReference.com
dev the Web
tip archive  •   about  •   contact  •   jobs  •   sitemap

Biz Resources
Data Integration Software
Web Hosting
Email Solutions


[next]

How to Create an Ajax Autocomplete Text Field: Part 3

By Rob Gravelle

Digg This Add to del.icio.us

ASP 3.0/.NET Developer
Jupitermedia
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume

In the previous installment, Part 2 - The Fund Class, we built a container class for the fund's properties. On the server, a HashMap object will hold a collection of funds to be searched. Matching ones will be converted to a JSON array and returned to the browser to display in our Autocomplete list. This week we'll be developing the AutocompleteServlet. The servlet acts much like an ASP, Perl or PHP script in that it's executed by the server and accepts parameters from the browser. There's one important difference between Java and the languages that I just mentioned: unlike these, Java is a complete language, not a script. That provides several advantages over the former. For instance, the byte code a compiled class uses runs at a fairly decent speed. More importantly, Java can be developed in feature-rich IDEs like Eclipse which allow thorough debugging and testing. It also provides all the perks that come with OOP languages such as encapsulation, inheritance, abstraction, polymorphism, etc. For these reasons, it's hard to beat the scalability of Java.

Importing the JSON Java Utility Files

In the last article, we downloaded the JSON Java Utility source files, but we haven't done anything with them yet. Now would be a good time to bring them into our project, because our Servlet class won't compile without them. Here's how to do it from within Eclipse.

If you open any of the JSON Java files in a text editor or Eclipse, you'll see by the top line that they should reside in a package called "org.json."

Therefore, we'll create two subfolders under our main source folder for the JSON files. There are several ways to do it, but the simplest method is to use the "Import" command under "File: Import..." on the main menu (See Figure 1).

In the "Import" dialog, select "File System" (See Figure 2).

The next thing is to provide the "From Directory." Use the "Browse..." button to locate the folder which contains the seven JSON files. Once you've closed the "Import from Directory" dialog, the folder you selected will appear in the left window pane, while the seven files will appear in the right one. The checkboxes beside each item are used to flag it for import. Unless you have other files in the same folder, simply selecting the source folder on the left will automatically select all the files contained therein. Now we have to specify the "Into Folder." Click the second "Browse" button and select the "src" folder, since it's the root for our source files. Our one remaining task is to create the two subfolders under our project root. To do that, we can type the extra path info in the "Into Folder" textbox. Hence, we would add "/org/json" to the path of "Autocomplete/src" (See Figure 3).

Click the "Finish" button to see the new packages and files under the "Java Resources: src" folder.

Create the Autocomplete Servlet

Looking at the Fund class, it should be apparent that it doesn't do much. The actual processing takes place in the AutocompleteServlet class. A servlet is a special type of Java class that can handle server requests and provide an appropriate response. We'll be overriding a method that will take a course of action based on the "action" parameter.

We'll create the new class in very much the same way as we did for the Fund:

  1. Right-click on the "Java Resources" branch in the "Project Explorer" pane.
  2. Hover the mouse pointer over the "New" item to bring up the second popup.
  3. Click on the "Class" item to bring up the "New Java Class" dialog (See Figure 4).

The AutocompleteServlet class must extend HttpServlet. To set its parent class, we can use the "Browse..." button to bring up the "Superclass Selection" dialog. Type "httpservlet" in the "Choose a Type" textbox. In fact, you probably won't have to type more than a couple of characters before the "javax.servlet.http.HttpServlet" package appears at the top of the list (See Figure 5)

Select it and click on OK to close the dialog and create the new class.

Here's the resulting empty AutocompleteServlet class:


[next]

Recent Articles

WebReference.com site name
How to Create an Ajax Autocomplete Text Field: Part 6
Software Engineering for Ajax
Perl Pragma Primer
internet.com site name
Using File Virtualization for Disaster Recovery
VoIP Security: SIP—Versatile but Vulnerable
Around the World in 80 Nodes




JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Microsoft Article: 7.0, Microsoft's Lucky Version?
Microsoft Article: Hyper-V--The Killer Feature in Windows Server 2008
Avaya Article: How to Feed Data into the Avaya Event Processor
Microsoft Article: Install What You Need with Windows Server 2008
HP eBook: Putting the Green into IT
Whitepaper: HP Integrated Citrix XenServer for HP ProLiant Servers
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 1
Intel Go Parallel Portal: Interview with C++ Guru Herb Sutter, Part 2--The Future of Concurrency
Avaya Article: Setting Up a SIP A/S Development Environment
IBM Article: How Cool Is Your Data Center?
Microsoft Article: Managing Virtual Machines with Microsoft System Center
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Video: Are Multi-core Processors Here to Stay?
On-Demand Webcast: Five Virtualization Trends to Watch
HP Video: Page Cost Calculator
Intel Video: APIs for Parallel Programming
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Sun Download: Solaris 8 Migration Assistant
Sybase Download: SQL Anywhere Developer Edition
Red Gate Download: SQL Backup Pro and free DBA Best Practices eBook
Red Gate Download: SQL Compare Pro 6
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
How-to-Article: Preparing for Hyper-Threading Technology and Dual Core Technology
eTouch PDF: Conquering the Tyranny of E-Mail and Word Processors
IBM Article: Collaborating in the High-Performance Workplace
HP Demo: StorageWorks EVA4400
Intel Featured Algorhythm: Intel Threading Building Blocks--The Pipeline Class
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES