初试JSON

弄了一天的json终于把环境给配置出来!!还真的很烦,官方都不提供相关的依赖包下载还要挨个挨个的找累死咯

顺便写了个小例子,自己备份下!!

1.

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <script type="text/javascript">
  	var xmlhttp;
  	function validate(){
  		if(window.ActiveXObject){
  			xmlhttp=new ActiveXObject("Microsoft.XMLHttp");
  		}else if(window.XMLHttpRequest){
  			xmlhttp=new XMLHttpRequest();
  		}
  		var url="testJson";
  		xmlhttp.open("get",url,true);
  		xmlhttp.onreadystatechange=callback;
  		xmlhttp.send(null);
  	}
  	function callback(){
  		if(xmlhttp.readyState==4){
  			if(xmlhttp.status==200){
  			 var data=xmlhttp.responseText;
  			 alert(data);
  			 var str = '{ "name": "Violet", "occupation": "character" }';    
   			 var obj = eval("(" + data + ")");    
    		 alert(obj.list[1]);    
  			}
  		}
  		
  	}
  </script>
  <body>
   	<input type="button" value="submit" οnclick="validate()">
  </body>
</html>

 

上面这个是客户端的测试

2. 接下来我写了个servlet来测试这个json+ajax是否可以成功

 

 

package com;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;

public class TestJson extends HttpServlet {
	private static final long serialVersionUID = 8171316224790401861L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List list = new ArrayList();
		for(int i=0;i<10;i++){
			list.add("test"+i);
		}
		JSONObject object = new JSONObject();
		object.accumulate("list", list);
		response.getWriter().print(object.toString());
	}

}

  

 接下来是我web.xml的一个配置

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>TestJson</servlet-name>
  	<servlet-class>com.TestJson</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>TestJson</servlet-name>
  	<url-pattern>/testJson</url-pattern>
  </servlet-mapping>
</web-app>

 

4.其中用的的jar包有这么几个:

commons-beanutils-1.7.0.jar
commons-collections-3.2.1.jar
commons-httpclient-3.0.1.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
ezmorph-1.0.6.jar
json-lib-2.2.3-jdk15.jar

 附件:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值