首先所涉及到知识点:Javase部分的list,map,html前端css和table表格
servlet中配置list和map集合
package com.servlet;
import
java.io
.IOException;
import
java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySecondServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) //
throws ServletException, IOException {
System.out.println("运行");
//编译map和list
Map map = new HashMap();
List<Map> list =new ArrayList<Map>();
map.put("name","caicai");
map.put("age","18");
map.put("hobby","goushi");
list.add(map);
Map map1 = new HashMap();
map1.put("name","gaochao");
map1.put("age","19");
map1.put("hobby","dagoushi");
list.add(map1);
Map map2 = new HashMap();
map2.put("name","san");
map2.put("age","20");
map2.put("hobby","xiaogoushi");
list.add(map2);
Map map3 = new HashMap();
map3.put("name","dashen");
map3.put("age","18");
map3.put("hobby","zhushi");
list.add(map3);
Map map4 = new HashMap();
map4.put("name","laoliu");
map4.put("age","22");
map4.put("hobby","gezhongshi");
list.add(map4);
Map map5 = new HashMap();
map5.put("name","me");
map5.put("age","17");
map5.put("hobby","lm");
list.add(map5);
for (Map map_1 : list) {
System.out.println(map_1);
}
request.setAttribute("key_list",list);
request.getRequestDispatcher("/index.jsp").forward(request, respons);
}
}
import
.IOException;
import
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MySecondServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) //
throws ServletException, IOException {
System.out.println("运行");
//编译map和list
Map map = new HashMap();
List<Map> list =new ArrayList<Map>();
map.put("name","caicai");
map.put("age","18");
map.put("hobby","goushi");
list.add(map);
Map map1 = new HashMap();
map1.put("name","gaochao");
map1.put("age","19");
map1.put("hobby","dagoushi");
list.add(map1);
Map map2 = new HashMap();
map2.put("name","san");
map2.put("age","20");
map2.put("hobby","xiaogoushi");
list.add(map2);
Map map3 = new HashMap();
map3.put("name","dashen");
map3.put("age","18");
map3.put("hobby","zhushi");
list.add(map3);
Map map4 = new HashMap();
map4.put("name","laoliu");
map4.put("age","22");
map4.put("hobby","gezhongshi");
list.add(map4);
Map map5 = new HashMap();
map5.put("name","me");
map5.put("age","17");
map5.put("hobby","lm");
list.add(map5);
for (Map map_1 : list) {
System.out.println(map_1);
}
request.setAttribute("key_list",list);
request.getRequestDispatcher("/index.jsp").forward(request, respons);
}
}
jsp中配置jstl的内容
<%@ page language="java" contentType="text/html; UTF-8"pageEncoding="GBK"%>
<%@ taglib uri="
http://java.sun.com/jsp/jstl/core " prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd ">
<%@ taglib uri="
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<style>
th, tr, td, table {
border: 1px solid red;
}
</style>
</head>
<body>
<table>
<tr>
<th>name</th>
<th>age</th>
<th>hobby</th>
</tr>
<c:forEach items="${key_list}" var="usr" varStatus="idx">
<tr>
<td>${
usr.name
}</td> <td>${usr.age}</td> <td>${usr.hobby}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<style>
th, tr, td, table {
border: 1px solid red;
}
</style>
</head>
<body>
<table>
<tr>
<th>name</th>
<th>age</th>
<th>hobby</th>
</tr>
<c:forEach items="${key_list}" var="usr" varStatus="idx">
<tr>
<td>${
}</td> <td>${usr.age}</td> <td>${usr.hobby}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
重点代码已用红字标出
遍历结果如下图