|
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
Hi, Please help.
I'm a beginner. As said above, I copied json_simple-1.1.jar into WEB-INF folder and found this error. "Class org.json.simple.JSONObject not found in import.".
Do I need to download org.json.JSONObject.jar too?
json_simple-1.1.jar should be enough. Please make sure you put it in WEB-INF/lib (instead of WEB-INF).
Thx a tone!!! Vinod Kumar(vinodiap)
I've always been confused by the use of the "pseudo-procotol" in HTML event handler attributes, as show in the example above... "onclick='javascript:show()'" The pseudo-protocol is completely unnecessary. "onclick='show()'" is sufficient, and will execute exactly the same.
how you could run something like this in google app engine?
Wow! I was looking for an easy and simple example about handle JSON object interchange between client and server using JSP, like this one. Thanks so much!
thanx!!!!!!!!!!!!!!!!!
loco
FrameWork?: JQuery 1.4.2 client.html
<html> <head>
function show2() { });} </script> </head> <body>
</html>thanks ^^
The import org.json cannot be resolved, error i am getting, what to do for that?
Thank-you for the example!
good!
great!
My Client.html is not making a call to service.jsp!!
i am passing the data from servlet but when i am passing the source its not identifying and i am getting alerts from the function as undefined json
Thanks!
Thanks!
this one is to get data to client side ,what if i want to send data to server and store it in database
Please How to send data to server and store it in database?
wow..helped me a lot..; Can we create multidimensional json object?
please help me.it is error undefind
it worked for me..
nice one.
thanks for giving this small and helpfull example
by itsme_vidhi1010@rediffmail.com
thanks! very nice examples.
sir\mem,i am not understand how to execute the program of ajax with help jsp program.......... plz help me...........send me tree structure of execution of program
僕が成功した。ありがとう!