我将数组对象从servlet传递给jsp有问题。当我尝试将其传递给jsp时,我得到NullPointerException但是如果我在servlet中打印该对象,我就拥有了所有数据。
Servlet:
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM APP.CUSTOMER");
CustomerBean customer = null;
List<CustomerBean> list = new ArrayList<CustomerBean>();
while (rs.next()) {
customer = new CustomerBean();
customer.setCustomer_id(rs.getInt("CUSTOMER_ID"));
customer.setName(rs.getString("NAME"));
customer.setPhone(rs.getString("PHONE"));
customer.setEmail(rs.getString("EMAIL"));
customer.setCity(rs.getString("CITY"));
list.add(customer);
}
request.getSession().setAttribute("list", list);
RequestDispatcher disp;
disp = getServletContext( ).getRequestDispatcher("index.jsp");
disp.forward(request, response);
JSP:
<%
// retrieve your list from the request, with casting
ArrayList<CustomerBean> list = (ArrayList<CustomerBean>) request.getAttribute("list");
// print the information about every category of the list
for(CustomerBean list1 : list) {
out.println(list1.getCustomer_id());
out.println(list1.getName());
out.println(list1.getPhone());
out.println(list1.getEmail());
out.println(list1.getCity());
}
%>
更新
现在当我有一个数据列表时,如何在新的jsp中只显示一行点击href。我有类似的东西,但当我点击id不通过时:
for(CustomerBean list1 : list) {
out.println("<td>"+list1.getCustomer_id()+"</td>");
out.println("<td>"+list1.getName()+"</td>");
out.println("<td>"+list1.getPhone()+"</td>");
out.println("<td>"+list1.getEmail()+"</td>");
out.println("<td>"+list1.getCity()+"</td>");
out.println("<td><a href='details.jsp?customer_id='+list1.getCustomer_id()>details</a></td>");
out.println("</tr>");
}
答案 0 :(得分:0)
您正在将list
添加到会话中
request.getSession().setAttribute("list", list);
但尝试从您的请求中检索
ArrayList<CustomerBean> list = (ArrayList<CustomerBean>) request.getAttribute("list");
尝试将其添加到您request
request.setAttribute("list", list);