【发布时间】:2014-04-14 12:44:07
【问题描述】:
我正在尝试在向 servlet 提交表单时从 jsp 页面传递 arraylist 对象。
jsp页面代码:-
<form action="NewServlet">
<%
ArrayList al=new ArrayList();
al.add("abc");
al.add("xyz");
request.setAttribute("allproducts", al);
%>
<input type="submit" value="Show"></form>
NewServlet 的代码:-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
ArrayList al=(ArrayList)request.getAttribute("allproducts");
System.out.print(al.get(0));
}
当我运行此代码时,我在“System.out.print(al.get(0))”行收到NullPointerException。
谁能告诉我为什么会这样?
如果我想在 servlet 中使用这个 al 对象,我该怎么办?
【问题讨论】: