如何在 HttpSession 中存储 Java 对象?

发布于 2024-11-03 04:48:59 字数 612 浏览 5 评论 0原文

因此,当请求该 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我的痛♀有谁懂 2024-11-10 04:48:59

您不是将对象添加到会话中,而是将其添加到请求中。
您需要的是:

HttpSession session = request.getSession();
session.setAttribute("MySessionVariable", param);

在 Servlet 中,您有 4 个可以存储数据的范围。

  1. 应用程序
  2. 会话
  3. 请求
  4. 页面

确保您了解这些。有关更多信息,请查看此处

You are not adding the object to the session, instead you are adding it to the request.
What you need is:

HttpSession session = request.getSession();
session.setAttribute("MySessionVariable", param);

In Servlets you have 4 scopes where you can store data.

  1. Application
  2. Session
  3. Request
  4. Page

Make sure you understand these. For more look here

落花随流水 2024-11-10 04:48:59

将其添加到会话,而不是请求

HttpSession session = request.getSession();
session.setAttribute("object", object);

另外,不要在 JSP 中使用 scriptlet。使用 EL 代替;要访问object,您只需要${object}

JSP 技术 2.0 版的一个主要特性是它对表达式语言 (EL) 的支持。表达式语言可以轻松访问存储在 JavaBeans 组件中的应用程序数据。例如,JSP 表达式语言允许页面作者使用简单语法(例如表示简单变量的 ${name}${name.foo.bar} 对于嵌套属性。


Add it to the session, not to the request.

HttpSession session = request.getSession();
session.setAttribute("object", object);

Also, don't use scriptlets in the JSP. Use EL instead; to access object all you need is ${object}.

A primary feature of JSP technology version 2.0 is its support for an expression language (EL). An expression language makes it possible to easily access application data stored in JavaBeans components. For example, the JSP expression language allows a page author to access a bean using simple syntax such as ${name} for a simple variable or ${name.foo.bar} for a nested property.

月亮邮递员 2024-11-10 04:48:59

在这里,您可以使用HttpRequestHttpSession来完成。并认为您的问题出在 JSP 内。

如果您要使用内部servlet,请执行以下操作,

Object obj = new Object();
session.setAttribute("object", obj);

或者

HttpSession session = request.getSession();
Object obj = new Object();
session.setAttribute("object", obj);

在使用请求或会话设置属性后,使用以下在JSP中访问它,

<%= request.getAttribute("object")%>

或者

<%= session.getAttribute("object")%>

看来您的问题出在JSP中。

如果你想使用 scriptlet,它应该如下所示,

<%
Object obj = request.getSession().getAttribute("object");
out.print(obj);
%>

或者可以使用如下表达式,

<%= session.getAttribute("object")%>

或者可以使用 EL,如下所示,
${object}${sessionScope.object}

Here you can do it by using HttpRequest or HttpSession. And think your problem is within the JSP.

If you are going to use the inside servlet do following,

Object obj = new Object();
session.setAttribute("object", obj);

or

HttpSession session = request.getSession();
Object obj = new Object();
session.setAttribute("object", obj);

and after setting your attribute by using request or session, use following to access it in the JSP,

<%= request.getAttribute("object")%>

or

<%= session.getAttribute("object")%>

So seems your problem is in the JSP.

If you want use scriptlets it should be as follows,

<%
Object obj = request.getSession().getAttribute("object");
out.print(obj);
%>

Or can use expressions as follows,

<%= session.getAttribute("object")%>

or can use EL as follows,
${object} or ${sessionScope.object}

伴我心暖 2024-11-10 04:48:59

请求对象不是会话。

你要使用session对象来存储。会话被添加到请求中,并且是您想要跨请求保存数据的地方。可以从 获取会话

HttpSession session = request.getSession(true);

然后您可以在会话上使用 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

HttpSession session = request.getSession(true);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文