java代码中后台向前台传递list或map集合案例
导入jar包
新建一个servert传递map集合
ajax.java代码:
1 package servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.List; 8 import java.util.Map; 9 10 import javax.servlet.ServletException; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 import net.sf.json.JSONArray; 16 import net.sf.json.JSONObject; 17 18 public class Ajax extends HttpServlet { 19 public void doGet(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 doPost(request, response); 22 } 23 public void doPost(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 response.setContentType("text/plain"); 26 response.setCharacterEncoding("utf-8"); 27 //模拟数据库数据 28 Map<String, String> map=new HashMap<String, String>(); 29 map.put("1", "a"); 30 map.put("2", "ab"); 31 map.put("3", "王东"); 32 map.put("4", "毛东"); 33 //前台传过来的参数 34 String data=request.getParameter("data"); 35 JSONObject re=JSONObject.fromObject(map); 36 String result=re.toString(); 37 System.out.println("result"+result); 38 PrintWriter writer=response.getWriter(); 39 writer.write(result);//返回json数组 40 writer.flush(); 41 writer.close(); 42 } 43 44 }
前台页面:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>2.html</title> 5 <meta charset="utf-8"> 6 <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> 7 <script type="text/javascript"> 8 function ajax(){ 9 $.ajax({ 10 url:"servlet/Ajax", 11 data:"data=r", 12 type:"post", 13 error:function(){alert("服务器连接失败");}, 14 success:function(data){ 15 data=eval(data); 16 for(var key in data){ 17 alert(data) 18 } 19 } 20 }); 21 } 22 </script> 23 </head> 24 25 <body> 26 <input type="button" value="提交" onclick="ajax()" /> 27 </body> 28 </html>
修改ajax.java,向前台传递list
1 package servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.List; 8 import java.util.Map; 9 10 import javax.servlet.ServletException; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 import net.sf.json.JSONArray; 16 import net.sf.json.JSONObject; 17 18 public class Ajax extends HttpServlet { 19 public void doGet(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 doPost(request, response); 22 } 23 public void doPost(HttpServletRequest request, HttpServletResponse response) 24 throws ServletException, IOException { 25 response.setContentType("text/plain"); 26 response.setCharacterEncoding("utf-8"); 27 List<String> list=new ArrayList<String>(); 28 String data=request.getParameter("data"); 29 list.add("afds"); 30 list.add("反对反对"); 31 list.add(""); 32 33 JSONArray jsonArray=JSONArray.fromObject(list); 34 String result=jsonArray.toString(); 35 System.out.println(result); 36 PrintWriter writer=response.getWriter(); 37 writer.write(result);//返回json数组 38 writer.flush(); 39 writer.close(); 40 } 41 42 }
静态页面代码:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>2.html</title> 5 <meta charset="utf-8"> 6 <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> 7 <script type="text/javascript"> 8 function ajax(){ 9 $.ajax({ 10 url:"servlet/Ajax", 11 data:"data=r", 12 type:"post", 13 error:function(){alert("服务器连接失败");}, 14 success:function(data){ 15 data=eval(data); 16 for(var i=0;i<data.length;i++){ 17 alert(data[i]); 18 } 19 20 } 21 }); 22 } 23 </script> 24 </head> 25 26 <body> 27 <input type="button" value="提交" onclick="ajax()" /> 28 </body> 29 </html>