How to send from a servlet with gson to run a field with the typehead.js library?

1

When I place json locally, the autocomplete works. I'm using the tagsinput boopstrap library.

/*var equipos = new Bloodhound({
        datumTokenizer: Bloodhound.tokenizers.obj.whitespace('text'),
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        local: [
            {text: 'metronic', id: '2', estado: 'BUENO'},
            {text: 'keenthemes', id: '6', estado: 'INOPERATIVO'},
            {text: 'metronic theme', id: '5', estado: 'REGULAR'},
            {text: 'metronic template', id: '4', estado: 'BUENO'},
            {text: 'keenthemes team', id: '3', estado: 'REGULAR'}
        ]
    });*/
    /*local:           [
     {
     "id": "1",
     "text": "001 / COMPRESOR 1",
     "estado": "REGULAR"
     },
     {
     "id": "6",
     "text": "001001 / SUB COMPRESOR 1.0",
     "estado": "REGULAR"
     },
     {
     "id": "7",
     "text": "001002 / COMPRESOR 3",
     "estado": "BUENO"
     },
     {
     "id": "2",
     "text": "002 / COMPRESOR 2",
     "estado": "BUENO"
     },
     {
     "id": "10",
     "text": "003 / COMPRESOR CRISTAL 15*10",
     "estado": "INOPERATIVO"
     }
     ]*/
//   equipos.initialize();
    
asked by Luis Castillo Elera 09.05.2016 в 08:46
source

1 answer

1

What you want to do is something similar to a REST service, I would recommend using Spring and its @RestController instead of Servlets.

But if this is not possible, what you need is something like this:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          JSONArray array= new JSONArray();
          response.setContentType("application/json");
          PrintWriter out = response.getWriter();
          out.print(array);
          out.flush();
     }

Now you only have to make requests with Javascript by GET to the servlet.

JSONArray is an object in a library called JSON.simple

But I guess it's valid just like for Guava.

    
answered by 31.08.2016 в 20:43