자바 로 작성 한 JSon 서비스, 소스 코드 목록

3952 단어
package cc.kagula;

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

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

import net.sf.json.JSONObject;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

/**
 *          
 * http://localhost:8080/WageQuery/JsonServer
 * WageQuery     (    )
 * JSonServer web.xml       
 *     
 * JDK 1.6.0_45
 * Tomcat apache-tomcat-6.0.37
 */
public class WageQueryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public WageQueryServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sR = "{\"name\":\"kagula\",\"age\":27}";
		
		MyUtility.renderJson(response, sR);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		StringBuffer jb = new StringBuffer();
		String line = null;
		try {
			BufferedReader reader = request.getReader();
		    while ((line = reader.readLine()) != null)
		      jb.append(line);
		} catch (Exception e) { /*report an error*/ }

	    try {
		    JSONObject jsonObject = JSONObject.fromObject(jb.toString());
		    Iterator it = jsonObject.keys(); //gets all the keys
		    while(it.hasNext())
		    {
		        String key = (String) it.next(); // get key
		        Object o = jsonObject.get(key); // get value
		        System.out.println(key + " : " +  o); // print the key and value
		    }
		    //http://stackoverflow.com/questions/19568142/how-to-read-json-sent-by-ajax-in-servlet
		} catch (ParseException e) {
			// crash and burn
			throw new IOException("Error parsing JSON request string");
		}
	    
		String sR = jb.toString();
		
		MyUtility.renderJson(response, sR);
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>WageQuery</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>WageQueryServlet</display-name>
    <servlet-name>WageQueryServlet</servlet-name>
    <servlet-class>cc.kagula.WageQueryServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>WageQueryServlet</servlet-name>
    <url-pattern>/JsonServer</url-pattern>
  </servlet-mapping>
</web-app>
package cc.kagula;

import javax.servlet.http.HttpServletResponse;

public class MyUtility {
	public static String renderJson(HttpServletResponse response, String content){
		response.setContentType("application/json");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		java.io.PrintWriter pw = null;
		try{
			pw = response.getWriter();
			pw.write(content);
		}catch (Exception e){
			//
		}finally{
			pw.close();
		}
		return null;
	}
}

좋은 웹페이지 즐겨찾기