My favorites | Sign in
Project Logo
                
Search
for
Updated Mar 11, 2009 by fangyidong
JSPAndAJAXExamples  
Examples of JSP and AJAX

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.


Comment by kiranraj, Feb 10, 2009

thanks! A simple example

Comment by KuanFan99, Mar 08, 2009

ggg hghgh

Comment by megarons, Apr 22, 2009

dfg

Comment by yauhun, Jun 17, 2009

Hi, Just found out the output JSON response is not compatible with Jquery getJSON function. Any idea?

Comment by i...@danielbruessler.de, Jun 17, 2009

Hey, it works very great!!

Comment by pa.mardones, Jul 03, 2009

crack que buen ejemplo perro te pasaste

Comment by deep.kaul, Jul 12, 2009

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..!!!

Comment by vikash.1234.khatri, Aug 10, 2009

Just Great!

Comment by kausalya.tce, Oct 05, 2009

server side jsp is not working for me.... In which WEB-INS/lib the jar file have to put in

Comment by jadhav.dilipkumar, Oct 11, 2009

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.

Comment by mven...@gmail.com, Nov 02, 2009

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

Comment by erich.reich, Dec 07, 2009

This is amazing and will help my project greatly. Thank you very much for this!

Comment by bharath.mvkumar, Dec 17, 2009

Thanks a lot.. I was looking to develop a web 2.0 application using dojo and jsp at the server side.. Thanks a ton!

Comment by sukicbe, Jan 05 (2 days ago)

fd


Sign in to add a comment
Hosted by Google Code