请将5个学生对象存入到List集合,用JSTL循环输出该集合中的学生对象。要求:
1、学生属性有:姓名、年龄、成绩;
2、输出学生信息用表格提示;
3、输出学生的平均年龄和平均分
.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1px">
<tr>
<td>姓名</td>
<td>年龄</td>
<td>成绩</td>
</tr>
<!-- 循环输出 -->
<c:forEach items="${list1}" var="item">
<tr>
<td><c:out value="${item.name}" escapeXml="false"></c:out></td>
<td><c:out value="${item.age}" escapeXml="false"></c:out></td>
<td><c:out value="${item.cj}" escapeXml="false"></c:out></td>
</tr>
</c:forEach>
</table>
<h3>
<!-- 先拿值求和再除list.siez() -->
<c:forEach items="${list1}" var="use" >
<c:set var="ageSum" value="${ageSum + use.age }"/> <!-- 年龄和 -->
<c:set var="cjSum" value="${cjSum + use.cj }"/> <!-- 成绩和 -->
</c:forEach>
平均年龄:${ageSum/list1.size()}<br/> <!-- 平均 -->
平均成绩:${cjSum/list1.size()}
</h3>
</body>
</html>
.servlet页面
package com.zktr01702.controller;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zktr01702.model.L9Yx2L;
/**
* Servlet implementation class L9Yx2Servlet
*/
@WebServlet("/l9yx2servlet")
public class L9Yx2Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public L9Yx2Servlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
//实体
L9Yx2L l=new L9Yx2L("王大",19,100.00);
L9Yx2L l1=new L9Yx2L("张三",12,90.00);
L9Yx2L l2=new L9Yx2L("李四",13,80.00);
L9Yx2L l3=new L9Yx2L("刘邦",149,97.00);
L9Yx2L l4=new L9Yx2L("项羽",196,95.00);
//List
List<L9Yx2L> list=new ArrayList<>();
list.add(l);
list.add(l1);
list.add(l2);
list.add(l3);
list.add(l4);
request.setAttribute("list1", list);
request.getRequestDispatcher("L9/Yx2.jsp").forward(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
.实体类
package com.zktr01702.model;
public class L9Yx2L {
private String name;//姓名
private int age;//年龄
private double cj;/成绩
public int getAge() {
return age;
}
public double getCj() {
return cj;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age = age;
}
public void setCj(double cj) {
this.cj = cj;
}
public void setName(String name) {
this.name = name;
}
public L9Yx2L(String name, int age, double cj) {
super();
this.name = name;
this.age = age;
this.cj = cj;
}
public L9Yx2L() {
super();
}
@Override
public String toString() {
return "L9Yx2L [name=" + name + ", age=" + age + ", cj=" + cj + "]";
}
}