url 重定向时的会话变量

发布于 2024-12-21 01:45:32 字数 447 浏览 0 评论 0 原文

我有一个在 tomcat 服务器上运行的应用程序,该应用程序被重定向到另一个网站进行密码身份验证(并存储一些数据),然后重定向回我的 tomcat 服务器

EX

1 到 A
A 到 B
B 至 2

其中 1 和 2 是我的申请中的页面
A 和 B 是其他应用程序上的页面

我在页面 1 上设置会话变量

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

,并在页面 2 上使用它,

String loginUser= session.getAttribute("loginUser");

但在 2 上出现空指针异常

I have a application running on tomcat server , which is redirected to another website for password authentication ( and store some data) , and then redirected back to my tomcat server

EX

1 to A
A to B
B to 2

Where 1 and 2 are pages on my application
A and B are pages on other applications

I am setting a session variable on my page 1 using

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

and using it on page 2 using

String loginUser= session.getAttribute("loginUser");

but getting a null pointer exception on 2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

仅此而已 2024-12-28 01:45:32

如果您在同一上下文(应用程序)中执行重定向,请使用 RequestDispatcher 执行 forward(传递您的 request >响应)。这样,您就可以完成会话。

否则,我的建议是不要将 loginUser 存储在会话上,而是将用户 id 变量作为一些智能(且混乱)字符串传递给其他应用程序,编写一种机制来检索登录会话状态的用户,并从那里继续。这称为单点登录。

If you're doing a redirect in the same context (Application), rather use RequestDispatcher to do a forward (passing your request and response). That way, you pass your session through.

Otherwise, my suggestion is to not store the loginUser on the session but pass user id variable as some intelligent (and confused) string to the other application, write a mechanism to retrieve the user logged in session state and carry on from there. This is called, Single-Sign On.

蓝色星空 2024-12-28 01:45:32

发生这种情况可能是因为

“会话是暂时的,当出现以下情况之一时,其生命周期就会结束:

A user leaves your site and the user's browser does not accept cookies.

A user quits the browser.

The session is timed out due to inactivity.

The session is completed and invalidated by the servlet.

The user logs out and is invalidated by the servlet.'

请参阅http://docs.oracle.com/cd/E15523_01/web.1111/e13712/sessions.htm

it may be happening because of

'A session is transient, and its lifetime ends when one of the following occurs:

A user leaves your site and the user's browser does not accept cookies.

A user quits the browser.

The session is timed out due to inactivity.

The session is completed and invalidated by the servlet.

The user logs out and is invalidated by the servlet.'

refer http://docs.oracle.com/cd/E15523_01/web.1111/e13712/sessions.htm

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