Java 在 JSP 页面中使用 request.setAttribute

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/229937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:47:22  来源:igfitidea点击:

Using request.setAttribute in a JSP page

javajakarta-eesetattributerequest-object

提问by ria

Is it possible to use request.setAttributeon a JSP page and then on HTML Submit get the same request attribute in the Servlet?

是否可以request.setAttribute在 JSP 页面上使用,然后在 HTML Submit 中获得相同的请求属性Servlet

采纳答案by ria

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.

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

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

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

  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)
  1. 在您的表单中有一个隐藏的输入,例如<input type="hidden" name="myhiddenvalue" value="<%= request.getParameter("value") %>" />. 然后这将作为请求参数在 servlet 中可用。
  2. 把它放在会话中(参见request.getSession()- 在 JSP 中,这可以简单地使用session

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

我建议使用 Session,因为它更易于管理。

回答by ria

Correct me if wrong...I think request does persist between consecutive pages..

如果错误,请纠正我......我认为请求确实在连续页面之间持续存在..

Think you traverse from page 1--> page 2-->page 3.

认为您从第 1 页--> 第 2 页--> 第 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".

您使用第 1 页的 setAttribute 在请求对象中设置了一些值,您在第 2 页使用 getAttribute 检索该值,然后如果您尝试在同一请求对象中再次设置某些内容以在第 3 页检索它,则它无法为您提供空值“创建 JSP 的请求和提交 JSP 时生成的请求是完全不同的请求,放在第一个上的任何属性在第二个上都将不可用”。

I mean something like this in page 2 fails:

我的意思是第 2 页中的类似内容失败了:

Where as the same thing has worked in case of page 1 like:

在第 1 页的情况下,同样的事情也有效,例如:

So I think I would need to proceed with either of the two options suggested by Phill.

所以我想我需要继续执行 Phill 建议的两个选项中的任何一个。

回答by user31475

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 reallyneed 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.

Phil Sacre 的回答是正确的,但是会话不应该仅仅用于它的地狱。您应该只将它用于真正需要在会话的生命周期内存活的值,例如用户登录。经常会看到人们过度使用会话并遇到更多问题,尤其是在处理集合时,或者当用户返回他们之前访问过的页面时,却发现他们仍有上次访问留下的值。一个聪明的程序尽可能地最小化变量的范围,一个坏的程序使用过多的会话。

回答by user31475

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.

我认为 phil 是正确的请求选项,直到页面加载为止。因此,如果我们想将值发送到另一个页面,我们希望在隐藏标签中或在会话中设置,如果您只需要另一个页面上的值而不是更多,那么如果您需要该值,隐藏标签是最佳选择在超过一页的那个时间会话是比隐藏标签更好的选择。

回答by Lino

Try

尝试

request.getSession().setAttribute("SUBFAMILY", subFam);
request.getSession().getAttribute("SUBFAMILY");

回答by connect2krish

If you want your requests to persists try this:

如果您希望您的请求持续存在,请尝试以下操作:

example: on your JSP or servlet page

示例:在您的 JSP 或 servlet 页面上

request.getSession().setAttribute("SUBFAMILY", subFam);

and on any receiving page use the below lines to retrieve your session and data:

并在任何接收页面上使用以下几行来检索您的会话和数据:

SubFamily subFam = (SubFamily)request.getSession().getAttribute("SUBFAMILY");

回答by Prasad

You can do it using pageContext attributes, though:

不过,您可以使用 pageContext 属性来做到这一点:

In the JSP:

在 JSP 中:

<form action="Enter.do">
    <button type="SUBMIT" id="btnSubmit" name="btnSubmit">SUBMIT</button>
</form>
<% String s="opportunity";
pageContext.setAttribute("opp", s, PageContext.APPLICATION_SCOPE); %>

In the Servlet (linked to the "Enter.do" url-pattern):

在 Servlet 中(链接到“Enter.do”url-pattern):

String s=(String) request.getServletContext().getAttribute("opp");

There are other scopes besides APPLICATION_SCOPE like SESSION_SCOPE. APPLICATION_SCOPE is used for ServletContext attributes.

除了 APPLICATION_SCOPE 之外,还有其他作用域,如 SESSION_SCOPE。APPLICATION_SCOPE 用于 ServletContext 属性。