BLOG main image
분류 전체보기 (313)
교육 (13)
NEIS (6)
Edufine (0)
Programmer (5)
Android Programming (1)
Internet W3 (18)
JAVA Programming (9)
JSP/Servlet (1)
Framework (7)
Spring For Beginner (4)
eGovFrame (10)
MEAN Stack (2)
NodeJS (5)
SublimeText (30)
SublimeText_Tips (18)
Eclipse (16)
JavaScript (8)
jQuery (12)
jQuery_tips (1)
Ajax (3)
DWR(Direct Web Remote) (4)
JSON(JS Object Notation) (4)
Oracle (2)
MySQL (28)
OS (16)
Download (3)
Life (10)
Favorit Site (1)
Books (2)
Healthy (1)
Stocks (1)
HTML5/CSS (1)
Python (4)
Security (7)
CISSP (0)
Ruby On Rails (5)
일기장 (0)
영어 교과서(중2) (3)
알고리즘 (0)
Go Lang (3)
VB 2010 (12)
C# (1)
정보보안기사(네트워크보안) (0)
업무 활용 엑셀 (11)
틈틈이 활용팁 (14)
하루 하루 살아가며 ……. (2)
기술 (1)
파이썬 & 데이터분석 (1)
Visitors up to today!
Today hit, Yesterday hit
daisy rss
tistory 티스토리 가입하기!
2015. 7. 17. 20:31

Example 1 - Server side JSP encoding

service.jsp:

  <%@page contentType="text/html; charset=UTF-8"%> 
  <%@page import="org.json.simple.JSONObject"%> 
  <% 
    JSONObject obj=new JSONObject(); 
    obj.put("name","foo"); 
    obj.put("num",new Integer(100)); 
    obj.put("balance",new Double(1000.21)); 
    obj.put("is_vip",new Boolean(true)); 
    obj.put("nickname",null); 
    out.print(obj); 
    out.flush(); 
  %>

Please note that you need to place json_simple-1.1.jar in WEB-INF/lib before running the JSP. Then the client side will get the resulting JSON text.

Example 2 - Client side XMLHttpRequest

client.html:

<html> 
<head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
</head> 
 
<script type="text/javascript"> 
function createXMLHttpRequest(){ 
  // See http://en.wikipedia.org/wiki/XMLHttpRequest 
  // Provide the XMLHttpRequest class for IE 5.x-6.x: 
  if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() { 
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {} 
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {} 
    try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {} 
    try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {} 
    throw new Error( "This browser does not support XMLHttpRequest." ) 
  }; 
  return new XMLHttpRequest(); 
} 
 
var AJAX = createXMLHttpRequest(); 
 
function handler() { 
  if(AJAX.readyState == 4 && AJAX.status == 200) { 
      var json = eval('(' + AJAX.responseText +')'); 
      alert('Success. Result: name => ' + json.name + ',' + 'balance => ' + json.balance); 
  }else if (AJAX.readyState == 4 && AJAX.status != 200) { 
    alert('Something went wrong...'); 
  } 
} 
 
function show(){ 
  AJAX.onreadystatechange = handler; 
  AJAX.open("GET", "service.jsp"); 
  AJAX.send(""); 
}; 
</script> 
 
<body> 
  <a href="#" onclick="javascript:show();"> Click here to get JSON data from the server side</a> 
</html>

Please place client.html and service.jsp (see Example 1) in the same directory and then open client.html in IE or Firefox, click the link and you'll get result.

[출처] http://code.google.com/p/json-simple/wiki/JSPAndAJAXExamples

반응형