9:36 am - Wednesday November 21, 2018

JSON Object With Servlet

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

ProjectStructure:

  

 

 

 

 

 

 

 

 JarFiles:

 

 

 

 

 JObject.java:

package com.candidjava;

import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.Iterator;

import javax.servlet.ServletException;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
 import org.json.JSONObject;

public class JObject extends HttpServlet {
 int length;
 String opt;
 boolean data;
 String getMark;
 String getCity;
 boolean mail;

protected void doGet(HttpServletRequest request,
 HttpServletResponse response) throws ServletException, IOException {
 JSONObject object = new JSONObject();

try {
 object.put("name", "Deepa");
 object.put("Reg No", new Integer(12345));
 object.put("Mark", new Double(99));
 object.put("mail", "deepa@ebullitent.com");
 object.put("City", "Chennai");
 length = object.length();
 opt = object.optString("name");
 data = object.isNull("name");
 getMark = object.getString("Mark");
 getCity = (String) object.get("City");
 mail = object.has("mail");
 // object.append("mark1","98");
 } catch (JSONException e) {

e.printStackTrace();
 }
 PrintWriter out = response.getWriter();

out.println("<html>");
 out.println(object);
 out.println("<head></head>");
 out.println("<body bgcolor='pink'>");
 out.println("<br/>");
 out.println("Name: " + opt);
 out.println("<br/>");
 out.println("Is Null: " + data);
 out.println("<br/>");
 out.println("city: " + getCity);
 out.println("<br/>");
 out.println("has mail: " + mail);
 out.println("<br/>");
 out.println("Mark:" + getMark);
 out.println("<br/>");
 out.println("List of keys:");
 out.println("<br/>");
 out.println("--------------");
 out.println("<br/>");

out.println("length: " + length + "\n");
 out.println("</body></html>");
 out.println();
 Iterator i = object.keys();
 while (i.hasNext()) {
 out.println(i.next());
 }
 object.toString();

}

}

 

OUTPUT:

 

 

 

 

 

 

 DOWNLOAD+ SOURCECODE

 

 

 

 

 

 

 

 

 

 

  

  

 

 

 

 

 

 

 

 

 

 

 

 

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

Filed in: Z-A JSON codes

Comments are closed.