package FrameworkWrapper;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ServletResponse extends HttpServlet
{
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
Enumeration keys;
String key;
String myName = "";
keys = request.getParameterNames();
while (keys.hasMoreElements())
{
key = (String) keys.nextElement();
if (key.equalsIgnoreCase("myName")) myName = request.getParameter(key);
}
System.out.println("Name = ");
if (myName == "") myName = "Hello";
response.setContentType("text/html");
response.setHeader("Pragma", "No-cache");
response.setDateHeader("Expires", 0);
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
out.println("json object");
out.flush();
// ---------------- Note:One approach--------------------------
//response.setContentType("application/json");
// Get the printwriter object from response to write the required json object to the output stream
// PrintWriter out = response.getWriter();
// Assuming your json object is **jsonObject**, perform the following, it will return your json object
//out.print(jsonObject);
//out.flush();
// ---------------- Note:One approach--------------------------
// JSONObject json = new JSONObject();
// JSONArray addresses = new JSONArray();
// JSONObject address;
// try
// {
// int count = 15;
//
// for (int i=0 ; i<count ; i++)
// {
// address = new JSONObject();
// address.put("CustomerName" , "Decepticons" + i);
// address.put("AccountId" , "1999" + i);
// address.put("SiteId" , "1888" + i);
// address.put("Number" , "7" + i);
// address.put("Building" , "StarScream Skyscraper" + i);
// address.put("Street" , "Devestator Avenue" + i);
// address.put("City" , "Megatron City" + i);
// address.put("ZipCode" , "ZZ00 XX1" + i);
// address.put("Country" , "CyberTron" + i);
// addresses.add(address);
// }
// json.put("Addresses", addresses);
// }
// catch (JSONException jse)
// {
//
// }
// response.setContentType("application/json");
// response.getWriter().write(json.toString());
// ---------------- Note:One approach--------------------------
// /**
// * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
// */
// protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//
// request.setCharacterEncoding("utf8");
// response.setCharacterEncoding("utf8");
// response.setContentType("application/json");
// PrintWriter out = response.getWriter();
// JSONObject jsonObj = (JSONObject) JSONValue.parse(request.getParameter("para"));
// System.out.println(jsonObj.get("message"));
// JSONObject obj = new JSONObject();
// obj.put("message", "hello from server");
// out.print(obj);
//
// }
}
}
No comments:
Post a Comment