【发布时间】:2014-04-14 17:53:17
【问题描述】:
这是我的 jsp 页面。
jsp页面代码:-
<img src='image.jpg' height=200px width=200px>
<form action="buyserv">
<%
ArrayList al=new ArrayList();
al.add("naman");
al.add("gupta");
request.setAttribute("allproducts", al);
RequestDispatcher rd = request.getRequestDispatcher("/buyserv");
rd.forward(request, response);
%>
<input type="submit" value="Buy"></form>
<a href="ShowAllProducts.jsp"><input type="button" value="Continue"></a>
我希望在单击 Buy 按钮时,arraylist (al) 应该传递给 buyserv(servlet)。 但是列表正在传递给buyserv,但问题是没有显示jsp页面。但是我想显示jsp页面并在按钮单击时传递arraylist。 谁能告诉我该怎么做?
buyserv 代码:-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException
{
ArrayList al=(ArrayList)request.getAttribute("allproducts");
PrintWriter out=response.getWriter();
out.print(al.get(0));
}
【问题讨论】:
-
为什么要将 硬编码 列表从 JSP 传递到 servlet?如果列表是硬编码的,它应该被硬编码在一个可供双方访问的类中。