在 JSP 页面中使用 request.setAttribute
是否可以在 JSP 页面上使用 request.setAttribute
,然后在 HTML Submit 上在 Servlet
中获取相同的请求属性?
Is it possible to use request.setAttribute
on a JSP page and then on HTML Submit get the same request attribute in the Servlet
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不可以。不幸的是,Request 对象仅在页面加载完成之前可用 - 一旦加载完成,您将丢失其中的所有值,除非它们已存储在某处。
如果您想通过请求保留属性,您需要:
request.getSession()
- 在 JSP 中,这可以简单地用作session
)。我建议使用 Session,因为它更易于管理。
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:
<input type="hidden" name="myhiddenvalue" value="<%= request.getParameter("value") %>" />
. This will then be available in the servlet as a request parameter.request.getSession()
- in a JSP this is available as simplysession
)I recommend using the Session as it's easier to manage.
Phil Sacre 的答复是正确的,但是该会议不应该仅仅用于该死的目的。 您应该仅将其用于真正需要在会话生命周期内生存的值,例如用户登录。 人们经常会过度使用会话并遇到更多问题,尤其是在处理集合时,或者当用户返回到之前访问过的页面时却发现仍有上次访问时剩余的值。 一个聪明的程序会尽可能地减少变量的范围,而一个糟糕的程序会过多地使用会话。
The reply by Phil Sacre was correct however the session shouldn't be used just for the hell of it. You should only use this for values which really need to live for the lifetime of the session, such as a user login. It's common to see people overuse the session and run into more issues, especially when dealing with a collection or when users return to a page they previously visited only to find they have values still remaining from a previous visit. A smart program minimizes the scope of variables as much as possible, a bad one uses session too much.
不过,您可以使用 pageContext 属性来完成此操作:
在 JSP 中:
在 Servlet 中(链接到“Enter.do”url 模式):
除了 APPLICATION_SCOPE 之外,还有其他范围,例如 SESSION_SCOPE。 APPLICATION_SCOPE 用于 ServletContext 属性。
You can do it using pageContext attributes, though:
In the JSP:
In the Servlet (linked to the "Enter.do" url-pattern):
There are other scopes besides APPLICATION_SCOPE like SESSION_SCOPE. APPLICATION_SCOPE is used for ServletContext attributes.
如果您希望请求持续存在,请尝试以下操作:
示例:在您的 JSP 或 servlet 页面
以及任何接收页面上,使用以下行来检索您的会话和数据:
If you want your requests to persists try this:
example: on your JSP or servlet page
and on any receiving page use the below lines to retrieve your session and data:
尝试
Try
如果错误请纠正我...我认为请求确实在连续页面之间持续存在..
认为您从页面 1--> 遍历 第2页-->第3页。
您使用第1页中的setAttribute在请求对象中设置了一些值,您在第2页中使用getAttribute检索该值,然后如果您尝试在同一请求对象中再次设置某些内容以在第3页中检索它然后它无法为您提供 null 值,因为“创建 JSP 的请求和提交 JSP 时生成的请求是完全不同的请求,并且放置在第一个请求上的任何属性在第二个请求上将不可用”。
我的意思是第二页中类似的事情失败了:
在第一页的情况下同样的事情也有效:
所以我认为我需要继续使用菲尔建议的两个选项之一。
Correct me if wrong...I think request does persist between consecutive pages..
Think you traverse from page 1--> page 2-->page 3.
You have some value set in the request object using setAttribute from page 1, which you retrieve in page 2 using getAttribute,then if you try setting something again in same request object to retrieve it in page 3 then it fails giving you null value as "the request that created the JSP, and the request that gets generated when the JSP is submitted are completely different requests and any attributes placed on the first one will not be available on the second".
I mean something like this in page 2 fails:
Where as the same thing has worked in case of page 1 like:
So I think I would need to proceed with either of the two options suggested by Phill.
我认为菲尔是正确的,请求选项在页面加载之前可用。 因此,如果我们想将值发送到另一个页面,我们想要在隐藏标签中或在会话中设置,如果您只需要另一个页面上的值而不需要更多,那么如果您需要该值,那么隐藏标签是最好的选择此时在多个页面上会话是比隐藏标签更好的选择。
i think phil is right request option is available till the page load. so if we want to sent value to another page we want to set the in the hidden tag or in side the session if you just need the value only on another page and not more than that then hidden tags are best option if you need that value on more than one page at that time session is the better option than hidden tags.