且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 JSP 页面中使用 request.setAttribute

更新时间:2023-01-16 17:14:02

没有.不幸的是,Request 对象仅在页面完成加载之前可用 - 一旦完成,您将丢失其中的所有值,除非它们已存储在某处.

No. Unfortunately the Request object is only available until the page finishes loading - once it's complete, you'll lose all values in it unless they've been stored somewhere.

如果您想通过请求保留属性,您需要:

If you want to persist attributes through requests you need to either:

  1. 在你的表单中有一个隐藏的输入,比如 "/>.这将在 servlet 中作为请求参数提供.
  2. 将其放入会话中(请参阅 request.getSession() - 在 JSP 中,这仅作为 session 可用)
  1. Have a hidden input in your form, such as <input type="hidden" name="myhiddenvalue" value="<%= request.getParameter("value") %>" />. This will then be available in the servlet as a request parameter.
  2. Put it in the session (see request.getSession() - in a JSP this is available as simply session)

我建议使用会话,因为它更易于管理.

I recommend using the Session as it's easier to manage.