我有一个jsp页面(说,MyJspPage.jsp
)-
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%
ArrayList<Person> ownerList = (ArrayList<Person>) request
.getAttribute("ownerList");
//set again ..
request.setAttribute("ownerList",ownerList) ;
%>
</head>
<body>
<%
//itr on all the persons ..;
for (Person person : ownerList) {
%>
// some HTML code..
<%
}
%>
<form action="servlet123" method="POST">
// some fields ..
<input type="submit" value="join" />
</form>
</body>
</html>
还有一个 servlet -
@WebServlet("/servlet123")
public class servlet123 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// get the then set ..
ArrayList<Person> ownerList = (ArrayList<Person)request.getAttribute("ownerList");
request.setAttribute("ownerList", ownerList);
// forward to `MyJspPage.jsp`
dispather.forward(request, response);
}
}
首先是另一个 servlet 转发到它工作正常,然后在andMyJspPage.jsp
之间就像乒乓球一样 。问题是,当第二次到达它时会引发异常 -MyJspPage.jsp
servlet123
MyJspPage.jsp
type Exception report
message java.lang.NullPointerException
description The server encountered an internal error (java.lang.NullPointerException) that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
应该注意的是,当我从 MyJspPage.jsp 中省略 for 循环并将其更改为 -
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<%
ArrayList<Person> ownerList = (ArrayList<Person>) request
.getAttribute("ownerList");
//set again ..
request.setAttribute("ownerList",ownerList) ;
%>
</head>
<body>
<form action="servlet123" method="POST">
// some fields ..
<input type="submit" value="join" />
</form>
</body>
</html>
MyJspPage.jsp
和回报之间的所有关系都servlet123
很好。