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>
时间: 2024-10-29 17:23:54

java代码中后台向前台传递list或map集合案例的相关文章

Java代码中获取Json的key值

测试json字符串: {"access_token":"hkbQl5o_l67dZ7_vJRATKBwTLk9Yj5QyMuOJThAr8Baj0xWf4wxW1p4ym4iTZ3-ptWRRoVtbCNJnyjGLfed_4bB0HiqGP8v-aXOJUUk1tFs","expires_in":7200,"refresh_token":"_or_31yEp8XhxGWvrAzdWIkXfFlKjfb3iTRpfa

Android如何在java代码中设置margin

习惯了直接在xml里设置margin(距离上下左右都是10dip),如: <ImageView android:layout_margin="10dip" android:src="@drawable/image" /> 只是有些情况下,需要在java代码里来写. API中,android.view.ViewGroup.MarginLayoutParams有个方法setMargins(left, top, right, bottom).可是View本身没

java代码中获取进程process id(转)

另一方面,线程ID=进程ID+内部线程对象ID并不成立,    参考: blog.csdn.net/heyetina/article/details/6633901     如何在java代码中获取进程process id, 实现方法如下所示:    参考: rednaxelafx.iteye.com/blog/716918 http://www.cnblogs.com/mumuxinfei/p/3678854.html

【安卓】在java代码中设置drawableLeft时如何给定合适尺寸?

textView.setCompoundDrawables(drawable, null, null, null);时看不到图片,是因为需要手动给定drawable对应的尺寸,即用drawable.setBounds. 如果该drawable为图片,可直接drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());. 即直接给定图片自身尺寸,此时效果和在xml中给定一样. [安卓]在java

如何在java代码中调用一个web项目jsp或者servlet

有时候需要调用一个web项目的jsp或者servlet,但是执行内部的代码,并不是打开jsp,例如需要在一段java代码中清除一个web项目中的缓存,那么可以把清除缓存的代码放在该web项目的一个servlet中,只需要执行如下代码: URL url = new URL("http://192.168.2.123:8080/sace/ClearCache"); url.openStream(); openStream() 执行一次相当于一次URL请求,其中url.openStream(

Spring MVC框架下在java代码中访问applicationContext.xml文件中配置的文件(可以用于读取配置文件内容)

<bean id="propertyConfigurer" class="com.****.framework.core.SpringPropertiesUtil" lazy-init="false"> <property name="locations"> <list> <value>classpath:config/sys.properties</value> &

关于在Java代码中写Sql语句需要注意的问题

最近做程序,时不时需要自己去手动将sql语句直接写入到Java代码中,写入sql语句时,需要注意几个小问题. 先看我之前写的几句简单的sql语句,自以为没有问题,但是编译直接报错. 1 String str = "insert into XXX(a,b,c) values ('"a.getA()"','"a.getB()"','"a.getC()"');"; 研究了半天发现应该是连接字符串问题,第一次修改过后将赋值字段前后加“

【转】java代码中实现android背景选择的selector-StateListDrawable的应用

原文网址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0924/1712.html 下面的代码应该很多人都熟悉: 1 2 3 4 5 6 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">

java代码中获取classpath路径

下载ava web工程中,有时候需要自己手动的去读取classpath下面的配置文件,这里总结一点读取classpath路径的方法,分享一下. 方法一: Java代码 下载 String path = Test.class.getResource("/").toString(); System.out.println("path = " + path); String path = Test.class.getResource("/").toSt