Hello, there was a problem. I have a user table. I want to create such a text input, in which at the time of entering the name, all the names that coincide with the entered text appear (for the moment).

For example, there are three people in the table:

  1. Vasya Pupkin
  2. Petya Ivanov
  3. Vasya Petrov

When you enter Vasya in the input, the drop-down list immediately suggests choosing a person from two Basilians, enter further Pu ... already offers a specific person. There are many such examples on different sites, at least Google.

But, for a beginner who is just mastering web programming, this task is a bit difficult. And its complexity is not only that I don’t know how to implement it, but it’s already difficult to google the necessary information. Because, each person calls it differently, and each does it in some incredible way (and mostly php code everywhere). And I believe that this is not such a complicated action. Thank!

Java language, DB MySQL.

ps At least throw links to topics (in English or Slavic language, it is not so significant), where this problem is described.

  • Will it be a desktop application or a web? or some other? - Mikhail Vaysman
  • Web application, using jsp and servlet (yes, of course, outdated technology, but we must start with a simple one). - resolution

1 answer 1

You can implement this input field as follows:

  1. A servlet that will search the database for the entered characters of users and return a list of matches in the form of json. To communicate with the base, you can use pure JDBC or some ORM framework, for example JPA.
  2. The page with the field to enter - jsp or just html
  3. JQuery library and Autocomplete plugin
  4. Something like this:

    // id вашего поля $( "#search" ).autocomplete({ // URL сервлета source: "/user/search", minLength: 2 }); 
  • It will not be possible to execute a query for all names, write them into a session or into a query, then somehow pull it out using autocomplete, it's a little difficult to imagine how it works, does it send a query for each character? How then to make such a request? - resolution
  • and if you have 100,000 users? Autocomplete can be configured to the desired behavior - how many characters to wait, how long to wait between characters, and so on. My code is a very primitive example. - Mikhail Vaysman
  • Then I do not understand one, how to make requests to the database? - resolution
  • I supplemented the answer with this information - Mikhail Vaysman
  • I understand how to contact the base) I do not understand how the request will be made, as soon as I enter 2 characters, I have to pull them out like that with the request, then make the request select name from users where name = 'Ва'; Then return a string with all the names separated by a comma and create json? - resolution