jar:
commons-beanutils-1.7.0.jar commons-collections-3.2.jar commons-lang-2.3.jar commons-logging-1.1.jar ezmorph-1.0.4.jar json-lib-2.2.2-jdk15.jar 下载地址:http://www.docjar.com/
<%@ page language="java" pageEncoding="utf-8"%> <html> <head> <title>jQuery&json</title> <mce:style type="text/css"><!-- .css_a { background:#ecf6fc; /*这行将给所有的tr加上背景色*/ } .css_b { background:#bcd4ec; /*这行将给所有的tr加上背景色*/ } .css_c { background:#0066FF; } --></mce:style> <mce:script type="text/javascript" src="./jquery.js" mce_src="jquery.js"></mce:script> <mce:script type="text/javascript"><!-- $(document).ready(function(){ $("#ajax1").click(function(){ var now=new Date().getTime(); var data = {param1:"参数一", param2:"参数二", param3:"参数三", param4:"参数4"}; $.getJSON('jobject.do?now='+now, data, function(json){//now表示每次点击都会发送新的请求 var str=""; str = "<table id = 'table1'><tr id ='tr'><td>param1</td><td>param2</td><td>param3</td><td>param4</td>"; //jsonObject str +="<tr class = 'tr1' ><td>"+json.param1+"</td>"; str +="<td>"+json.param2+"</td>" str +="<td>"+json.param3+"</td>" str +="<td>"+json.param4+"</td></tr>" //jsonArray /* array4 var len = json.length; for(var i=0; i<len; i++) { str +="<tr class = 'tr1' ><td>"+json[i].col+"</td>"; str +="<td>"+json[i].row+"</td>" str +="<td>"+json[i].value+"</td>" str +="<td>"+json[i]+"</td></tr>" } */ str +="</table>"; $('#div1').html(str); $('.tr1').each(function(i){ if (i%2==0) { $(this).addClass("css_a"); } }); $('.tr1').each(function(i){ if (i%2==1) { $(this).addClass("css_b"); } }); $('#table1 .tr1').hover(function() { $(this).addClass('css_c'); }, function() { $(this).removeClass('css_c'); } ); }); }); }); // --></mce:script> </head> <body> <div id="div1"></div> <input type="button" id="ajax1" value="showme"> </body> </html>
package json.servlet; import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import json.bean.JsonBean; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class JObject extends HttpServlet { public void service(ServletRequest request, ServletResponse response) { response.setContentType("text/html; charset=utf-8"); System.out.println("JObject"); String param1 = request.getParameter("param1"); String param2 = request.getParameter("param2"); String param3 = request.getParameter("param3"); String param4 = request.getParameter("param4"); try { param1 = new String(param1.getBytes("iso-8859-1"), "utf-8"); param2 = new String(param2.getBytes("iso-8859-1"), "utf-8"); param3 = new String(param3.getBytes("iso-8859-1"), "utf-8"); param4 = new String(param4.getBytes("iso-8859-1"), "utf-8"); } catch (Exception e) { } System.out.println("param1 = " + param1); System.out.println("param2 = " + param2); /** * JSONObject */ JSONObject jsonObject = new JSONObject(); jsonObject.put("param1", param1); jsonObject.put("param2", param2); jsonObject.put("param3", param3); jsonObject.put("param4", param4); /* Map map = new HashMap(); map.put("name", "json"); map.put("bool", Boolean.TRUE); map.put("int", new Integer(1)); map.put("arr", new String[] { "a", "b" }); map.put("func", "function(i){ return this.arr[i]; }"); JSONObject jsonObject = JSONObject.fromObject(map); */ /* JsonBean bean = new JsonBean(); bean.setCol(1); bean.setRow(2); bean.setValue("3"); JSONObject jsonObject = JSONObject.fromObject(bean); */ /** * JSONArray */ /* array1 List list = new ArrayList(); list.add("first"); list.add("second"); list.add("third"); list.add("第四"); JSONArray jsonArray = JSONArray.fromObject(list); */ /* array2 boolean[] boolArray = new boolean[] { true, false, true }; JSONArray jsonArray = JSONArray.fromObject(boolArray); */ /* array3 JSONArray jsonArray = JSONArray.fromObject("['json','is','easy']" ); */ /* array4 List<JsonBean> list2 = new ArrayList<JsonBean>(); JsonBean jb1 = new JsonBean(); jb1.setCol(1); jb1.setRow(1); jb1.setValue("xx"); JsonBean jb2 = new JsonBean(); jb2.setCol(2); jb2.setRow(2); jb2.setValue("yy"); list2.add(jb1); list2.add(jb2); JSONArray jsonArray = JSONArray.fromObject(list2); */ try { PrintWriter out = response.getWriter(); out.println(jsonObject.toString()); //object //out.println(jsonArray.toString()); //array out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } } |