小编典典

如何将数组列表从jsp发送到servlet

jsp

我想从jsp向Servlet发送ArrayList并在Servlet中检索该ArrayList。我正在使用form action=myServlet并发送ArrayList作为隐藏参数。但这是行不通的。

如何将数组列表从JSP发送到Servlet。有可能吗


阅读 339

收藏
2020-06-08

共1个答案

小编典典

如何将数组列表从JSP发送到Servlet。有可能吗

是的,肯定有可能,您需要

  • 在请求对象中填充arrayList对象。
  • 在servlet中检索列表。

由于您没有提供信息是使用脚本集还是使用jstl,因此我假设使用脚本集。

在您的JSP中,

 request.setAttribute("arrayList", yourListObject);
 RequestDispatcher rd = request.getRequestDispatcher("servletUrlPattern");
 rd.forward(request, response);

在servlet中,您可以使用以下命令访问列表

 List yourList = (List)request.getAttribute("arrayList");

要从您的JSP文件中调用servlet,

 <form action = "yourServletUrl" method = "POST">
    //everything in the form here.
     <input type = "submit" name = "submit">
 </form>

按提交按钮后,您将可以将jsp数据放入其中。

2020-06-08