search the site  

Enter search terms:


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


[next]

How to Create an Ajax Autocomplete Text Field - Part 6

By Rob Gravelle

Digg This Add to del.icio.us

Sr. Programmer/Analyst
Professional Technical Resources
US-OR-Portland

Justtechjobs.com Post A Job | Post A Resume

In part 5 of this series, we wrote the client-side script to manage the behavior of the Autocomplete control in the browser. The payoff is in sight, because this week we're going to finish the remaining Web files, including the all-important AutocompleteSearch.jsp page.

Create the AutocompleteSearch.jsp File

Use the "Shift-Alt-N" shortcut to bring up the "New" popup menu and select the "JSP" item to open the "New JavaServer Page" dialog box (see (See Figure 1).

We'll call this file AutocompleteSearch. Click the "Finish" button to create it.

Link External Files

We'll now link three external files to the page. The first is the AutocompleteList.js file that we created in Part 5 of the series. The second file is the JSON parser that we downloaded and imported in Part 5 to assist with the processing of the AJAX response. The third is the AutocompleteList.css file that we created in Part 4.

Add the following three lines to the <head> section to link the files to the AutocompleteSearch.jsp page. Be sure to substitute the name of your JSON parser if it's not the same one as mine (such as json2.js):

Next, we'll modify the <body>; tag to include our two JavaScript calls:

The Autocomplete Control Elements

The following code will make up the Autocomplete control (See Figure 2)

It all starts with a form (shown in red outline above) that will be submitted when the "Lookup Funds" button is clicked. We'll set the action attribute to "autocomplete" to match the name servlet-name in the web.xml file (to be configured later on):

The Difference Between GET and POST

When we wrote the Java code for the AutocompleteServlet, we overrode the HttpServlet class's protected doGet() method. It handles GET requests. The HttpServlet class also provides a doPost() method to handle POST requests (See Figure 3)

When the user enters text in the Autocomplete field and clicks the "Lookup Funds" button, there are two ways that the form data can be sent to the server:

The GET method appends name/value pairs to the URL. This option is best suited for short requests, because the length of a URL is limited to 256 on most browsers, although, according to Microsoft, Internet Explorer has a maximum length of 2048 characters. As a result, the URL could be truncated if the form uses a large number of parameters or if the parameters contain large amounts of data. Another caveat is that the parameters are visible in the address field of the browser — not the best place for sensitive data.

The POST method, however, packages the name/value pairs inside the body of the HTTP request, which makes for a cleaner URL and imposes no size limitations on the forms output. It's also more secure since it takes some technical knowhow to intercept the request. Web programming languages makes it simple to retrieve name/value pairs using either method. For instance, in Java, a parameter can be retrieved using the HttpServletRequest object's getParameter(java.lang.String arg0) function, regardless of the method used.

We're using the GET method because our requests will be quite short and the fields contain no sensitive information.

The Servlet Action

A hidden field called "action" will tell the servlet that we want to look up matching funds by name. This is not to be confused with the form's action attribute, which sets the page that the form's contents will be sent to, the action field will be passed to the Autocomplete servlet as a name/value pair in the URL to set the servlet action. A value of "lookupbyname" tells the servlet to search for matching funds. Here's the code for the form with the hidden field:


[next]

Recent Articles

WebReference.com site name
Popular JavaScript Framework Libraries: An Overview
Controllers: Programming Application Logic - Part 2
How to Use JavaScript to Validate Form Data
internet.com site name
Choosing the Right Online Backup Provider
Mother Avaya Nurtures Her Technology Partners
Software as a Service a Winning Model for Hotspot Provider




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
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES