java代码中后台向前台传递list或map集合案例

时间:2021-09-01 20:45:09

导入jar包

java代码中后台向前台传递list或map集合案例

新建一个servert传递map集合

ajax.java代码:

 package servlet;

 import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class Ajax extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
//模拟数据库数据
Map<String, String> map=new HashMap<String, String>();
map.put("1", "a");
map.put("2", "ab");
map.put("3", "王东");
map.put("4", "毛东");
//前台传过来的参数
String data=request.getParameter("data");
JSONObject re=JSONObject.fromObject(map);
String result=re.toString();
System.out.println("result"+result);
PrintWriter writer=response.getWriter();
writer.write(result);//返回json数组
writer.flush();
writer.close();
} }

前台页面:

 <!DOCTYPE html>
<html>
<head>
<title>2.html</title>
<meta charset="utf-8">
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function ajax(){
$.ajax({
url:"servlet/Ajax",
data:"data=r",
type:"post",
error:function(){alert("服务器连接失败");},
success:function(data){
data=eval(data);
for(var key in data){
alert(data)
}
}
});
}
</script>
</head> <body>
<input type="button" value="提交" onclick="ajax()" />
</body>
</html>

修改ajax.java,向前台传递list

 package servlet;

 import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class Ajax extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("utf-8");
List<String> list=new ArrayList<String>();
String data=request.getParameter("data");
list.add("afds");
list.add("反对反对");
list.add(""); JSONArray jsonArray=JSONArray.fromObject(list);
String result=jsonArray.toString();
System.out.println(result);
PrintWriter writer=response.getWriter();
writer.write(result);//返回json数组
writer.flush();
writer.close();
} }

静态页面代码:

 <!DOCTYPE html>
<html>
<head>
<title>2.html</title>
<meta charset="utf-8">
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function ajax(){
$.ajax({
url:"servlet/Ajax",
data:"data=r",
type:"post",
error:function(){alert("服务器连接失败");},
success:function(data){
data=eval(data);
for(var i=0;i<data.length;i++){
alert(data[i]);
} }
});
}
</script>
</head> <body>
<input type="button" value="提交" onclick="ajax()" />
</body>
</html>