我是关于j2EE的新手,我正在用servlet和EJB移动我的第一步。我使用Netbeans,Glassfish。当我传递我的数据(我希望在我的JSP中查看postgress中的Products表中的数据以及我确定有效的连接)从servlet到JSP时,我有一个奇怪的空指针异常。 这是Servlet类中的processRequestMethod:
protected void processRequest(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
List<Products> list=new ArrayList<Products>();
list = productsFacade.findAll();
request.setAttribute("list", list);
String arg = "/" + this.getServletName() + ".jsp";
RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(arg);
dispatcher.forward(request, response);
}
方法findAll():
public List<Products> findAll()
{
return em.createQuery("SELECT p FROM Products p").getResultList();
}
相反,JSP就是这样:
<%@page import="java.util.ArrayList"%>
<%@page import="entities.Products"%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="entities.Products_"%>
<%@page import="java.util.List"%>
<%
List<Products> list=new ArrayList<Products>();
list = (List<Products>)request.getAttribute("list");
%>
<!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=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Content of my Table</h1>
<%
if (list != null)
{ for (Products e: list)
{ out.println(e.getName() + " " + e.getPrice() + "<br/>"); }
}
%>
</body>
</html>
为什么我的列表是NULL?