|
JSPAndAJAXExamples
Examples of JSP and AJAX
Example 1 - Server side JSP encodingservice.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 XMLHttpRequestclient.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. |
► Sign in to add a comment
thanks! A simple example
ggg hghgh
dfg
Hi, Just found out the output JSON response is not compatible with Jquery getJSON function. Any idea?
Hey, it works very great!!
crack que buen ejemplo perro te pasaste
A great tool... A No Frills library. something exactly what I was looking for. No dependencies on common-jar, logging blah blah...
Thanks a TON..!!!
Just Great!
server side jsp is not working for me.... In which WEB-INS/lib the jar file have to put in
Kausalya.tce: You would need to add the json_simple-1.1.jar in the WEB-INF/lib folder of your web application. You can just paste the jar file in the lib folder without creating any subfolders. Please post the exact error message that you get when you run the server side jsp. Creators of this utility: Thank you so much....As the name suggests this is really really simple to implement.
Nice Example Thank you very much. Really when i am thinking about sending arrayList using Ajax to client side . JSON is good approach .
for More reference on JSON : JSON.org
This is amazing and will help my project greatly. Thank you very much for this!
Thanks a lot.. I was looking to develop a web 2.0 application using dojo and jsp at the server side.. Thanks a ton!
fd