Monday, April 22, 2013

servlet to jsp using json (object passing)

jquery


$('.snd').click(function (){
    $.ajax({
        url: '/M08CDECUStructuralOptimiser/NodeAction?action=test',
        data: {cl1: $('.t11').val()},
        dataType: "json",
        success: function (response){
            alert(response[1]);
        }
    });
});



Servlet



if (action.compareTo("test") == 0) {
           
            String[] ss= {"n1","n2"};
            Form f1=new Form(1,2,3);
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            try {

                Gson gson = new GsonBuilder().setPrettyPrinting().create();
                String json=gson.toJson(ss);
               
                out.println(json);
            } finally {
                out.close();
            }
        }