com.bbk.cgac.controller.pagecontroller.ax.SampleAjaxPageController.java Source code

Java tutorial

Introduction

Here is the source code for com.bbk.cgac.controller.pagecontroller.ax.SampleAjaxPageController.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 com.bbk.cgac.controller.pagecontroller.ax;

import com.bbk.cgac.controller.frontcontroller.TempObj;
import com.bbk.cgac.json.JSON;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SampleAjaxPageController extends AbstractAjaxPageController {

    public SampleAjaxPageController(HttpServletRequest req, HttpServletResponse resp) {
        super(req, resp);
    }

    @Override
    public void execute() throws IOException { //Sample Code
        //Test Request
        JsonElement jel = mJson;
        if (jel.isJsonArray()) {
            JsonArray jarr = jel.getAsJsonArray();
            if (jarr.get(0).isJsonObject()) {
                JsonObject jobj = jarr.get(0).getAsJsonObject();
                System.out.println("JsonObject .");
                System.out.println("a: " + jobj.get("a"));
            }
            if (jarr.get(1).isJsonArray()) {
                JsonArray jarr2 = jarr.get(1).getAsJsonArray();
                System.out.println("jarr[1]? JsonArray .");
            }
        }
        //Test Response (Example)
        TempObj obj = new TempObj("Kim");
        String arg01 = JSON.parseToString(obj);
        TempObj obj1 = new TempObj("Lee");
        String arg02 = JSON.parseToString(obj1);
        mResponse.setContentType("text/html;charset=UTF-8");
        mResponse.getWriter().write("[" + arg01 + "," + arg02 + "]");
    }
}