将列表数据从jsp发送到servlet

发布于 2024-10-02 14:50:43 字数 300 浏览 0 评论 0原文

我有一个 List,我想将其作为 GET 查询字符串参数传递给后续请求:

<a href="servlet?list=<%=request.getAttribute("list")%>">link</a>

在 servlet 内,我尝试按如下方式检索它:

String[] list = req.getParameterValues("list");

它不起作用。我怎样才能让它发挥作用?

I have a List and I want to pass it to the subsequent request as GET query string parameter:

<a href="servlet?list=<%=request.getAttribute("list")%>">link</a>

Inside the servlet I am trying to retrieve it as follows:

String[] list = req.getParameterValues("list");

It does not work. How can I get it to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独难免 2024-10-09 14:50:43

为了能够使用getParameterValues(),必须以以下格式发送多个参数:

list=item1&list=item2&list=item3

但是List#toString() 打印以下格式(在浏览器中右键单击页面并选择查看源代码来查看它):

list=[item1,item2,item3]

这显然行不通。有几种方法可以解决这个问题:

  1. 正如 Bozho 所说,打印它以逗号分隔(或保持不变)并使用 request.getParameter() 代替,分割字符串并使用常见的 String 方法,例如 split()substring()indexOf()

  2. 只需以预期格式打印即可。最好的方法是为此创建一个 EL 函数。

  3. 将其存储在会话中:

     request.getSession().setAttribute("list", list);
    

    这样您就可以在下一个请求中从同一会话中检索它:

     列表 list = (List) request.getSession().getAttribute("list");
    

    如果需要,您可以将密钥作为请求参数传递。

  4. 如果您已经在服务器端(应用程序范围、数据库等)中拥有该列表,那么就不要传递该列表。仅传递那些能够提供足够信息以重新加载/重新填充 servlet 中的列表的参数。查询字符串有最大长度限制,最好不超过 255 个 ASCII 字符。如果列表包含超过数百个项目,那么您将面临它们被截断的风险。

In order to be able to use getParameterValues(), multiple parameters has to be sent in the format:

list=item1&list=item2&list=item3

But the List#toString() prints the following format (rightclick page in browser and choose View Source to see it):

list=[item1,item2,item3]

This is obviously not going to work. There are several ways to solve this problem:

  1. As Bozho said, print it commaseparated (or keep it unchanged) and use request.getParameter() instead and split the string and repopulate the list using the usual String methods like split(), substring(), indexOf(), etc.

  2. Just print it in the expected format. Nicest would be to create an EL function for that.

  3. Store it in the session:

     request.getSession().setAttribute("list", list);
    

    so that you can just retrieve it from the same session in the next request:

     List list = (List) request.getSession().getAttribute("list");
    

    If necessary, you can pass the key as request parameter instead.

  4. If you already have the list in the server side (application scope, database, etc), then just don't pass the list around. Pass only those parameters around which gives enough information to reload/repopulate the list in the servlet. The query string has a limitation in maximum length which should preferably not exceed 255 ASCII characters. If the list contains over some hundred of items, you risk that they will be truncated anyway.

汐鸠 2024-10-09 14:50:43

list 属性是一个 List,您不应该依赖它的 toString(),它在您的代码中调用(在幕后) 。

相反,您必须迭代列表并在元素之间插入逗号。

the list attribute is a List, and you should not rely on its toString(), which is called in your code (behind the scene).

Instead, you have to iterate the list and insert commas between the elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文