How to Create an Ajax Autocomplete Text Field: Part 3 / Page 2 | WebReference

How to Create an Ajax Autocomplete Text Field: Part 3 / Page 2

[previous] [next]

How to Create an Ajax Autocomplete Text Field: Part 3 [con't]

Generate Servlet Methods

The AutoCompleteServlet will contain two public methods, both of which extend superclass implementations. Eclipse has a command for this as well. Click on "Alt+Shift+S" to bring up the "Source" context menu and select "Override/Implement Methods..." to bring up the corresponding dialog. Under HttpServlet, click on the doGet(HttpServletRequest, HttpServletResponse) checkbox to select it. Under GenericServlet, select the init(ServletConfig) checkbox. The init() method needs the ServletConfig object to forward the request to other pages (See Figure 6)

Click on OK to close the dialog and add the methods. Here's the class with the two new methods:

The class could now run as is, but it wouldn't do anything exciting (until we add our code).

The init() Method

Similar to a regular POJO's constructor, the init() method of the GenericServlet class can be overridden to perform one-time initialization tasks, when the servlet is first called into service by the server container. Hence, the developer wouldn't explicitly call the servlet's constructor. It comes in two flavors, one without parameters and one that takes a ServletConfig parameter. We'll use the latter.

In our init() method, we need to do three things:

  1. call super.init() (already done for us!)
  2. store the context in a member variable for later use
  3. create a HashMap to store the funds

In a real world application, the funds would probably be stored in a database or another type of persistent data store, but a HashMap will do just fine here, since we're only interested in testing the behavior of our Autocomplete text control. We'll put each Fund in the HashMap using the symbol as the key:

As we've seen before, the IDE will alert us that we've not created the "context" or "funds" variables yet. Click the "X" icon in the left-hand margin of the line containing the "context" variable and select "Create field 'context in type 'AutocompleteServlet'" to create a private class member (See Figure 7).

Unfortunately, following the same procedure for the "funds" variable doesn't work because Eclipse thinks that the put() method belongs to the JSONArray class! (See Figure 8).

With no shortcuts available, you'll just have to add it yourself. (Programming can be such hard work!) Here's the code to add beneath the "context" field:

Type "Ctrl+Shift+M" to add "HashMap" to the import list. The extra "<String, Fund>" code is a new Java 1.5 feature called "Generics." They eliminate runtime errors caused by invalid casting by telling the compiler what to expect. Here, we're telling it that we'll be putting in Strings for the key and Fund classes as a complex data value. Here's the code for the new variables and the completed init() method:

The doGet() Method

The doGet() method will contain the following three local variables:

  1. a String to store the "action" parameter
  2. a String to store the "searchString" parameter
  3. a JSONArray to hold the matching Funds

Here's the code to declare and initialize them:

Next, we'll need an if statement to process various action requests from the browser. There are three possibilities, which we'll differentiate by their name. They are:

  1. complete: tells the servlet to return all the matching funds to the characters typed in the search textbox. Returns a JSONArray.
  2. lookupbyname: activated when the user hits the search button. Similar to the "complete" action, but forwards an ArrayList of funds to the Funds.jsp page to be processed by the JSTL parser.
  3. lookup: retrieves one fund based on the symbol and passes it on the the Fund.jsp page to display.

Here's the code for the if statement:

[previous] [next]