如何在 HttpSession 中存储 Java 对象?
因此,当请求该 servlet 时,我试图让 servlet 将 Java 对象添加到用户的会话中。但在 servlet 重定向到下一页并尝试检索该对象后,我得到了一个 null
对象。
以下是我将对象添加到 HttpSession(在 servlet 中)的操作:
request.setAttribute("object", obj);
然后我尝试通过(在 JSP 中)检索它:
Object obj = request.getAttribute("object");
那么如何让 obj 不为 null?
更新: 我也尝试过这一点:
HttpSession session = request.getSession();
session.setAttribute("object", obj);
在 JSP 中使用以下内容:
Object obj = request.getSession().getAttribute("object");
两种方式仍然返回 null。
So I am trying to get a servlet to add a Java object to the session of the user, when this servlet is requested. But after the servlet redirects to the next page and I try to retrieve the object, I get a null
object instead.
Here is what I do to add the object to the HttpSession (in the servlet):
request.setAttribute("object", obj);
Then I try to retrieve it by (in the JSP):
Object obj = request.getAttribute("object");
So how would I get obj to not be null?
Update:
I have also tried this with nothing:
HttpSession session = request.getSession();
session.setAttribute("object", obj);
with the following in the JSP:
Object obj = request.getSession().getAttribute("object");
Both ways still return null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不是将对象添加到会话中,而是将其添加到请求中。
您需要的是:
在 Servlet 中,您有 4 个可以存储数据的范围。
确保您了解这些。有关更多信息,请查看此处
You are not adding the object to the session, instead you are adding it to the request.
What you need is:
In Servlets you have 4 scopes where you can store data.
Make sure you understand these. For more look here
将其添加到会话,而不是请求。
另外,不要在 JSP 中使用 scriptlet。使用 EL 代替;要访问
object
,您只需要${object}
。Add it to the session, not to the request.
Also, don't use scriptlets in the JSP. Use EL instead; to access
object
all you need is${object}
.在这里,您可以使用
HttpRequest
或HttpSession
来完成。并认为您的问题出在 JSP 内。如果您要使用内部servlet,请执行以下操作,
或者
在使用请求或会话设置属性后,使用以下在JSP中访问它,
或者
看来您的问题出在JSP中。
如果你想使用 scriptlet,它应该如下所示,
或者可以使用如下表达式,
或者可以使用 EL,如下所示,
${object}
或${sessionScope.object}
Here you can do it by using
HttpRequest
orHttpSession
. And think your problem is within the JSP.If you are going to use the inside servlet do following,
or
and after setting your attribute by using request or session, use following to access it in the JSP,
or
So seems your problem is in the JSP.
If you want use scriptlets it should be as follows,
Or can use expressions as follows,
or can use EL as follows,
${object}
or${sessionScope.object}
请求对象不是会话。
你要使用session对象来存储。会话被添加到请求中,并且是您想要跨请求保存数据的地方。可以从 获取会话
然后您可以在会话上使用 setAttribute 或 getAttribute。
有关 jsp 会话的最新教程是:http: //courses.coreservlets.com/Course-Materials/pdf/csajsp2/08-Session-Tracking.pdf
The request object is not the session.
You want to use the session object to store. The session is added to the request and is were you want to persist data across requests. The session can be obtained from
Then you can use setAttribute or getAttribute on the session.
A more up to date tutorial on jsp sessions is: http://courses.coreservlets.com/Course-Materials/pdf/csajsp2/08-Session-Tracking.pdf