ar.com.mapfre.ejemploajaxjsonarray.ServletCargarTabla.java Source code

Java tutorial

Introduction

Here is the source code for ar.com.mapfre.ejemploajaxjsonarray.ServletCargarTabla.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.mapfre.ejemploajaxjsonarray;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author cassisi
 */
@WebServlet(name = "ServletCargarTabla", urlPatterns = { "/ServletCargarTabla" })
public class ServletCargarTabla extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        ArrayList<Paises> pais = new ArrayList<>();
        pais = ObtenerDatos.getAllCountries();
        Gson gson = new Gson();
        JsonElement element = gson.toJsonTree(pais, new TypeToken<List<Paises>>() {
        }.getType());

        JsonArray jsonArray = element.getAsJsonArray();
        response.setContentType("application/json");
        response.getWriter().print(jsonArray);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

    }

}