在集合中创建一个Map对象
DataServlet中把请求转发给index.jsp然后在该jsp页面中使用EL表达式遍历Map
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,Integer> map = new HashMap<>();
map.put("tom", 9);
map.put("rose", 18);
map.put("lili", 27);
request.setAttribute("map", map);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
<%@page import="com.zzu.vo.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:forEach var="m" items="${map }">
${m.key } ---> ${m.value } <br>
</c:forEach>
</body>
</html>
页面内输出结果为: