如何从sevrlet发送ArrayList到jsp并打印

时间:2018-10-31 13:39:28

标签: jsp servlets

我在这里为学生设置数据  我想将值从Arraylist打印到jsp页面

这是servlet类

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        studentDao =new StudentDao();
        List<Student> studentList=studentDao.display();

        request.setAttribute("students",studentList);
          for(Student stu:studentList){
            System.out.println(stu.getAge()+" " +stu.getStudentName());
        }
        RequestDispatcher requestDispatcher=getServletContext().getRequestDispatcher("/viewstudent.jsp");
        requestDispatcher.forward(request,response);
    }//method end

//这是jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
    <title>view Student</title>
</head>
<body>
<table >
<c:forEach items="${students}" var="student">
    <tr>
        <td><c:out value="${student.roll_no}"></c:out></td>
    </tr>
    </c:forEach>
</table>
</body>
</html>

1 个答案:

答案 0 :(得分:-1)

getRequestDispatcher("/viewstudent.jsp?students="+studentList);

在jsp上,只需接受get方法的值,例如

List<Student> studentList = (List<Student>)request.getParameter("students")

像这样打印出来;

<%for(int i = 0; i<student.size();i++){ out.print(sutudent.getName()); }

就是这样。要点是将arrayList传递给链接的参数。