在 JSP 页面中使用 request.setAttribute [英] Using request.setAttribute in a JSP page
本文介绍了在 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
?
推荐答案
没有.不幸的是,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:
- 在你的表单中有一个隐藏的输入,比如
"/>
.这将在 servlet 中作为请求参数提供. - 将其放入会话中(请参阅
request.getSession()
- 在 JSP 中,这仅作为session
可用)
- 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. - Put it in the session (see
request.getSession()
- in a JSP this is available as simplysession
)
我建议使用会话,因为它更易于管理.
I recommend using the Session as it's easier to manage.
这篇关于在 JSP 页面中使用 request.setAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文